-
Notifications
You must be signed in to change notification settings - Fork 8
/
schema_test.go
276 lines (239 loc) · 5.85 KB
/
schema_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package jsonapi_test
import (
"testing"
. "github.com/mfcochauxlaberge/jsonapi"
"github.com/stretchr/testify/assert"
)
func TestSchemaTypes(t *testing.T) {
assert := assert.New(t)
// Add a type
schema := &Schema{}
err := schema.AddType(Type{Name: "type1"})
assert.NoError(err)
assert.True(schema.HasType("type1"))
assert.False(schema.HasType("type2"))
// Add an invalid type (no name)
schema = &Schema{}
err = schema.AddType(Type{})
assert.Error(err)
// Add two types with the same name
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddType(Type{Name: "type1"})
assert.Error(err)
// Remove a type
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
schema.RemoveType("type1")
typ := schema.GetType("type1")
assert.Equal("", typ.Name)
// Add and remove an attribute
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
attr := Attr{
Name: "attr1",
Type: AttrTypeString,
Nullable: false,
}
err = schema.AddAttr("type1", attr)
assert.NoError(err)
typ = schema.GetType("type1")
assert.Contains(typ.Attrs, "attr1")
assert.Equal(attr, typ.Attrs["attr1"])
schema.RemoveAttr("type1", "attr1")
assert.NotContains(schema.GetType("type1").Attrs, "attr1")
// Add an invalid attribute (no name)
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddAttr("type1", Attr{Name: ""})
assert.Error(err)
// Add an invalid attribute (type does not exist)
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddAttr("type2", Attr{Name: "attr1"})
assert.Error(err)
// Add and remove an relationship
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
rel := Rel{
FromName: "rel1",
ToOne: true,
ToType: "type1",
}
err = schema.AddRel("type1", rel)
assert.NoError(err)
typ = schema.GetType("type1")
assert.Contains(typ.Rels, "rel1")
assert.Equal(rel, typ.Rels["rel1"])
schema.RemoveRel("type1", "rel1")
assert.NotContains(schema.GetType("type1").Rels, "rel1")
// Add an invalid relationship (no name)
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddRel("type1", Rel{FromName: ""})
assert.Error(err)
// Add an invalid relationship (type does not exist)
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddRel("type2", Rel{FromName: "rel1"})
assert.Error(err)
}
func TestSchemaAddTwoWayRel(t *testing.T) {
assert := assert.New(t)
// Add two-way relationship
schema := &Schema{}
_ = schema.AddType(Type{Name: "type1"})
_ = schema.AddType(Type{Name: "type2"})
err := schema.AddTwoWayRel(Rel{
FromType: "type1",
FromName: "parent",
ToOne: true,
ToType: "type2",
ToName: "children",
FromOne: false,
})
assert.NoError(err)
// Add two-way relationship (missing type)
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddTwoWayRel(Rel{
FromType: "type1",
FromName: "parent",
ToOne: true,
ToType: "type2",
ToName: "children",
FromOne: false,
})
assert.EqualError(err, `jsonapi: types "type1" and "type2" must exist`)
// Add two-way relationship (invalid relationship)
schema = &Schema{}
_ = schema.AddType(Type{Name: "type1"})
err = schema.AddTwoWayRel(Rel{
FromType: "type1",
FromName: "parent",
ToOne: true,
ToType: "",
ToName: "",
FromOne: false,
})
assert.EqualError(err, `jsonapi: relationship type is empty`)
err = schema.AddTwoWayRel(Rel{
FromType: "",
FromName: "",
ToOne: true,
ToType: "type1",
ToName: "parent",
FromOne: false,
})
assert.EqualError(err, `jsonapi: relationship type is empty`)
}
func TestSchemaCheck(t *testing.T) {
assert := assert.New(t)
schema := &Schema{}
type1 := Type{
Name: "type1",
Attrs: map[string]Attr{},
Rels: map[string]Rel{
"rel1": {
FromName: "rel1",
ToType: "type2",
},
"rel2": {
FromName: "rel2-invalid",
ToType: "nonexistent",
},
"rel3": {
FromName: "rel3",
ToType: "type1",
},
},
}
err := schema.AddType(type1)
assert.NoError(err)
type2 := Type{
Name: "type2",
Attrs: map[string]Attr{},
Rels: map[string]Rel{
"rel1": {
FromName: "rel1",
FromType: "type1",
ToName: "rel1",
ToType: "type1",
},
"rel2": {
FromName: "rel2",
FromType: "type2",
ToName: "rel3",
ToType: "type1",
},
},
}
err = schema.AddType(type2)
assert.NoError(err)
// assert.NotEmpty(schema.Types)
// assert.NotEmpty(schema.GetType("type1").Rels)
// Check schema
errs := schema.Check()
errsStr := []string{}
for _, err := range errs {
errsStr = append(errsStr, err.Error())
}
assert.Len(errs, 3)
assert.Contains(
errsStr,
"jsonapi: field ToType of relationship \"rel2-invalid\" of type \"type1\" does not exist",
)
assert.Contains(
errsStr,
"jsonapi: field FromType of relationship \"rel1\" "+
"must be its type's name (\"type2\", not \"type1\")",
)
assert.Contains(
errsStr,
"jsonapi: relationship \"rel2\" of type \"type2\" and its inverse do not point each other",
)
}
func TestSchemaRels(t *testing.T) {
assert := assert.New(t)
schema := &Schema{}
users := Type{
Name: "users",
Rels: map[string]Rel{
"posts": {
FromName: "posts",
FromType: "users",
ToOne: false,
ToName: "author",
ToType: "messages",
FromOne: true,
},
"favorites": {
FromName: "favorites",
FromType: "users",
ToOne: false,
ToName: "",
ToType: "messages",
FromOne: false,
},
},
}
_ = schema.AddType(users)
messages := Type{
Name: "messages",
Rels: map[string]Rel{
"author": {
FromName: "author",
FromType: "messages",
ToOne: true,
ToName: "posts",
ToType: "users",
FromOne: false,
},
},
}
_ = schema.AddType(messages)
rels := schema.Rels()
assert.Len(rels, 2)
assert.Equal(messages.Rels["author"], rels[0])
assert.Equal(users.Rels["favorites"], rels[1])
}