This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from datacontract/datacontract-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema_test.go
443 lines (417 loc) · 10.8 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
package datacontract
import (
"fmt"
"reflect"
"testing"
)
func TestExtractSchemaSpecification(t *testing.T) {
specificationString := `version: 2
models:
- name: my_table
description: "contains data"
config:
materialized: table
columns:
- name: my_column
data_type: text
description: "contains values"`
specificationMap := map[string]any{
"version": 2,
"models": []map[string]any{
{
"name": "my_table",
"description": "contains data",
"config": map[string]any{
"materialized": "table",
},
"columns": []map[string]any{
{
"name": "my_column",
"data_type": "text",
"description": "contains values",
},
},
},
},
}
type args struct {
contract DataContract
}
tests := []struct {
name string
args args
wantSchemaType string
wantSpecification any
wantErr bool
}{
{
name: "map",
args: args{contract: DataContract{"schema": map[string]any{
"type": "dbt",
"specification": specificationMap,
}}},
wantSchemaType: "dbt",
wantSpecification: specificationMap,
wantErr: false,
},
{
name: "string",
args: args{contract: DataContract{"schema": map[string]any{
"type": "dbt",
"specification": specificationString,
}}},
wantSchemaType: "dbt",
wantSpecification: specificationString,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotSchemaType, gotSpecification, err := extractSchemaSpecification(tt.args.contract, []string{"schema", "type"}, []string{"schema", "specification"})
if (err != nil) != tt.wantErr {
t.Errorf("extractSchemaSpecification() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotSchemaType != tt.wantSchemaType {
t.Errorf("extractSchemaSpecification() got schemaType = %v, want %v", gotSchemaType, tt.wantSchemaType)
}
var equal bool
if _, ok := gotSpecification.(map[string]any); ok {
equal = reflect.DeepEqual(gotSpecification, tt.wantSpecification)
} else {
equal = gotSpecification == tt.wantSpecification
}
if !equal {
t.Errorf("extractSchemaSpecification() got specification = %v, want %v", gotSpecification, tt.wantSpecification)
}
})
}
}
func TestParseSchema(t *testing.T) {
modelName := "email_provider_usage"
modelType := "table"
modelDescription := "Description of the model"
otherModelName := "other_model_name"
otherModelType := "other_model_type"
otherModelDescription := "other_model_description"
fieldName := "email_provider"
fieldType := "text"
fieldDescription := "Description of the column"
otherFieldName := "other"
otherFieldType := "timestamp"
otherFieldDescription := "other field"
type args struct {
schemaType string
specification []byte
}
tests := []struct {
name string
args args
want InternalModelSpecification
}{
{
name: "unkown",
args: args{"unkown", []byte{}},
},
{
name: "dbt",
args: args{"dbt", []byte(fmt.Sprintf(`version: 2
models:
- name: %v
description: "%v"
config:
materialized: %v
columns:
- name: %v
data_type: %v
description: "%v"`, modelName, modelDescription, modelType, fieldName, fieldType, fieldDescription))},
want: InternalModelSpecification{Models: []InternalModel{
{
Name: modelName,
Type: &modelType,
Description: &modelDescription,
Fields: []InternalField{
{
Name: fieldName,
Type: &fieldType,
Description: &fieldDescription,
},
},
},
}},
},
{
name: "dbt-model-level-constraints",
args: args{"dbt", []byte(fmt.Sprintf(`version: 2
models:
- name: %v
description: "%v"
config:
materialized: %v
constraints:
- type: not_null
columns: [%v]
- type: unique
columns: [%v]
- type: check
expression: "id > 0"
columns: [%v]
columns:
- name: %v
data_type: %v
description: "%v"`, modelName, modelDescription, modelType, fieldName, fieldName, fieldName, fieldName, fieldType, fieldDescription))},
want: InternalModelSpecification{Models: []InternalModel{
{
Name: modelName,
Type: &modelType,
Description: &modelDescription,
Fields: []InternalField{
{
Name: fieldName,
Type: &fieldType,
Description: &fieldDescription,
Required: true,
Unique: true,
AdditionalConstraints: []InternalFieldConstraint{{Type: "check", Expression: "id > 0"}},
},
},
},
}},
},
{
name: "dbt-column-level-constraints",
args: args{"dbt", []byte(fmt.Sprintf(`version: 2
models:
- name: %v
description: "%v"
config:
materialized: %v
columns:
- name: %v
data_type: %v
description: "%v"
constraints:
- type: not_null
- type: unique
- type: check
expression: "id > 0"
`, modelName, modelDescription, modelType, fieldName, fieldType, fieldDescription))},
want: InternalModelSpecification{Models: []InternalModel{
{
Name: modelName,
Type: &modelType,
Description: &modelDescription,
Fields: []InternalField{
{
Name: fieldName,
Type: &fieldType,
Description: &fieldDescription,
Required: true,
Unique: true,
AdditionalConstraints: []InternalFieldConstraint{{Type: "check", Expression: "id > 0"}},
},
},
},
}},
},
{
name: "dbt-two-models",
args: args{"dbt", []byte(fmt.Sprintf(`version: 2
models:
- name: %v
description: "%v"
config:
materialized: %v
- name: %v
description: "%v"
config:
materialized: %v
`, modelName, modelDescription, modelType, otherModelName, otherModelDescription, otherModelType))},
want: InternalModelSpecification{Models: []InternalModel{
{
Name: modelName,
Type: &modelType,
Description: &modelDescription,
Fields: []InternalField{},
},
{
Name: otherModelName,
Type: &otherModelType,
Description: &otherModelDescription,
Fields: []InternalField{},
},
}},
},
{
name: "dbt-two-columns",
args: args{"dbt", []byte(fmt.Sprintf(`version: 2
models:
- name: %v
description: "%v"
config:
materialized: %v
columns:
- name: %v
data_type: %v
description: %v
- name: %v
data_type: %v
description: %v
`, modelName, modelDescription, modelType, fieldName, fieldType, fieldDescription, otherFieldName, otherFieldType, otherFieldDescription))},
want: InternalModelSpecification{Models: []InternalModel{
{
Name: modelName,
Type: &modelType,
Description: &modelDescription,
Fields: []InternalField{
{
Name: fieldName,
Type: &fieldType,
Description: &fieldDescription,
},
{
Name: otherFieldName,
Type: &otherFieldType,
Description: &otherFieldDescription,
},
},
},
}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := ParseSchema(tt.args.schemaType, tt.args.specification)
if !got.equals(tt.want) {
t.Errorf("ParseSchema() got = %v, want %v", got, tt.want)
}
})
}
}
func (specification InternalModelSpecification) equals(other InternalModelSpecification) bool {
if len(specification.Models) != len(other.Models) {
return false
}
for i, model := range specification.Models {
if !model.equals(other.Models[i]) {
return false
}
}
return true
}
func (model InternalModel) equals(other InternalModel) bool {
return fieldsAreEqual(model.Fields, other.Fields) &&
model.Name == other.Name &&
*model.Type == *other.Type &&
*model.Description == *other.Description
}
func (field InternalField) equals(other InternalField) bool {
return fieldsAreEqual(field.Fields, other.Fields) &&
field.Name == other.Name &&
*field.Type == *other.Type &&
*field.Description == *other.Description &&
field.Required == other.Required &&
field.Unique == other.Unique &&
fieldConstraintsAreEqual(field.AdditionalConstraints, other.AdditionalConstraints)
}
func fieldsAreEqual(fields []InternalField, otherFields []InternalField) bool {
if len(fields) != len(otherFields) {
return false
}
for i, field := range fields {
if !field.equals(otherFields[i]) {
return false
}
}
return true
}
func fieldConstraintsAreEqual(constraints, other []InternalFieldConstraint) bool {
for _, constraint := range constraints {
if !constraint.isIn(other) {
return false
}
}
for _, constraint := range other {
if !constraint.isIn(constraints) {
return false
}
}
return true
}
func TestCreateSchema(t *testing.T) {
modelType := "table"
modelDescription := "contains fields"
fieldType := "string"
fieldDescription := "contains values"
type args struct {
schemaType string
specification InternalModelSpecification
}
tests := []struct {
name string
args args
want []byte
wantErr bool
}{
{
name: "dbt",
args: args{
schemaType: "dbt",
specification: InternalModelSpecification{
Type: "dbt",
Models: []InternalModel{{
Name: "my_model",
Type: &modelType,
Description: &modelDescription,
Fields: []InternalField{{
Name: "my_field",
Type: &fieldType,
Description: &fieldDescription,
Required: true,
Unique: true,
AdditionalConstraints: []InternalFieldConstraint{{
Type: "check",
Expression: "1 == 1",
}},
Fields: nil,
}},
}},
},
},
wantErr: false,
want: []byte(`models:
- name: my_model
description: contains fields
config:
materialized: table
constraints: []
columns:
- name: my_field
description: contains values
data_type: contains values
constraints:
- type: not_null
expression: ""
columns: []
- type: unique
expression: ""
columns: []
- type: check
expression: 1 == 1
columns: []
`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreateSchema(tt.args.schemaType, tt.args.specification)
if (err != nil) != tt.wantErr {
t.Errorf("CreateSchema() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreateSchema() got = %v, want %v", string(got), string(tt.want))
}
})
}
}