Files
ent/schema/edge/edge_test.go
Ariel Mashraki 47f1a203aa schema/edge: an storage-key option for edges (#545)
* schema/edge: an storage-key option for edges

* schema/edge: use Column(s) with functional options
2020-06-15 23:06:00 +03:00

87 lines
2.2 KiB
Go

// Copyright 2019-present Facebook Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package edge_test
import (
"testing"
"github.com/facebookincubator/ent"
"github.com/facebookincubator/ent/schema/edge"
"github.com/stretchr/testify/assert"
)
func TestEdge(t *testing.T) {
assert := assert.New(t)
type User struct{ ent.Schema }
e := edge.To("friends", User.Type).
Required().
Descriptor()
assert.False(e.Inverse)
assert.Equal("User", e.Type)
assert.Equal("friends", e.Name)
assert.True(e.Required)
type Node struct{ ent.Schema }
e = edge.To("parent", Node.Type).
Unique().
Descriptor()
assert.False(e.Inverse)
assert.True(e.Unique)
assert.Equal("Node", e.Type)
assert.Equal("parent", e.Name)
assert.False(e.Required)
t.Log("m2m relation of the same type")
from := edge.To("following", User.Type).
From("followers").
Descriptor()
assert.True(from.Inverse)
assert.False(from.Unique)
assert.Equal("followers", from.Name)
assert.NotNil(from.Ref)
assert.Equal("following", from.Ref.Name)
assert.False(from.Ref.Unique)
t.Log("o2m relation of the same type")
from = edge.To("following", User.Type).
Unique().
From("followers").
Descriptor()
assert.False(from.Unique)
assert.True(from.Ref.Unique)
from = edge.To("following", User.Type).
From("followers").
Unique().
Descriptor()
assert.True(from.Unique)
assert.False(from.Ref.Unique)
t.Log("o2o relation of the same type")
from = edge.To("following", User.Type).
Unique().
From("followers").
Unique().
Descriptor()
assert.True(from.Unique)
assert.True(from.Ref.Unique)
e = edge.To("user", User.Type).
StructTag(`json:"user_name,omitempty"`).
Descriptor()
assert.Equal(`json:"user_name,omitempty"`, e.Tag)
from = edge.To("following", User.Type).
StructTag("following").
StorageKey(edge.Table("user_followers"), edge.Columns("following_id", "followers_id")).
From("followers").
StructTag("followers").
Descriptor()
assert.Equal("followers", from.Tag)
assert.Equal("following", from.Ref.Tag)
assert.Equal(edge.StorageKey{Table: "user_followers", Columns: []string{"following_id", "followers_id"}}, *from.Ref.StorageKey)
}