-
Notifications
You must be signed in to change notification settings - Fork 0
/
cue_test.go
719 lines (673 loc) · 19.6 KB
/
cue_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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
package mpath
import (
"encoding/json"
"sort"
"testing"
)
func Benchmark_CueStringTableTests(b *testing.B) {
sort.Slice(cueTableTests, func(i, j int) bool {
return len(cueTableTests[i].name) > len(cueTableTests[j].name)
})
for _, test := range cueTableTests {
b.Run(test.name, func(b *testing.B) {
for n := 0; n < b.N; n++ {
b.ReportAllocs()
tc, err := CueValidate(test.mq, cueStringForTests, test.cp)
if err != nil && !test.expectErrors {
b.Errorf("test '%s'; got unexpected returned error: %v", test.name, err)
}
if tc != nil && tc.HasErrors() != test.expectErrors {
b.Errorf("test '%s'; expected %t got %t for HasErrors(); err was '%v'", test.name, test.expectErrors, tc.HasErrors(), tc.GetErrors())
}
b.SetBytes(int64(len(test.mq + cueStringForTests + test.cp)))
}
})
}
}
func Benchmark_BigCueFile(b *testing.B) {
cueFile := `"input": {
num: int,
_dependencies: []
}
"3dedbf75-1c91-4ec5-8018-99b1efe47462": {
result: int,
_dependencies: []
}
"52a015ef-6e51-407d-82e2-72fb218ae65b": {
result: {
#company: {
id: number
address?: {
name?: string
contact?: string
phone?: string
email?: string
addressLine1?: string
addressLine2?: string
addressLinesFriendly?: string
hasAddressLine2: bool
}
name: string
accountCode: string
isEnterprise: bool
}
consignments: [...{
id: number
consignmentNumber: string
carrierConsignmentId: string
dateCreated: string
insertedBy: number
service: {
name: string
abbreviation: string
serialisedSettings?: {...}
}
despatchDateTime: string
fromAddress: {
location: {
id: number
postcode: string
paddedPostcode: string
state: {
id: number
name: string
code: string
}
subLocality?: string
suburb: string
description: string
timeZone: string
country: {
id: number
name: string
code2: string
code3: string
numeric: string
isInternational: bool
}
}
name: string
contact?: string
phone?: string
email?: string
addressLine1: string
addressLine2?: string
carrierZone?: {
id: number
name: string
abbreviation: string
}
}
fromLocationCarrierZoneSettings?: {...}
toAddress: {
location: {
id: number
postcode: string
paddedPostcode: string
state: {
id: number
name: string
code: string
}
subLocality?: string
suburb: string
description: string
timeZone: string
country: {
id: number
name: string
code2: string
code3: string
numeric: string
isInternational: bool
}
}
name: string
contact?: string
phone?: string
email?: string
addressLine1: string
addressLine2?: string
carrierZone?: {
id: number
name: string
abbreviation: string
}
}
toLocationCarrierZoneSettings?: {...}
specialInstructions?: string
customerReference?: string
customerReference2?: string
totalWeight: number
totalCubic: number
totalVolume: number
validFromCompanyLocation: bool
validToCompanyLocation: bool
fromCompanyLocationId?: number
toCompanyLocationId?: number
fromCompanyLocationAbbreviation?: string
toCompanyLocationAbbreviation?: string
receiverCode?: string
isReceiverPays: bool
isDgConsignment: bool
dgsDeclaration: bool
dgsDeclarationDateTimeUtc?: string
dgsDeclarationUserId?: number
emergencyContactNumber?: string
surcharges?: [...{
name: string
abbreviation: string
quantity: number
}]
etaLocal: string
primaryReferenceType: number
primaryReference: string
carrierReference?: string
totalItemCount: number
items: [...{
id: number
carrierItemType?: string
itemType: string
name: string
sku?: string
height: number
weight: number
length: number
width: number
quantity: number
palletSpaces?: number
volume: number
isDgItem: bool
integrationItemDgItems?: [...{
id: number
companyDgItemId?: number
unNumber: number
packingGroup: number
packingGroupStringValue: string
containerType: number
containerTypeStringValue: string
aggregateQuantity: number
isAggregateQuantityWeight: bool
numberOfContainers: number
dgClassType: number
dgClassTypeStringValue: string
dgClassTypeStringDescription: string
subDgClassTypes?: [...number]
subDgClassTypesSeparator?: string
subDgClassTypesStringValue?: string
hasSubRisk: bool
properShippingNameValue: string
properShippingName: string
technicalOrChemicalGroupNames?: string
hasTechnicalName: bool
isMarinePollutant: bool
isTemperatureControlled: bool
isEmptyDgContainer: bool
quantityPerContainer: number
totalAggregateQuantity: number
totalNumberOfContainers: number
dgClassDisplay: string
}
]
integrationItemReferences?: [...{
id: number
carrierItemReference: string
itemNumber: number
carrierReference?: string
}
]
integrationItemContents?: [...{
id: number
description: string
reference1?: string
reference2?: string
reference3?: string
quantity: number
dollarValue?: number
ciMarksAndNumbers?: string
harmonizedCode?: string
partNumber?: string
purpose?: string
countryOfManufactureId?: string
countryOfManufacture?: {
id: number
name: string
code2: string
code3: string
numeric: string
}
}
]
}
]
internationalFromCity?: string
internationalFromPostcode?: string
internationalFromProvince?: string
fromCountry?: string
internationalToCity?: string
internationalToPostcode?: string
internationalToProvince?: string
toCountry?: string
isInternational: bool
}
]
pickup: {
pickupDateTime: string
palletSpaces?: number
pickupBooked: bool
pickupClosingTime?: string
pickupSpecialInstructions?: string
pickupRequired: bool
permanentPickup: bool
totalWeightKgs: number
}
consigningCompany: #company
accountHolderCompany: #company
carrier: {
id: number
name: string
code: string
serialisedSettings?: {...}
}
account: {
name: string
accountCode?: string
abbreviation: string
serialisedSettings?: {...}
}
bookingType?: string
},
_dependencies: ["3dedbf75-1c91-4ec5-8018-99b1efe47462"]
}
"bd33058f-d866-4800-aa97-098c0137e8c0": {
result: string
_dependencies: ["52a015ef-6e51-407d-82e2-72fb218ae65b"]
}`
testName := "Big Cue"
b.Run(testName, func(b *testing.B) {
for n := 0; n < b.N; n++ {
b.ReportAllocs()
tc, err := CueValidate(`$.52a015ef-6e51-407d-82e2-72fb218ae65b.result.consignments`, cueFile, `bd33058f-d866-4800-aa97-098c0137e8c0`)
if err != nil {
b.Errorf("test '%s'; got unexpected returned error: %v", testName, err)
}
if tc != nil && tc.HasErrors() != false {
b.Errorf("test '%s'; expected %t got %t for HasErrors(); err was '%v'", testName, false, tc.HasErrors(), tc.GetErrors())
}
b.SetBytes(int64(len(`$.52a015ef-6e51-407d-82e2-72fb218ae65b.result.consignments` + cueFile + `bd33058f-d866-4800-aa97-098c0137e8c0`)))
}
})
}
func Test_CueStringTableTests(t *testing.T) {
t.Parallel()
var onlyRunTest string
var copyAndLog bool
// onlyRunTest = "can use AsJSON on array"
// copyAndLog = true
for _, test := range cueTableTests {
if onlyRunTest != "" && test.name != onlyRunTest {
continue
}
tc, err := CueValidate(test.mq, cueStringForTests, test.cp)
if err != nil && !test.expectErrors {
t.Errorf("test '%s'; got unexpected returned error: %v", test.name, err)
}
if tc != nil && tc.HasErrors() != test.expectErrors {
t.Errorf("test '%s'; expected %t got %t for HasErrors(); err was '%v'", test.name, test.expectErrors, tc.HasErrors(), tc.GetErrors())
}
if copyAndLog {
tcb, _ := json.MarshalIndent(tc, "", " ")
// clipboard.WriteAll(string(tcb))
t.Log(string(tcb))
}
}
}
type tableTest struct {
name string
mq string
expectErrors bool
cp string
}
var (
cueTableTests = []tableTest{
{
name: "check that array without ... can be assessed",
mq: `$.step1.result[@.age.Equal($.step1.num)].First()`,
cp: "step2",
},
{
name: "check that array with ... can be assessed",
mq: `$.step2.result[@.age.Equal($.step2.num)].First()`,
cp: "step3",
},
{
name: "try to access step that is not available",
mq: `$.step2.result[@.age.Equal($.step2.num)].First()`,
cp: "step2",
expectErrors: true,
},
{
name: "test that optional value in optional step can be referenced",
mq: `$.stepOptional.result.name`,
cp: "step3",
},
{
name: "test logical op at root of query",
mq: `{OR,$.step1.num.Greater(0),$.step2.num.Less(0)}`,
cp: "step3",
},
{
name: "root only",
mq: "$",
cp: "87e54fe3-6e64-454e-acf7-1a36801f1b87",
},
{
name: "input only",
mq: "$.input.name",
cp: "input",
},
{
name: "object after ParseJSON",
mq: `$.step4.result.ParseJSON().object`,
cp: "step5",
},
{
name: "empty string",
mq: "",
cp: "input",
expectErrors: true,
},
{
name: "new line",
mq: "\n",
cp: "input",
expectErrors: true,
},
{
name: "incomplete ident",
mq: "$.step1.result[@.na]",
cp: "step2",
expectErrors: true,
},
{
name: "complex can be filtered",
mq: `{OR,$.step7.results[AND,@.example.AnyOf("Test","Something"),@.example.NotEqual("Another")].First().array[OR,@.object.nested.boolean].First().object.nested.boolean,$.step6.result.Equal($.input.num.Multiply(12))}`,
cp: "step8",
},
{
name: "test get error for incomplete",
mq: `$.52a015ef-6e51-407d-82e2-72fb218ae65b.results[AND,@.ex]`,
cp: "bd33058f-d866-4800-aa97-098c0137e8c0",
expectErrors: true,
},
{
name: "retrieve any value",
mq: `$.step9.result`,
cp: "step10",
},
{
name: "retrieve key from any value",
mq: `$.step9.result.subkey.subkey`,
cp: "step10",
},
{
name: "retrieve any value in slice",
mq: `$.step10.result.sl.First().value`,
cp: "step11",
},
{
name: "retrieve key from any value in slice",
mq: `$.step10.result.sl.First().value.subkey.subkey`,
cp: "step11",
},
{
name: "retrieve bytes from result",
mq: `$.step11.result`,
cp: "step12",
},
{
name: "check that optional fields return",
mq: `$.step2.result.First()`,
cp: "stepOptional",
},
{
name: "can get into field afer 'First()' function call",
mq: `$.step2.result.First().age`,
cp: "stepOptional",
},
{
name: "can use AsJSON on array",
mq: `$.step1.result.AsJSON()`,
cp: "step2",
},
}
)
const (
cueStringForTests = `
step1: {
num: int
_dependencies: []
result: [{
name: string
age: int
}]
}
step2: {
num: int
_dependencies: ["step1"]
result: [...{
name?: string
age!: int
}]
}
stepOptional?: {
result?: {
name: int
}
_dependencies: ["step2"]
}
step3 : {
_dependencies: ["stepOptional","step2"]
}
def: {
result: float
_dependencies: ["abc"]
}
input: {
_dependencies: []
name: string
num: int
...
}
step4: {
_dependencies: []
result: string
}
step5: {
_dependencies: ["step4"]
result: string
}
"0f2fbb3e-c95a-4760-8bad-ae5610c28454": {
_dependencies: ["87e54fe3-6e64-454e-acf7-1a36801f1b87"]
...
}
"4ebec022-4119-4cc8-bb7f-7a713790305c": {
result: string
"_errored": bool
_dependencies: ["f17e90d1-d3f7-4f55-a50f-4323206c7fd1"]
"_error"?: {
message: string
}
}
"87e54fe3-6e64-454e-acf7-1a36801f1b87": {
result: _
"_errored": bool
_dependencies: ["4ebec022-4119-4cc8-bb7f-7a713790305c"]
"_error"?: {
message: string
}
}
"97abc773-2f79-4d77-9859-2e78f38161d7": {
_dependencies: ["0f2fbb3e-c95a-4760-8bad-ae5610c28454"]
...
}
"f17e90d1-d3f7-4f55-a50f-4323206c7fd1": {
result: float
"_errored": bool
_dependencies: []
"_error"?: {
message: string
}
}
"step6": {
result: int,
_dependencies: []
}
"step7": {
results: [{
example: string
array: [{
object: {
nested: {
boolean: bool
}
}
}]
}],
_dependencies: ["step6"]
}
"step8": {
result: string
_dependencies: ["step7"]
}
"3dedbf75-1c91-4ec5-8018-99b1efe47462": {
result: int,
_dependencies: []
}
"52a015ef-6e51-407d-82e2-72fb218ae65b": {
results: [{
example: string
array: [{
object: {
nested: {
boolean: bool
}
}
}]
}],
_dependencies: ["3dedbf75-1c91-4ec5-8018-99b1efe47462"]
}
"bd33058f-d866-4800-aa97-098c0137e8c0": {
result: string
_dependencies: ["52a015ef-6e51-407d-82e2-72fb218ae65b"]
}
"step9": {
result: _
_dependencies: ["step7"]
}
"step10": {
_dependencies: ["step9"]
result: {
sl: [{
value: _
}]
}
}
"step11": {
_dependencies: ["step10"]
result: bytes
}
"step12": {
_dependencies: ["step11"]
result: _
}
variables: {
test: string
_dependencies: []
}
`
)
func Test_CueStringNoCurrentPath(t *testing.T) {
t.Parallel()
type tableTest struct {
name string
mq string
expectErrors bool
cp string
}
test := tableTest{
name: "get root objects",
mq: `$.c`,
cp: ``,
expectErrors: false,
}
cueString := `
"a": string
"b": int
"c": {
"x": string
}
`
tc, err := CueValidate(test.mq, cueString, test.cp)
if err != nil && !test.expectErrors {
t.Errorf("test '%s'; got unexpected returned error: %v", test.name, err)
}
if tc != nil && tc.HasErrors() != test.expectErrors {
t.Errorf("test '%s'; expected %t got %t for HasErrors(); err was '%v'", test.name, test.expectErrors, tc.HasErrors(), tc.GetErrors())
}
tcb, _ := json.MarshalIndent(tc, "", " ")
// clipboard.WriteAll(string(tcb))
t.Log(string(tcb))
}
func Test_CueStringManual(t *testing.T) {
t.Parallel()
type tableTest struct {
name string
mq string
expectErrors bool
cp string
}
test := tableTest{
name: "manual test",
mq: `$.52a015ef-6e51-407d-82e2-72fb218ae65b.results[AND,@.ex]`,
cp: `bd33058f-d866-4800-aa97-098c0137e8c0`,
expectErrors: true,
}
cueString := `
"input": {
num: int,
_dependencies: []
}
"3dedbf75-1c91-4ec5-8018-99b1efe47462": {
result: int,
_dependencies: []
}
"52a015ef-6e51-407d-82e2-72fb218ae65b": {
results: [{
example: string
array: [{
object: {
nested: {
boolean: bool
}
}
}]
}],
_dependencies: ["3dedbf75-1c91-4ec5-8018-99b1efe47462"]
}
"bd33058f-d866-4800-aa97-098c0137e8c0": {
result: string
_dependencies: ["52a015ef-6e51-407d-82e2-72fb218ae65b"]
}
`
tc, err := CueValidate(test.mq, cueString, test.cp)
if err != nil && !test.expectErrors {
t.Errorf("test '%s'; got unexpected returned error: %v", test.name, err)
}
if tc != nil && tc.HasErrors() != test.expectErrors {
t.Errorf("test '%s'; expected %t got %t for HasErrors(); err was '%v'", test.name, test.expectErrors, tc.HasErrors(), tc.GetErrors())
}
tcb, _ := json.MarshalIndent(tc, "", " ")
// clipboard.WriteAll(string(tcb))
t.Log(string(tcb))
}