1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
// 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/facebook/ent"
"github.com/facebook/ent/schema"
"github.com/facebook/ent/schema/edge"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
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)
}
type GQL struct {
Field string
}
func (GQL) Name() string {
return "GQL"
}
func TestAnnotations(t *testing.T) {
type User struct{ ent.Schema }
to := edge.To("user", User.Type).
Annotations(GQL{Field: "to"}).
Descriptor()
require.Equal(t, []schema.Annotation{GQL{Field: "to"}}, to.Annotations)
from := edge.From("user", User.Type).
Annotations(GQL{Field: "from"}).
Descriptor()
require.Equal(t, []schema.Annotation{GQL{Field: "from"}}, from.Annotations)
bidi := edge.To("following", User.Type).
Annotations(GQL{Field: "to"}).
From("followers").
Annotations(GQL{Field: "from"}).
Descriptor()
require.Equal(t, []schema.Annotation{GQL{Field: "from"}}, bidi.Annotations)
require.Equal(t, []schema.Annotation{GQL{Field: "to"}}, bidi.Ref.Annotations)
}
|