/
schema_test.go
479 lines (404 loc) · 15.1 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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
// Copyright 2022 Tigris Data, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package schema
import (
"fmt"
"reflect"
"testing"
"time"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestCollectionSchema(t *testing.T) {
// invalid
type empty struct {
}
// valid. implicit primary key
type noPK struct {
Key1 string `json:"key_1"`
}
type unknownTag struct {
Key1 string `tigris:"some"`
}
type unsupportedType struct {
Key1 string `json:"key_1" tigris:"primary_key"`
Unsupported chan struct{}
}
// valid. one part primary key with implicit index 1
type pk struct {
Key1 string `json:"key_1" tigris:"primary_key"`
}
// invalid. index starts from 1
type pk0 struct {
Key1 string `json:"key_1" tigris:"primary_key:0"`
}
// valid. optional index specified
type pk1 struct {
Key1 string `json:"key_1" tigris:"primary_key:1"`
}
// valid. composite primary key
type pk2 struct {
Key2 string `json:"key_2" tigris:"primary_key:2"`
Key1 string `json:"key_1" tigris:"primary_key:1"`
}
type pkDup struct {
Key2 string `json:"key_2" tigris:"primary_key:1"`
Key1 string `json:"key_1" tigris:"primary_key:1"`
}
// invalid. promary key index greater then number of fields
type pkOutOfBound struct {
Key3 string `json:"key_3" tigris:"primary_key:3"`
Key1 string `json:"key_1" tigris:"primary_key:1"`
}
// invalid. gap in primary key indexes
type pkGap struct {
Key3 string `json:"key_3" tigris:"primary_key:3"`
Key1 string `json:"key_1" tigris:"primary_key:1"`
Data1 string
}
type pkInvalidType struct {
Key2 bool `json:"key_2" tigris:"primary_key:1"`
}
type pkInvalidTag struct {
Key2 bool `json:"key_2" tigris:"primary_key:1:a"`
}
type pk3 struct {
Key2 string `json:"key_2" tigris:"primary_key:2"`
Key1 string `json:"key_1" tigris:"primary_key:1"`
Key3 string `json:"key_3" tigris:"primary_key:3"`
}
type subSubStructPK struct {
Data1 int `tigris:"-"`
Field1 string `json:"ss_field_1" tigris:"primary_key:1"`
}
type subStructPK struct {
Field1 string `json:"field_1"`
Nested subSubStructPK
}
type subSubStruct struct {
Data1 int `tigris:"-"`
Field1 string `json:"ss_field_1"`
}
type subStruct struct {
Field1 string `json:"field_1"`
Nested subSubStruct
}
type TestModel struct {
TestField int `json:"ID" tigris:"primary_key:1,autoGenerate"`
}
type testModelSub struct {
TestModel // nested embedded testModel
Tm TestModel // named field included as is
}
type embModel struct {
TestModel // treated as a metadata model
Field1 string `json:"ss_field_1"`
Tm TestModel // name field included as is
Sub testModelSub
}
// user can extend implicit primary key
type embModelPK struct {
TestModel
Field1 string `json:"ss_field_1" tigris:"primary_key:2"`
}
type autoGen struct {
Field1 string `tigris:"primaryKey,autoGenerate"`
Field2 int `tigris:"autoGenerate"`
Field3 int64 `tigris:"autoGenerate"`
Field4 uuid.UUID `tigris:"autoGenerate"`
Field5 time.Time `tigris:"autoGenerate"`
}
type autoGenNeg struct {
Field1 bool `json:"ss_field_1" tigris:"autoGenerate"`
}
// field with name ID is automatically considered as primary key
type autoID struct {
ID time.Time
}
type autoIDOverride struct {
RealID time.Time `tigris:"primaryKey,autoGenerate"`
ID time.Time
}
type autoIDBadType struct {
ID []bool
}
type allTypes struct {
Tm time.Time
TmPtr *time.Time
UUID uuid.UUID
UUIDPtr *uuid.UUID
Int32 int32 `json:"int_32"`
Int64 int64 `json:"int_64"`
Int int `json:"int_1"`
Bytes []byte `json:"bytes_1"`
BytesA [4]byte `json:"bytes_2"`
Float32 float32 `json:"float_32"`
Float64 float64 `json:"float_64"`
Bool bool `json:"bool_1"`
String string `json:"string_1" tigris:"primary_key:1"`
Data1 subStructPK `json:"data_1"`
Slice1 []string `json:"slice_1"`
Arr1 [3]string `json:"arr_1"`
Map map[string]string `json:"map_1"`
Slice2 []subStruct `json:"slice_2"`
Map2 map[string]subStruct `json:"map_2"`
Data2 subStructPK `json:"data_2" tigris:"primary_key:3,-"` // should be skipped
Bool123 bool `json:"bool_123"`
DataSkipped int `json:"-"`
PtrStruct *subSubStruct
// DataEnc int64 `tigris:"encrypted"`
// DataPII int64 `tigris:"pii"`
// unexported fields should not be in the schema
//nolint:unused
skipUnexported int
}
cases := []struct {
input interface{}
output *Schema
err error
}{
{empty{}, nil, fmt.Errorf("no data fields in the collection schema")},
{unknownTag{}, nil, fmt.Errorf("unknown tigris tag: some")},
{unsupportedType{}, nil, fmt.Errorf("unsupported type: name='' kind='chan'")},
{pk0{}, nil, fmt.Errorf("primary key index starts from 1")},
{pkOutOfBound{}, nil, fmt.Errorf("maximum primary key index is 2")},
{pkGap{}, nil, fmt.Errorf("gap in the primary key index")},
{pkInvalidType{}, nil, fmt.Errorf("type is not supported for the key: bool")},
{pkInvalidTag{}, nil, fmt.Errorf("only one colon allowed in the tag")},
{input: noPK{}, output: &Schema{Name: "no_pks", Fields: map[string]*Field{
"ID": {Type: typeString, Format: formatUUID, AutoGenerate: true},
"key_1": {Type: typeString}}, PrimaryKey: []string{"ID"}}},
{input: pk{}, output: &Schema{Name: "pks", Fields: map[string]*Field{
"key_1": {Type: typeString}}, PrimaryKey: []string{"key_1"}}},
{pk1{}, &Schema{Name: "pk_1", Fields: map[string]*Field{
"key_1": {Type: typeString}}, PrimaryKey: []string{"key_1"}}, nil},
{pk2{}, &Schema{Name: "pk_2", Fields: map[string]*Field{
"key_2": {Type: typeString}, "key_1": {Type: typeString}},
PrimaryKey: []string{"key_1", "key_2"}}, nil},
{pk3{}, &Schema{Name: "pk_3", Fields: map[string]*Field{
"key_2": {Type: typeString}, "key_1": {Type: typeString}, "key_3": {Type: typeString}},
PrimaryKey: []string{"key_1", "key_2", "key_3"}}, nil},
{allTypes{}, &Schema{Name: "all_types", Fields: map[string]*Field{
"Tm": {Type: typeString, Format: formatDateTime},
"TmPtr": {Type: typeString, Format: formatDateTime},
"UUID": {Type: typeString, Format: formatUUID},
"UUIDPtr": {Type: typeString, Format: formatUUID},
"int_32": {Type: typeInteger, Format: formatInt32},
"int_64": {Type: typeInteger},
// {Name: "uint_64", Type: typeInteger},
"int_1": {Type: typeInteger},
"bytes_1": {Type: typeString, Format: formatByte},
"bytes_2": {Type: typeString, Format: formatByte},
"float_32": {Type: typeNumber},
"float_64": {Type: typeNumber},
"bool_1": {Type: typeBoolean},
"string_1": {Type: typeString},
"data_1": {
Type: "object",
Fields: map[string]*Field{
"field_1": {Type: typeString},
"Nested": {
Type: "object",
Fields: map[string]*Field{
"ss_field_1": {Type: typeString},
},
},
},
},
"slice_1": {Type: typeArray, Items: &Field{Type: typeString}},
"arr_1": {Type: typeArray, Items: &Field{Type: typeString}},
"map_1": {Type: typeObject},
"slice_2": {Type: typeArray,
Items: &Field{
Type: "object",
Fields: map[string]*Field{
"field_1": {Type: typeString},
"Nested": {
Type: "object",
Fields: map[string]*Field{
"ss_field_1": {Type: typeString},
},
},
},
},
},
"map_2": {Type: typeObject},
// use original name if JSON tag name is not defined
"bool_123": {Type: typeBoolean},
"PtrStruct": {Type: typeObject, Fields: map[string]*Field{
"ss_field_1": {
Type: "string",
}}},
// {Name: "DataEnc", Type: typeInteger, Tags: []string{"encrypted"}},
// {Name: "DataPII", Type: typeInteger, Tags: []string{"pii"}},
}, PrimaryKey: []string{"string_1"}}, nil},
{embModel{}, &Schema{Name: "emb_models", Fields: map[string]*Field{
"Sub": {
Type: "object",
Fields: map[string]*Field{
"ID": {Type: typeInteger},
"Tm": {
Type: "object",
Fields: map[string]*Field{
"ID": {Type: typeInteger},
}},
}},
"ID": {Type: typeInteger, AutoGenerate: true},
"ss_field_1": {Type: typeString},
"Tm": {
Type: "object",
Fields: map[string]*Field{
"ID": {Type: typeInteger},
}},
},
PrimaryKey: []string{"ID"}}, nil},
{embModelPK{}, &Schema{Name: "emb_model_pks", Fields: map[string]*Field{
"ID": {Type: typeInteger, AutoGenerate: true},
"ss_field_1": {Type: typeString},
},
PrimaryKey: []string{"ID", "ss_field_1"}}, nil},
{autoGen{}, &Schema{Name: "auto_gens", Fields: map[string]*Field{
"Field1": {Type: typeString, AutoGenerate: true},
"Field2": {Type: typeInteger, AutoGenerate: true},
"Field3": {Type: typeInteger, AutoGenerate: true},
"Field4": {Type: typeString, Format: formatUUID, AutoGenerate: true},
"Field5": {Type: typeString, Format: formatDateTime, AutoGenerate: true},
},
PrimaryKey: []string{"Field1"}}, nil},
{autoGenNeg{}, nil, fmt.Errorf("type cannot be autogenerated: bool")},
{autoID{}, &Schema{Name: "auto_ids", Fields: map[string]*Field{
"ID": {Type: typeString, Format: formatDateTime, AutoGenerate: true},
},
PrimaryKey: []string{"ID"}}, nil},
{autoIDOverride{}, &Schema{Name: "auto_id_overrides", Fields: map[string]*Field{
"RealID": {Type: typeString, Format: formatDateTime, AutoGenerate: true},
"ID": {Type: typeString, Format: formatDateTime},
},
PrimaryKey: []string{"RealID"}}, nil},
{autoIDBadType{}, nil, fmt.Errorf("type is not supported for the key: array")},
}
for _, c := range cases {
t.Run(reflect.TypeOf(c.input).Name(), func(t *testing.T) {
schema, err := fromCollectionModel(c.input, Documents)
assert.Equal(t, c.err, err)
if schema != nil {
assert.Equal(t, Documents, schema.CollectionType)
c.output.CollectionType = Documents
}
assert.Equal(t, c.output, schema)
})
}
t.Run("build", func(t *testing.T) {
s, err := fromCollectionModel(allTypes{}, Documents)
require.NoError(t, err)
assert.Equal(t, Documents, s.CollectionType)
s.CollectionType = ""
b, err := s.Build()
require.NoError(t, err)
require.Equal(t, `{"title":"all_types","properties":{"PtrStruct":{"type":"object","properties":{"ss_field_1":{"type":"string"}}},"Tm":{"type":"string","format":"date-time"},"TmPtr":{"type":"string","format":"date-time"},"UUID":{"type":"string","format":"uuid"},"UUIDPtr":{"type":"string","format":"uuid"},"arr_1":{"type":"array","items":{"type":"string"}},"bool_1":{"type":"boolean"},"bool_123":{"type":"boolean"},"bytes_1":{"type":"string","format":"byte"},"bytes_2":{"type":"string","format":"byte"},"data_1":{"type":"object","properties":{"Nested":{"type":"object","properties":{"ss_field_1":{"type":"string"}}},"field_1":{"type":"string"}}},"float_32":{"type":"number"},"float_64":{"type":"number"},"int_1":{"type":"integer"},"int_32":{"type":"integer","format":"int32"},"int_64":{"type":"integer"},"map_1":{"type":"object"},"map_2":{"type":"object"},"slice_1":{"type":"array","items":{"type":"string"}},"slice_2":{"type":"array","items":{"type":"object","properties":{"Nested":{"type":"object","properties":{"ss_field_1":{"type":"string"}}},"field_1":{"type":"string"}}}},"string_1":{"type":"string"}},"primary_key":["string_1"]}`, string(b))
})
t.Run("multiple_models", func(t *testing.T) {
s, err := FromCollectionModels(Documents, pk{}, pk1{})
require.NoError(t, err)
assert.Equal(t, map[string]*Schema{
"pks": {Name: "pks", Fields: map[string]*Field{"key_1": {Type: typeString}}, PrimaryKey: []string{"key_1"}, CollectionType: Documents},
"pk_1": {Name: "pk_1", Fields: map[string]*Field{"key_1": {Type: typeString}}, PrimaryKey: []string{"key_1"}, CollectionType: Documents},
}, s)
})
t.Run("duplicate_pk_index", func(t *testing.T) {
_, err := FromCollectionModels(Documents, pkDup{})
if err.Error() == "duplicate primary key index 1 set for key_1 and key_2" {
require.Equal(t, err, fmt.Errorf("duplicate primary key index 1 set for key_1 and key_2"))
} else {
require.Equal(t, err, fmt.Errorf("duplicate primary key index 1 set for key_2 and key_1"))
}
})
}
func TestDatabaseSchema(t *testing.T) {
type Coll1 struct {
Key1 int64 `tigris:"primary_key"`
}
type Coll2 struct {
Key2 int64 `tigris:"primary_key"`
}
type Db1 struct {
c1 Coll1
c2 *Coll2
c3 []Coll2
C4 []*Coll2 `json:"coll_4"`
}
_ = Db1{c1: Coll1{}, c2: &Coll2{}, c3: []Coll2{}, C4: []*Coll2{}}
type Db3 struct {
Coll1
Coll2 `tigris:"-"`
}
type Db4 struct {
int64
}
_ = Db4{1}
coll1 := Schema{Name: "Coll1", Fields: map[string]*Field{"Key1": {Type: "integer"}}, PrimaryKey: []string{"Key1"}, CollectionType: Documents}
c1 := Schema{Name: "c1", Fields: map[string]*Field{"Key1": {Type: "integer"}}, PrimaryKey: []string{"Key1"}, CollectionType: Documents}
c2 := Schema{Name: "c2", Fields: map[string]*Field{"Key2": {Type: "integer"}}, PrimaryKey: []string{"Key2"}, CollectionType: Documents}
c3 := Schema{Name: "c3", Fields: map[string]*Field{"Key2": {Type: "integer"}}, PrimaryKey: []string{"Key2"}, CollectionType: Documents}
c4 := Schema{Name: "coll_4", Fields: map[string]*Field{"Key2": {Type: "integer"}}, PrimaryKey: []string{"Key2"}, CollectionType: Documents}
var i int64
cases := []struct {
input interface{}
name string
output map[string]*Schema
err error
}{
{Db1{}, "Db1", map[string]*Schema{"c1": &c1, "c2": &c2, "c3": &c3, "coll_4": &c4}, nil},
{&Db3{}, "Db3", map[string]*Schema{"Coll1": &coll1}, nil},
{Db4{}, "", nil, fmt.Errorf("model should be of struct type, not int64")},
{i, "", nil, fmt.Errorf("database model should be of struct type containing collection models types as fields")},
}
for _, c := range cases {
t.Run(reflect.TypeOf(c.input).Name(), func(t *testing.T) {
name, schema, err := FromDatabaseModel(c.input)
assert.Equal(t, c.name, name)
assert.Equal(t, c.err, err)
assert.Equal(t, c.output, schema)
})
}
}
func TestMessagesSchema(t *testing.T) {
type valid struct {
Key1 string `json:"key_1"`
}
type invalid struct {
Key1 string `json:"key_1" tigris:"primary_key:1"`
}
cases := []struct {
input interface{}
output *Schema
err error
}{
{valid{}, &Schema{Name: "valids", Fields: map[string]*Field{
"key_1": {Type: typeString}}, PrimaryKey: []string{}}, nil},
{invalid{}, nil, fmt.Errorf("primary key should not be defined for `messages` collection type")},
}
for _, c := range cases {
t.Run(reflect.TypeOf(c.input).Name(), func(t *testing.T) {
schema, err := fromCollectionModel(c.input, Messages)
assert.Equal(t, c.err, err)
if schema != nil {
assert.Equal(t, Messages, schema.CollectionType)
c.output.CollectionType = Messages
}
assert.Equal(t, c.output, schema)
})
}
}