forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
type_comparators_test.go
149 lines (139 loc) · 3.95 KB
/
type_comparators_test.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package graphql
import (
"testing"
)
func TestIsEqualType_SameReferenceAreEqual(t *testing.T) {
if !isEqualType(String, String) {
t.Fatalf("Expected same reference to be equal")
}
}
func TestIsEqualType_IntAndFloatAreNotEqual(t *testing.T) {
if isEqualType(Int, Float) {
t.Fatalf("Expected GraphQLInt and GraphQLFloat to not equal")
}
}
func TestIsEqualType_ListsOfSameTypeAreEqual(t *testing.T) {
if !isEqualType(NewList(Int), NewList(Int)) {
t.Fatalf("Expected lists of same type are equal")
}
}
func TestIsEqualType_ListsAreNotEqualToItem(t *testing.T) {
if isEqualType(NewList(Int), Int) {
t.Fatalf("Expected lists are not equal to item")
}
}
func TestIsEqualType_NonNullOfSameTypeAreEqual(t *testing.T) {
if !isEqualType(NewNonNull(Int), NewNonNull(Int)) {
t.Fatalf("Expected non-null of same type are equal")
}
}
func TestIsEqualType_NonNullIsNotEqualToNullable(t *testing.T) {
if isEqualType(NewNonNull(Int), Int) {
t.Fatalf("Expected non-null is not equal to nullable")
}
}
func testSchemaForIsTypeSubTypeOfTest(t *testing.T, fields Fields) *Schema {
schema, err := NewSchema(SchemaConfig{
Query: NewObject(ObjectConfig{
Name: "Query",
Fields: fields,
}),
})
if err != nil {
t.Fatalf("Invalid schema: %v", err)
}
return &schema
}
func TestIsTypeSubTypeOf_SameReferenceIsSubtype(t *testing.T) {
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: String},
})
if !isTypeSubTypeOf(schema, String, String) {
t.Fatalf("Expected same reference is subtype")
}
}
func TestIsTypeSubTypeOf_IntIsNotSubtypeOfFloat(t *testing.T) {
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: String},
})
if isTypeSubTypeOf(schema, Int, Float) {
t.Fatalf("Expected int is not subtype of float")
}
}
func TestIsTypeSubTypeOf_NonNullIsSubtypeOfNullable(t *testing.T) {
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: String},
})
if !isTypeSubTypeOf(schema, NewNonNull(Int), Int) {
t.Fatalf("Expected non-null is subtype of nullable")
}
}
func TestIsTypeSubTypeOf_NullableIsNotSubtypeOfNonNull(t *testing.T) {
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: String},
})
if isTypeSubTypeOf(schema, Int, NewNonNull(Int)) {
t.Fatalf("Expected nullable is not subtype of non-null")
}
}
func TestIsTypeSubTypeOf_ItemIsNotSubTypeOfList(t *testing.T) {
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: String},
})
if isTypeSubTypeOf(schema, Int, NewList(Int)) {
t.Fatalf("Expected item is not subtype of list")
}
}
func TestIsTypeSubTypeOf_ListIsNotSubtypeOfItem(t *testing.T) {
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: String},
})
if isTypeSubTypeOf(schema, NewList(Int), Int) {
t.Fatalf("Expected list is not subtype of item")
}
}
func TestIsTypeSubTypeOf_MemberIsSubtypeOfUnion(t *testing.T) {
memberType := NewObject(ObjectConfig{
Name: "Object",
IsTypeOf: func(p IsTypeOfParams) bool {
return true
},
Fields: Fields{
"field": &Field{Type: String},
},
})
unionType := NewUnion(UnionConfig{
Name: "Union",
Types: []*Object{memberType},
})
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: unionType},
})
if !isTypeSubTypeOf(schema, memberType, unionType) {
t.Fatalf("Expected member is subtype of union")
}
}
func TestIsTypeSubTypeOf_ImplementationIsSubtypeOfInterface(t *testing.T) {
ifaceType := NewInterface(InterfaceConfig{
Name: "Interface",
Fields: Fields{
"field": &Field{Type: String},
},
})
implType := NewObject(ObjectConfig{
Name: "Object",
IsTypeOf: func(p IsTypeOfParams) bool {
return true
},
Interfaces: []*Interface{ifaceType},
Fields: Fields{
"field": &Field{Type: String},
},
})
schema := testSchemaForIsTypeSubTypeOfTest(t, Fields{
"field": &Field{Type: implType},
})
if !isTypeSubTypeOf(schema, implType, ifaceType) {
t.Fatalf("Expected implementation is subtype of interface")
}
}