forked from geldata/gel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_edgeql_insert.py
933 lines (829 loc) · 25.5 KB
/
test_edgeql_insert.py
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
##
# Copyright (c) 2016-present MagicStack Inc.
# All rights reserved.
#
# See LICENSE for details.
##
import os.path
import unittest # NOQA
import uuid
from edgedb.client import exceptions as exc
from edgedb.server import _testbase as tb
class TestInsert(tb.QueryTestCase):
'''The scope of the tests is testing various modes of Object creation.'''
SCHEMA = os.path.join(os.path.dirname(__file__), 'schemas',
'insert.eschema')
async def test_edgeql_insert_fail_1(self):
err = 'missing value for required pointer ' + \
'{test::InsertTest}.{test::l2}'
with self.assertRaisesRegex(exc.MissingRequiredPointerError, err):
await self.con.execute('''
INSERT test::InsertTest;
''')
async def test_edgeql_insert_simple_01(self):
result = await self.con.execute(r"""
INSERT test::InsertTest {
name := 'insert simple 01',
l2 := 0,
};
INSERT test::InsertTest {
name := 'insert simple 01',
l3 := "Test\"1\"",
l2 := 1
};
INSERT test::InsertTest {
name := 'insert simple 01',
l3 := 'Test\'2\'',
l2 := 2
};
INSERT test::InsertTest {
name := 'insert simple 01',
l3 := '\"Test\'3\'\"',
l2 := 3
};
SELECT
test::InsertTest {
l2, l3
}
FILTER
test::InsertTest.name = 'insert simple 01'
ORDER BY
test::InsertTest.l2;
""")
self.assert_data_shape(result, [
[1],
[1],
[1],
[1],
[{
'id': uuid.UUID,
'l2': 0,
'l3': 'test',
}, {
'id': uuid.UUID,
'l2': 1,
'l3': 'Test"1"',
}, {
'id': uuid.UUID,
'l2': 2,
'l3': "Test'2'",
}, {
'id': uuid.UUID,
'l2': 3,
'l3': '''"Test'3'"''',
}]
])
async def test_edgeql_insert_simple_02(self):
res = await self.con.execute('''
WITH MODULE test
INSERT DefaultTest1 { foo := '02' };
INSERT test::DefaultTest1 { foo := '02' };
INSERT test::DefaultTest1 { foo := '02' };
WITH MODULE test
SELECT DefaultTest1 { num } FILTER DefaultTest1.foo = '02';
''')
self.assert_data_shape(
res[-1],
[{'num': 42}, {'num': 42}, {'num': 42}],
)
async def test_edgeql_insert_simple_03(self):
res = await self.con.execute('''
INSERT test::DefaultTest1 { num := 100 };
WITH MODULE test
INSERT DefaultTest2;
INSERT test::DefaultTest1 { num := 101 };
INSERT test::DefaultTest2;
INSERT test::DefaultTest1 { num := 102 };
INSERT test::DefaultTest2;
WITH MODULE test
SELECT DefaultTest2 { num }
ORDER BY DefaultTest2.num;
''')
self.assert_data_shape(
res[-1],
[{'num': 101}, {'num': 102}, {'num': 103}],
)
async def test_edgeql_insert_nested_01(self):
res = await self.con.execute('''
INSERT test::Subordinate {
name := 'subtest 1'
};
INSERT test::Subordinate {
name := 'subtest 2'
};
INSERT test::InsertTest {
name := 'insert nested',
l2 := 0,
subordinates := (
SELECT test::Subordinate
FILTER test::Subordinate.name LIKE 'subtest%'
)
};
SELECT test::InsertTest {
subordinates: {
name,
@comment,
} ORDER BY test::InsertTest.subordinates.name
}
FILTER
test::InsertTest.name = 'insert nested';
''')
self.assert_data_shape(
res[-1],
[{
'id': uuid.UUID,
'subordinates': [{
'id': uuid.UUID,
'name': 'subtest 1',
'@comment': None,
}, {
'id': uuid.UUID,
'name': 'subtest 2',
'@comment': None,
}]
}]
)
async def test_edgeql_insert_nested_02(self):
res = await self.con.execute('''
WITH MODULE test
INSERT Subordinate {
name := 'subtest 3'
};
WITH MODULE test
INSERT Subordinate {
name := 'subtest 4'
};
WITH MODULE test
INSERT InsertTest {
name := 'insert nested 2',
l2 := 0,
subordinates := (
SELECT Subordinate {
@comment := (SELECT 'comment ' + Subordinate.name)
}
FILTER Subordinate.name IN {'subtest 3', 'subtest 4'}
)
};
WITH MODULE test
SELECT InsertTest {
subordinates: {
name,
@comment,
} ORDER BY InsertTest.subordinates.name
}
FILTER
InsertTest.name = 'insert nested 2';
''')
self.assert_data_shape(
res[-1],
[{
'id': uuid.UUID,
'subordinates': [{
'id': uuid.UUID,
'name': 'subtest 3',
'@comment': 'comment subtest 3',
}, {
'id': uuid.UUID,
'name': 'subtest 4',
'@comment': 'comment subtest 4',
}]
}]
)
async def test_edgeql_insert_nested_03(self):
res = await self.con.execute('''
WITH MODULE test
INSERT InsertTest {
name := 'insert nested 3',
l2 := 0,
subordinates: Subordinate {
name := 'nested sub 3.1'
}
};
WITH MODULE test
SELECT InsertTest {
subordinates: {
name
} ORDER BY InsertTest.subordinates.name
}
FILTER
InsertTest.name = 'insert nested 3';
''')
self.assert_data_shape(
res[-1],
[{
'id': uuid.UUID,
'subordinates': [{
'id': uuid.UUID,
'name': 'nested sub 3.1'
}]
}]
)
async def test_edgeql_insert_nested_04(self):
res = await self.con.execute('''
WITH MODULE test
INSERT InsertTest {
name := 'insert nested 4',
l2 := 0,
subordinates: Subordinate {
name := 'nested sub 4.1',
@comment := 'comment 4.1',
}
};
WITH MODULE test
SELECT InsertTest {
subordinates: {
name,
@comment,
} ORDER BY InsertTest.subordinates.name
}
FILTER
InsertTest.name = 'insert nested 4';
''')
self.assert_data_shape(
res[-1],
[{
'id': uuid.UUID,
'subordinates': [{
'id': uuid.UUID,
'name': 'nested sub 4.1',
'@comment': 'comment 4.1'
}]
}]
)
async def test_edgeql_insert_nested_05(self):
res = await self.con.execute('''
INSERT test::Subordinate {
name := 'only subordinate'
};
INSERT test::Subordinate {
name := 'never subordinate'
};
WITH MODULE test
INSERT InsertTest {
name := 'insert nested 5',
l2 := 0,
subordinates := (
SELECT Subordinate
FILTER Subordinate.name = 'only subordinate'
)
};
WITH MODULE test
SELECT InsertTest {
name,
l2,
subordinates: {
name
}
} FILTER InsertTest.name = 'insert nested 5';
''')
self.assert_data_shape(
res[-1],
[{
'name': 'insert nested 5',
'l2': 0,
'subordinates': [{
'name': 'only subordinate'
}]
}],
)
async def test_edgeql_insert_returning_01(self):
res = await self.con.execute('''
WITH MODULE test
INSERT DefaultTest1 {
foo := 'ret1',
num := 1,
};
WITH MODULE test
SELECT (INSERT DefaultTest1 {
foo := 'ret2',
num := 2,
}) {foo};
WITH MODULE test
SELECT (INSERT DefaultTest1 {
foo := 'ret3',
num := 3,
}).num;
''')
self.assert_data_shape(
res,
[
[1],
[{
'foo': 'ret2',
}],
[3],
]
)
async def test_edgeql_insert_returning_02(self):
res = await self.con.execute('''
WITH MODULE test
SELECT (INSERT DefaultTest1 {
foo := 'ret1',
num := 1,
});
WITH MODULE test
SELECT (INSERT DefaultTest1 {
foo := 'ret2',
num := 2,
}) {foo};
WITH MODULE test
SELECT (INSERT DefaultTest1 {
foo := 'ret3',
num := 3,
}).num;
''')
self.assert_data_shape(
res,
[
[{
'id': uuid.UUID,
}],
[{
'foo': 'ret2',
}],
[3],
]
)
async def test_edgeql_insert_returning_03(self):
res = await self.con.execute('''
INSERT test::Subordinate {
name := 'sub returning 3'
};
WITH
MODULE test,
I := (INSERT InsertTest {
name := 'insert nested returning 3',
l2 := 0,
subordinates := (
SELECT Subordinate
FILTER Subordinate.name = 'sub returning 3'
)
})
SELECT I {
name,
l2,
subordinates: {
name
}
};
''')
self.assert_data_shape(
res[-1],
[{
'name': 'insert nested returning 3',
'l2': 0,
'subordinates': [{
'name': 'sub returning 3'
}]
}],
)
async def test_edgeql_insert_returning_04(self):
await self.assert_query_result(r'''
WITH MODULE test
SELECT (INSERT DefaultTest1 {
foo := 'DT returning 5',
num := 33,
}) {foo, num};
WITH
MODULE test,
I := (INSERT _ := InsertTest {
name := 'IT returning 5',
l2 := 9999,
})
SELECT
DefaultTest1 {foo, num}
FILTER DefaultTest1.num > I.l2;
WITH
MODULE test,
I := (INSERT _ := InsertTest {
name := 'IT returning 5',
l2 := 9,
})
SELECT
DefaultTest1 {foo, num}
FILTER DefaultTest1.num > I.l2;
''', [
[{
'foo': 'DT returning 5',
'num': 33,
}],
[],
[{
'foo': 'DT returning 5',
'num': 33,
}],
])
async def test_edgeql_insert_for_01(self):
res = await self.con.execute(r'''
WITH MODULE test
FOR x IN {3, 5, 7, 2}
UNION (INSERT InsertTest {
name := 'insert for 1',
l2 := x,
});
WITH MODULE test
FOR Q IN {(SELECT InsertTest{foo := 'foo' + <str> InsertTest.l2}
FILTER .name = 'insert for 1')}
UNION (INSERT InsertTest {
name := 'insert for 1',
l2 := 35 % Q.l2,
l3 := Q.foo,
});
WITH MODULE test
SELECT InsertTest{name, l2, l3}
FILTER .name = 'insert for 1'
ORDER BY .l2 THEN .l3;
''')
self.assert_data_shape(
res[-1], [
# insertion based on existing data
{
'name': 'insert for 1',
'l2': 0,
'l3': 'foo5',
},
{
'name': 'insert for 1',
'l2': 0,
'l3': 'foo7',
},
{
'name': 'insert for 1',
'l2': 1,
'l3': 'foo2',
},
{
'name': 'insert for 1',
'l2': 2,
'l3': 'foo3',
},
# inserted based on static data
{
'name': 'insert for 1',
'l2': 2,
'l3': 'test',
},
{
'name': 'insert for 1',
'l2': 3,
'l3': 'test',
},
{
'name': 'insert for 1',
'l2': 5,
'l3': 'test',
},
{
'name': 'insert for 1',
'l2': 7,
'l3': 'test',
},
]
)
async def test_edgeql_insert_for_02(self):
res = await self.con.execute(r'''
# create 1000 DefaultTest3 objects, each object is defined
# as having a randomly generated value for 'foo'
WITH MODULE test
FOR x IN {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
UNION (INSERT DefaultTest3);
# statistically, randomly generated value for 'foo' should not be
# identical for all 10 records
WITH
MODULE test,
DT3 := DETACHED DefaultTest3
SELECT count(
DefaultTest3 FILTER DefaultTest3.foo != DT3.foo) > 0;
''')
self.assert_data_shape(
res[-1], [True]
)
async def test_edgeql_insert_for_03(self):
res = await self.con.execute(r'''
# Create 5 DefaultTest4 objects. The default value for
# 'bar' is technically evaluated for each object, but
# because it is deterministic it will be same for all 5
# new objects.
WITH MODULE test
FOR x IN {1, 2, 3, 4, 5}
UNION (INSERT DefaultTest4);
WITH MODULE test
SELECT DefaultTest4.bar
ORDER BY DefaultTest4.bar;
''')
self.assert_data_shape(
res[-1], [0, 0, 0, 0, 0]
)
async def test_edgeql_insert_default_01(self):
res = await self.con.execute(r'''
# create 10 DefaultTest3 objects, each object is defined
# as having a randomly generated value for 'foo'
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
INSERT test::DefaultTest3;
# statistically, randomly generated value for 'foo' should not be
# identical for all 10 records
WITH
MODULE test,
DT3 := DETACHED DefaultTest3
SELECT count(
DefaultTest3 FILTER DefaultTest3.foo != DT3.foo) > 0;
''')
self.assert_data_shape(
res[-1], [True]
)
async def test_edgeql_insert_default_02(self):
res = await self.con.execute(r'''
# by default the 'bar' value is simply going to be "indexing" the
# created objects
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
WITH MODULE test
SELECT DefaultTest4 { bar }
ORDER BY DefaultTest4.bar;
''')
self.assert_data_shape(
res[-1], [{
'bar': 0,
}, {
'bar': 1,
}, {
'bar': 2,
}, {
'bar': 3,
}, {
'bar': 4,
}]
)
async def test_edgeql_insert_default_03(self):
res = await self.con.execute(r'''
# by default the 'bar' value is simply going to be "indexing" the
# created objects
INSERT test::DefaultTest4 { bar:= 10 };
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
WITH MODULE test
SELECT DefaultTest4 { bar }
ORDER BY DefaultTest4.bar;
''')
self.assert_data_shape(
res[-1], [{
'bar': 1,
}, {
'bar': 2,
}, {
'bar': 10,
}]
)
async def test_edgeql_insert_default_04(self):
res = await self.con.execute(r'''
# by default the 'bar' value is simply going to be "indexing" the
# created objects
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
INSERT test::DefaultTest4 { bar:= 0 };
INSERT test::DefaultTest4;
INSERT test::DefaultTest4;
WITH MODULE test
SELECT DefaultTest4 { bar }
ORDER BY DefaultTest4.bar;
''')
self.assert_data_shape(
res[-1], [{
'bar': 0,
}, {
'bar': 0,
}, {
'bar': 1,
}, {
'bar': 3,
}, {
'bar': 4,
}]
)
@unittest.expectedFailure
async def test_edgeql_insert_as_expr_01(self):
res = await self.con.execute(r'''
# insert several objects, then annotate one of the inserted batch
WITH MODULE test
FOR x IN {(
SELECT _i := (
FOR y IN {3, 5, 7, 2}
UNION (INSERT InsertTest {
name := 'insert expr 1',
l2 := y,
})
) ORDER BY _i.l2 DESC LIMIT 1
)}
UNION (INSERT Annotation {
name := 'insert expr 1',
note := 'largest ' + <str>x.l2,
subject := x
});
WITH MODULE test
SELECT
InsertTest {
name,
l2,
l3,
<subject: {
name,
note,
}
}
FILTER .name = 'insert expr 1'
ORDER BY .l2;
''')
self.assert_data_shape(
res[-1], [
# inserted based on static data
{
'name': 'insert expr 1',
'l2': 2,
'l3': 'test',
'subject': None,
},
{
'name': 'insert expr 1',
'l2': 3,
'l3': 'test',
'subject': None,
},
{
'name': 'insert expr 1',
'l2': 5,
'l3': 'test',
'subject': None,
},
{
'name': 'insert expr 1',
'l2': 7,
'l3': 'test',
'subject': [{
'name': 'insert expr 1',
'note': 'largest 7'
}]
},
]
)
@unittest.expectedFailure
async def test_edgeql_insert_as_expr_02(self):
res = await self.con.execute(r'''
# same as above, but refactored differently
WITH
MODULE test,
_i := (
FOR x IN {3, 5, 7, 2}
UNION (INSERT InsertTest {
name := 'insert expr 2',
l2 := x,
})
),
y := (SELECT _i ORDER BY _i.l2 DESC LIMIT 1)
INSERT Annotation {
name := 'insert expr 2',
note := 'largest ' + <str>y.l2,
subject := y
};
WITH MODULE test
SELECT
InsertTest {
name,
l2,
l3,
<subject: {
name,
note,
}
}
FILTER .name = 'insert expr 2'
ORDER BY .l2;
''')
self.assert_data_shape(
res[-1], [
# inserted based on static data
{
'name': 'insert expr 2',
'l2': 2,
'l3': 'test',
'subject': None,
},
{
'name': 'insert expr 2',
'l2': 3,
'l3': 'test',
'subject': None,
},
{
'name': 'insert expr 2',
'l2': 5,
'l3': 'test',
'subject': None,
},
{
'name': 'insert expr 2',
'l2': 7,
'l3': 'test',
'subject': [{
'name': 'insert expr 2',
'note': 'largest 7'
}]
},
]
)
async def test_edgeql_insert_linkprops_01(self):
res = await self.con.execute(r'''
INSERT test::Offer {
title := (SELECT 'Super offer' {@lang := 'en-US'})
};
INSERT test::Offer {
title := (SELECT 'Sample' {@lang := 'en-CA'}),
special := (SELECT 'Unavailable, sorry.' {@lang := 'en-CA'})
};
WITH MODULE test
SELECT Offer {
title: {
@target,
@lang
} ORDER BY Offer.title@lang,
special: {
@target,
@lang
}
}
ORDER BY Offer.special;
''')
self.assert_data_shape(
res[-1], [{
'title': [{
'@target': 'Super offer',
'@lang': 'en-US',
}],
}, {
'title': [{
'@target': 'Sample',
'@lang': 'en-CA',
}],
'special': {
'@target': 'Unavailable, sorry.',
'@lang': 'en-CA',
},
}]
)
@unittest.expectedFailure
async def test_edgeql_insert_polymorphic_01(self):
res = await self.con.execute(r'''
WITH MODULE test
INSERT Directive {
args: {
val := "something"
},
};
WITH MODULE test
SELECT Callable {
args: {
val
}
};
WITH MODULE test
SELECT Field {
args: {
val
}
};
WITH MODULE test
SELECT Directive {
args: {
val
}
};
WITH MODULE test
SELECT InputValue {
val
};
''')
self.assert_data_shape(
res, [
[1],
[{
'args': {'val': 'something'},
}],
[],
[{
'args': {'val': 'something'},
}],
[{
'val': 'something',
}],
]
)