forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema_test.go
executable file
·414 lines (350 loc) · 11.6 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package schema_test
import (
"testing"
"github.com/pocketbase/pocketbase/models/schema"
)
func TestNewSchemaAndFields(t *testing.T) {
testSchema := schema.NewSchema(
&schema.SchemaField{Id: "id1", Name: "test1"},
&schema.SchemaField{Name: "test2"},
&schema.SchemaField{Id: "id1", Name: "test1_new"}, // should replace the original id1 field
)
fields := testSchema.Fields()
if len(fields) != 2 {
t.Fatalf("Expected 2 fields, got %d (%v)", len(fields), fields)
}
for _, f := range fields {
if f.Id == "" {
t.Fatalf("Expected field id to be set, found empty id for field %v", f)
}
}
if fields[0].Name != "test1_new" {
t.Fatalf("Expected field with name test1_new, got %s", fields[0].Name)
}
if fields[1].Name != "test2" {
t.Fatalf("Expected field with name test2, got %s", fields[1].Name)
}
}
func TestSchemaInitFieldsOptions(t *testing.T) {
f0 := &schema.SchemaField{Name: "test1", Type: "unknown"}
schema0 := schema.NewSchema(f0)
err0 := schema0.InitFieldsOptions()
if err0 == nil {
t.Fatalf("Expected unknown field schema to fail, got nil")
}
// ---
f1 := &schema.SchemaField{Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Name: "test2", Type: schema.FieldTypeEmail}
schema1 := schema.NewSchema(f1, f2)
err1 := schema1.InitFieldsOptions()
if err1 != nil {
t.Fatal(err1)
}
if _, ok := f1.Options.(*schema.TextOptions); !ok {
t.Fatalf("Failed to init f1 options")
}
if _, ok := f2.Options.(*schema.EmailOptions); !ok {
t.Fatalf("Failed to init f2 options")
}
}
func TestSchemaClone(t *testing.T) {
f1 := &schema.SchemaField{Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Name: "test2", Type: schema.FieldTypeEmail}
s1 := schema.NewSchema(f1, f2)
s2, err := s1.Clone()
if err != nil {
t.Fatal(err)
}
s1Encoded, _ := s1.MarshalJSON()
s2Encoded, _ := s2.MarshalJSON()
if string(s1Encoded) != string(s2Encoded) {
t.Fatalf("Expected the cloned schema to be equal, got %v VS\n %v", s1, s2)
}
// change in one schema shouldn't result to change in the other
// (aka. check if it is a deep clone)
s1.Fields()[0].Name = "test1_update"
if s2.Fields()[0].Name != "test1" {
t.Fatalf("Expected s2 field name to not change, got %q", s2.Fields()[0].Name)
}
}
func TestSchemaAsMap(t *testing.T) {
f1 := &schema.SchemaField{Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Name: "test2", Type: schema.FieldTypeEmail}
testSchema := schema.NewSchema(f1, f2)
result := testSchema.AsMap()
if len(result) != 2 {
t.Fatalf("Expected 2 map elements, got %d (%v)", len(result), result)
}
expectedIndexes := []string{f1.Name, f2.Name}
for _, index := range expectedIndexes {
if _, ok := result[index]; !ok {
t.Fatalf("Missing index %q", index)
}
}
}
func TestSchemaGetFieldByName(t *testing.T) {
f1 := &schema.SchemaField{Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Name: "test2", Type: schema.FieldTypeText}
testSchema := schema.NewSchema(f1, f2)
// missing field
result1 := testSchema.GetFieldByName("missing")
if result1 != nil {
t.Fatalf("Found unexpected field %v", result1)
}
// existing field
result2 := testSchema.GetFieldByName("test1")
if result2 == nil || result2.Name != "test1" {
t.Fatalf("Cannot find field with Name 'test1', got %v ", result2)
}
}
func TestSchemaGetFieldById(t *testing.T) {
f1 := &schema.SchemaField{Id: "id1", Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Id: "id2", Name: "test2", Type: schema.FieldTypeText}
testSchema := schema.NewSchema(f1, f2)
// missing field id
result1 := testSchema.GetFieldById("test1")
if result1 != nil {
t.Fatalf("Found unexpected field %v", result1)
}
// existing field id
result2 := testSchema.GetFieldById("id2")
if result2 == nil || result2.Id != "id2" {
t.Fatalf("Cannot find field with id 'id2', got %v ", result2)
}
}
func TestSchemaRemoveField(t *testing.T) {
f1 := &schema.SchemaField{Id: "id1", Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Id: "id2", Name: "test2", Type: schema.FieldTypeText}
f3 := &schema.SchemaField{Id: "id3", Name: "test3", Type: schema.FieldTypeText}
testSchema := schema.NewSchema(f1, f2, f3)
testSchema.RemoveField("id2")
testSchema.RemoveField("test3") // should do nothing
expected := []string{"test1", "test3"}
if len(testSchema.Fields()) != len(expected) {
t.Fatalf("Expected %d, got %d (%v)", len(expected), len(testSchema.Fields()), testSchema)
}
for _, name := range expected {
if f := testSchema.GetFieldByName(name); f == nil {
t.Fatalf("Missing field %q", name)
}
}
}
func TestSchemaAddField(t *testing.T) {
f1 := &schema.SchemaField{Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{Id: "f2Id", Name: "test2", Type: schema.FieldTypeText}
f3 := &schema.SchemaField{Id: "f3Id", Name: "test3", Type: schema.FieldTypeText}
testSchema := schema.NewSchema(f1, f2, f3)
f2New := &schema.SchemaField{Id: "f2Id", Name: "test2_new", Type: schema.FieldTypeEmail}
f4 := &schema.SchemaField{Name: "test4", Type: schema.FieldTypeUrl}
testSchema.AddField(f2New)
testSchema.AddField(f4)
if len(testSchema.Fields()) != 4 {
t.Fatalf("Expected %d, got %d (%v)", 4, len(testSchema.Fields()), testSchema)
}
// check if each field has id
for _, f := range testSchema.Fields() {
if f.Id == "" {
t.Fatalf("Expected field id to be set, found empty id for field %v", f)
}
}
// check if f2 field was replaced
if f := testSchema.GetFieldById("f2Id"); f == nil || f.Type != schema.FieldTypeEmail {
t.Fatalf("Expected f2 field to be replaced, found %v", f)
}
// check if f4 was added
if f := testSchema.GetFieldByName("test4"); f == nil || f.Name != "test4" {
t.Fatalf("Expected f4 field to be added, found %v", f)
}
}
func TestSchemaValidate(t *testing.T) {
// emulate duplicated field ids
duplicatedIdsSchema := schema.NewSchema(
&schema.SchemaField{Id: "id1", Name: "test1", Type: schema.FieldTypeText},
&schema.SchemaField{Id: "id2", Name: "test2", Type: schema.FieldTypeText},
)
duplicatedIdsSchema.Fields()[1].Id = "id1" // manually set existing id
scenarios := []struct {
schema schema.Schema
expectError bool
}{
// no fields
{
schema.NewSchema(),
false,
},
// duplicated field ids
{
duplicatedIdsSchema,
true,
},
// duplicated field names (case insensitive)
{
schema.NewSchema(
&schema.SchemaField{Name: "test", Type: schema.FieldTypeText},
&schema.SchemaField{Name: "TeSt", Type: schema.FieldTypeText},
),
true,
},
// failure - base individual fields validation
{
schema.NewSchema(
&schema.SchemaField{Name: "", Type: schema.FieldTypeText},
),
true,
},
// success - base individual fields validation
{
schema.NewSchema(
&schema.SchemaField{Name: "test", Type: schema.FieldTypeText},
),
false,
},
// failure - individual field options validation
{
schema.NewSchema(
&schema.SchemaField{Name: "test", Type: schema.FieldTypeFile},
),
true,
},
// success - individual field options validation
{
schema.NewSchema(
&schema.SchemaField{Name: "test", Type: schema.FieldTypeFile, Options: &schema.FileOptions{MaxSelect: 1, MaxSize: 1}},
),
false,
},
}
for i, s := range scenarios {
err := s.schema.Validate()
hasErr := err != nil
if hasErr != s.expectError {
t.Errorf("(%d) Expected %v, got %v (%v)", i, s.expectError, hasErr, err)
continue
}
}
}
func TestSchemaMarshalJSON(t *testing.T) {
f1 := &schema.SchemaField{Id: "f1id", Name: "test1", Type: schema.FieldTypeText}
f2 := &schema.SchemaField{
Id: "f2id",
Name: "test2",
Type: schema.FieldTypeText,
Options: &schema.TextOptions{Pattern: "test"},
}
testSchema := schema.NewSchema(f1, f2)
result, err := testSchema.MarshalJSON()
if err != nil {
t.Fatal(err)
}
expected := `[{"system":false,"id":"f1id","name":"test1","type":"text","required":false,"presentable":false,"unique":false,"options":{"min":null,"max":null,"pattern":""}},{"system":false,"id":"f2id","name":"test2","type":"text","required":false,"presentable":false,"unique":false,"options":{"min":null,"max":null,"pattern":"test"}}]`
if string(result) != expected {
t.Fatalf("Expected %s, got %s", expected, string(result))
}
}
func TestSchemaUnmarshalJSON(t *testing.T) {
encoded := `[{"system":false,"id":"fid1", "name":"test1","type":"text","required":false,"unique":false,"options":{"min":null,"max":null,"pattern":""}},{"system":false,"name":"test2","type":"text","required":false,"unique":false,"options":{"min":null,"max":null,"pattern":"test"}}]`
testSchema := schema.Schema{}
testSchema.AddField(&schema.SchemaField{Name: "tempField", Type: schema.FieldTypeUrl})
err := testSchema.UnmarshalJSON([]byte(encoded))
if err != nil {
t.Fatal(err)
}
fields := testSchema.Fields()
if len(fields) != 2 {
t.Fatalf("Expected 2 fields, found %v", fields)
}
f1 := testSchema.GetFieldByName("test1")
if f1 == nil {
t.Fatal("Expected to find field 'test1', got nil")
}
if f1.Id != "fid1" {
t.Fatalf("Expected fid1 id, got %s", f1.Id)
}
_, ok := f1.Options.(*schema.TextOptions)
if !ok {
t.Fatal("'test1' field options are not inited.")
}
f2 := testSchema.GetFieldByName("test2")
if f2 == nil {
t.Fatal("Expected to find field 'test2', got nil")
}
if f2.Id == "" {
t.Fatal("Expected f2 id to be set, got empty string")
}
o2, ok := f2.Options.(*schema.TextOptions)
if !ok {
t.Fatal("'test2' field options are not inited.")
}
if o2.Pattern != "test" {
t.Fatalf("Expected pattern to be %q, got %q", "test", o2.Pattern)
}
}
func TestSchemaValue(t *testing.T) {
// empty schema
s1 := schema.Schema{}
v1, err := s1.Value()
if err != nil {
t.Fatal(err)
}
if v1 != "[]" {
t.Fatalf("Expected nil, got %v", v1)
}
// schema with fields
f1 := &schema.SchemaField{Id: "f1id", Name: "test1", Type: schema.FieldTypeText}
s2 := schema.NewSchema(f1)
v2, err := s2.Value()
if err != nil {
t.Fatal(err)
}
expected := `[{"system":false,"id":"f1id","name":"test1","type":"text","required":false,"presentable":false,"unique":false,"options":{"min":null,"max":null,"pattern":""}}]`
if v2 != expected {
t.Fatalf("Expected %v, got %v", expected, v2)
}
}
func TestSchemaScan(t *testing.T) {
scenarios := []struct {
data any
expectError bool
expectJson string
}{
{nil, false, "[]"},
{"", false, "[]"},
{[]byte{}, false, "[]"},
{"[]", false, "[]"},
{"invalid", true, "[]"},
{123, true, "[]"},
// no field type
{`[{}]`, true, `[]`},
// unknown field type
{
`[{"system":false,"id":"123","name":"test1","type":"unknown","required":false,"presentable":false,"unique":false}]`,
true,
`[]`,
},
// without options
{
`[{"system":false,"id":"123","name":"test1","type":"text","required":false,"presentable":false,"unique":false}]`,
false,
`[{"system":false,"id":"123","name":"test1","type":"text","required":false,"presentable":false,"unique":false,"options":{"min":null,"max":null,"pattern":""}}]`,
},
// with options
{
`[{"system":false,"id":"123","name":"test1","type":"text","required":false,"presentable":false,"unique":false,"options":{"min":null,"max":null,"pattern":"test"}}]`,
false,
`[{"system":false,"id":"123","name":"test1","type":"text","required":false,"presentable":false,"unique":false,"options":{"min":null,"max":null,"pattern":"test"}}]`,
},
}
for i, s := range scenarios {
testSchema := schema.Schema{}
err := testSchema.Scan(s.data)
hasErr := err != nil
if hasErr != s.expectError {
t.Errorf("(%d) Expected %v, got %v (%v)", i, s.expectError, hasErr, err)
continue
}
json, _ := testSchema.MarshalJSON()
if string(json) != s.expectJson {
t.Errorf("(%d) Expected json %v, got %v", i, s.expectJson, string(json))
}
}
}