1
16
17 package schema
18
19 import (
20 "reflect"
21 "testing"
22 )
23
24 func TestFindNamedType(t *testing.T) {
25 tests := []struct {
26 testName string
27 defs []TypeDef
28 namedType string
29 expectTypeDef TypeDef
30 expectExist bool
31 }{
32 {"existing", []TypeDef{{Name: "a"}, {Name: "b"}}, "a", TypeDef{Name: "a"}, true},
33 {"notExisting", []TypeDef{{Name: "a"}, {Name: "b"}}, "c", TypeDef{}, false},
34 }
35 for _, tt := range tests {
36 tt := tt
37 t.Run(tt.testName, func(t *testing.T) {
38 t.Parallel()
39 s := Schema{
40 Types: tt.defs,
41 }
42 td, exist := s.FindNamedType(tt.namedType)
43 if !td.Equals(&tt.expectTypeDef) {
44 t.Errorf("expected TypeDef %v, got %v", tt.expectTypeDef, td)
45 }
46 if exist != tt.expectExist {
47 t.Errorf("expected existing %t, got %t", tt.expectExist, exist)
48 }
49 })
50 }
51 }
52
53 func strptr(s string) *string { return &s }
54
55 func TestFindField(t *testing.T) {
56 tests := []struct {
57 testName string
58 defs []StructField
59 fieldName string
60 expectStructField StructField
61 expectExist bool
62 }{
63 {"existing", []StructField{
64 {Name: "a", Type: TypeRef{NamedType: strptr("a")}},
65 {Name: "b", Type: TypeRef{NamedType: strptr("b")}},
66 }, "a", StructField{Name: "a", Type: TypeRef{NamedType: strptr("a")}}, true},
67 {"notExisting", []StructField{
68 {Name: "a", Type: TypeRef{NamedType: strptr("a")}},
69 {Name: "b", Type: TypeRef{NamedType: strptr("b")}},
70 }, "c", StructField{}, false},
71 }
72 for _, tt := range tests {
73 tt := tt
74 t.Run(tt.testName, func(t *testing.T) {
75 t.Parallel()
76 s := Map{
77 Fields: tt.defs,
78 }
79 sf, exist := s.FindField(tt.fieldName)
80 if !reflect.DeepEqual(sf, tt.expectStructField) {
81 t.Errorf("expected StructField %v, got %v", tt.expectStructField, sf)
82 }
83 if exist != tt.expectExist {
84 t.Errorf("expected existing %t, got %t", tt.expectExist, exist)
85 }
86 })
87 }
88 }
89
90 func TestResolve(t *testing.T) {
91 existing := "existing"
92 notExisting := "not-existing"
93 numeric := Numeric
94 granular := Separable
95 atomic := Atomic
96
97 a := Atom{List: &List{}}
98 b := Atom{Scalar: &numeric}
99
100 emptyMap := Map{}
101 atomicMap := Map{ElementRelationship: Atomic}
102
103 emptyList := List{}
104 atomicList := List{ElementRelationship: Atomic}
105
106 tests := []struct {
107 testName string
108 schemaTypeDefs []TypeDef
109 typeRef TypeRef
110 expectAtom Atom
111 expectExist bool
112 }{
113 {"noNamedType", nil, TypeRef{Inlined: a}, a, true},
114 {"notExistingNamedType", nil, TypeRef{NamedType: ¬Existing}, Atom{}, false},
115 {"existingNamedType", []TypeDef{{Name: existing, Atom: a}}, TypeRef{NamedType: &existing}, a, true},
116 {"invalidRelationshipOnScalarType", []TypeDef{{Name: existing, Atom: b}}, TypeRef{NamedType: &existing, ElementRelationship: &granular}, Atom{}, false},
117 {"mapElementRelationshipNamed", []TypeDef{{Name: existing, Atom: Atom{Map: &emptyMap}}}, TypeRef{NamedType: &existing, ElementRelationship: &atomic}, Atom{Map: &atomicMap}, true},
118 {"mapElementRelationshipInlined", nil, TypeRef{Inlined: Atom{Map: &emptyMap}, ElementRelationship: &atomic}, Atom{Map: &atomicMap}, true},
119 {"listElementRelationshipInlined", nil, TypeRef{Inlined: Atom{List: &emptyList}, ElementRelationship: &atomic}, Atom{List: &atomicList}, true},
120 }
121 for _, tt := range tests {
122 tt := tt
123 t.Run(tt.testName, func(t *testing.T) {
124 t.Parallel()
125 s := Schema{
126 Types: tt.schemaTypeDefs,
127 }
128 atom, exist := s.Resolve(tt.typeRef)
129 if !reflect.DeepEqual(atom, tt.expectAtom) {
130 t.Errorf("expected Atom %v, got %v", tt.expectAtom, atom)
131 }
132 if exist != tt.expectExist {
133 t.Errorf("expected exist %t, got %t", tt.expectExist, exist)
134 }
135 })
136 }
137 }
138
139 func TestCopyInto(t *testing.T) {
140 existing := "existing"
141 notExisting := "not-existing"
142 a := Atom{List: &List{}}
143
144 tests := []struct {
145 testName string
146 schemaTypeDefs []TypeDef
147 typeRef TypeRef
148 }{
149 {"noNamedType", nil, TypeRef{Inlined: a}},
150 {"notExistingNamedType", nil, TypeRef{NamedType: ¬Existing}},
151 {"existingNamedType", []TypeDef{{Name: existing, Atom: a}}, TypeRef{NamedType: &existing}},
152 }
153
154 for i := range tests {
155 tt := tests[i]
156 t.Run(tt.testName, func(t *testing.T) {
157 t.Parallel()
158 s := Schema{
159 Types: tt.schemaTypeDefs,
160 }
161
162 theCopy := Schema{}
163 s.CopyInto(&theCopy)
164
165 if !reflect.DeepEqual(&s, &theCopy) {
166 t.Fatal("")
167 }
168
169
170 _, _ = s.Resolve(tt.typeRef)
171 theCopy = Schema{}
172 s.CopyInto(&theCopy)
173
174 if !reflect.DeepEqual(&s, &theCopy) {
175 t.Fatal("")
176 }
177 })
178 }
179 }
180
View as plain text