Files
ent/schema/edge/edge_test.go
Ariel Mashraki bd07c86b60 all: add license header to all go files
Summary:
Used addlicense to generate this:
 addlicense -c "Facebook Inc" -f license_header .

example was taken from: https://github.com/facebook/litho/blob/master/lib/soloader/BUCK

Reviewed By: alexsn

Differential Revision: D17070152

fbshipit-source-id: e7b91398d7f6181727be3400c1872ad5f28e38ed
2019-08-27 04:48:28 -07:00

85 lines
2.0 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").
From("followers").
StructTag("followers").
Descriptor()
assert.Equal("followers", from.Tag)
assert.Equal("following", from.Ref.Tag)
}