forked from geldata/gel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_server_proto.py
3005 lines (2416 loc) · 95.5 KB
/
test_server_proto.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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# This source file is part of the EdgeDB open source project.
#
# Copyright 2019-present MagicStack Inc. and the EdgeDB authors.
#
# 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.
#
import asyncio
import decimal
import json
import uuid
import struct
import unittest
import edgedb
from edb.common import devmode
from edb.common import taskgroup as tg
from edb.testbase import server as tb
from edb.server.compiler import enums
from edb.tools import test
SERVER_HEADER_CAPABILITIES = 0x1001
ALL_CAPABILITIES = 0xFFFFFFFFFFFFFFFF
def _capabilities(attrs):
bytes = attrs.pop(SERVER_HEADER_CAPABILITIES)
return enums.Capability(struct.unpack('>Q', bytes)[0])
class TestServerProto(tb.QueryTestCase):
TRANSACTION_ISOLATION = False
SETUP = '''
CREATE TYPE Tmp {
CREATE REQUIRED PROPERTY tmp -> std::str;
};
CREATE MODULE test;
CREATE TYPE test::Tmp2 {
CREATE REQUIRED PROPERTY tmp -> std::str;
};
CREATE TYPE TransactionTest EXTENDING std::Object {
CREATE PROPERTY name -> std::str;
};
CREATE SCALAR TYPE RGB
EXTENDING enum<'RED', 'BLUE', 'GREEN'>;
# Used by is_testmode_on() to ensure that config modifications
# persist correctly when set inside and outside of (potentially
# failing) transaction blocks.
CONFIGURE SESSION SET __internal_testmode := true;
'''
TEARDOWN = '''
DROP TYPE Tmp;
'''
def setUp(self):
super().setUp()
# Reset cached codecs for every test. That ensures that
# tests cannot interfere with each other when the connection
# is reused.
self.con._clear_codecs_cache()
async def is_testmode_on(self):
# The idea is that if __internal_testmode value config is lost
# (no longer "true") then this script fails.
try:
await self.con.execute('''
CREATE FUNCTION testconf() -> bool
USING SQL $$ SELECT true; $$;
DROP FUNCTION testconf();
''')
except edgedb.InvalidFunctionDefinitionError:
return False
return await self.con.query_single('''
SELECT cfg::Config.__internal_testmode LIMIT 1
''')
async def test_server_proto_parse_redirect_data_01(self):
# This is a regression fuzz test for ReadBuffer.redirect_messages().
# The bug was related to 'D' messages that were filling the entire
# receive buffer (8192 bytes) precisely.
for power in range(10, 20):
base = 2 ** power
for i in range(base - 100, base + 100):
v = await self.con.query_single(
'select str_repeat(".", <int64>$i)', i=i)
self.assertEqual(len(v), i)
async def test_server_proto_parse_error_recover_01(self):
for _ in range(2):
with self.assertRaises(edgedb.EdgeQLSyntaxError):
await self.con.query('select syntax error')
with self.assertRaises(edgedb.EdgeQLSyntaxError):
await self.con.query('select syntax error')
with self.assertRaisesRegex(edgedb.EdgeQLSyntaxError,
'Unexpected end of line'):
await self.con.query('select (')
with self.assertRaisesRegex(edgedb.EdgeQLSyntaxError,
'Unexpected end of line'):
await self.con.query_json('select (')
for _ in range(10):
self.assertEqual(
await self.con.query('select 1;'),
edgedb.Set((1,)))
self.assertTrue(await self.is_testmode_on())
async def test_server_proto_parse_error_recover_02(self):
for _ in range(2):
with self.assertRaises(edgedb.EdgeQLSyntaxError):
await self.con.execute('select syntax error')
with self.assertRaises(edgedb.EdgeQLSyntaxError):
await self.con.execute('select syntax error')
for _ in range(10):
await self.con.execute('select 1; select 2;'),
async def test_server_proto_exec_error_recover_01(self):
for _ in range(2):
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.query('select 1 / 0;')
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.query('select 1 / 0;')
self.assertEqual(self.con._get_last_status(), None)
for _ in range(10):
self.assertEqual(
await self.con.query('select 1;'),
edgedb.Set((1,)))
self.assertEqual(self.con._get_last_status(), 'SELECT')
async def test_server_proto_exec_error_recover_02(self):
for _ in range(2):
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.execute('select 1 / 0;')
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.execute('select 1 / 0;')
for _ in range(10):
await self.con.execute('select 1;')
async def test_server_proto_exec_error_recover_03(self):
query = 'select 10 // <int64>$0;'
for i in [1, 2, 0, 3, 1, 0, 1]:
if i:
self.assertEqual(
await self.con.query(query, i),
edgedb.Set([10 // i]))
else:
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.query(query, i)
async def test_server_proto_exec_error_recover_04(self):
for i in [1, 2, 0, 3, 1, 0, 1]:
if i:
await self.con.execute(f'select 10 // {i};')
else:
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.query(f'select 10 // {i};')
async def test_server_proto_exec_error_recover_05(self):
with self.assertRaisesRegex(edgedb.QueryError,
'cannot accept parameters'):
await self.con.execute(f'select <int64>$0')
self.assertEqual(
await self.con.query('SELECT "HELLO"'),
["HELLO"])
async def test_server_proto_fetch_single_command_01(self):
r = await self.con.query('''
CREATE TYPE server_fetch_single_command_01 {
CREATE REQUIRED PROPERTY server_fetch_single_command_01 ->
std::str;
};
''')
self.assertEqual(r, [])
self.assertEqual(self.con._get_last_status(), 'CREATE TYPE')
r = await self.con.query('''
DROP TYPE server_fetch_single_command_01;
''')
self.assertEqual(r, [])
self.assertEqual(self.con._get_last_status(), 'DROP TYPE')
r = await self.con.query('''
CREATE TYPE server_fetch_single_command_01 {
CREATE REQUIRED PROPERTY server_fetch_single_command_01 ->
std::str;
};
''')
self.assertEqual(len(r), 0)
r = await self.con.query('''
DROP TYPE server_fetch_single_command_01;
''')
self.assertEqual(len(r), 0)
r = await self.con.query_json('''
CREATE TYPE server_fetch_single_command_01 {
CREATE REQUIRED PROPERTY server_fetch_single_command_01 ->
std::str;
};
''')
self.assertEqual(r, '[]')
r = await self.con.query_json('''
DROP TYPE server_fetch_single_command_01;
''')
self.assertEqual(r, '[]')
async def test_server_proto_fetch_single_command_02(self):
r = await self.con.query('''
SET MODULE default;
''')
self.assertEqual(r, [])
self.assertEqual(self.con._get_last_status(), 'SET ALIAS')
r = await self.con.query('''
SET ALIAS foo AS MODULE default;
''')
self.assertEqual(r, [])
r = await self.con.query('''
SET MODULE default;
''')
self.assertEqual(len(r), 0)
r = await self.con.query_json('''
SET ALIAS foo AS MODULE default;
''')
self.assertEqual(r, '[]')
r = await self.con.query_json('''
SET MODULE default;
''')
self.assertEqual(r, '[]')
r = await self.con.query_json('''
SET ALIAS foo AS MODULE default;
''')
self.assertEqual(r, '[]')
async def test_server_proto_fetch_single_command_03(self):
qs = [
'START TRANSACTION',
'DECLARE SAVEPOINT t0',
'ROLLBACK TO SAVEPOINT t0',
'RELEASE SAVEPOINT t0',
'ROLLBACK',
'START TRANSACTION',
'COMMIT',
]
for _ in range(3):
for q in qs:
r = await self.con.query(q)
self.assertEqual(r, [])
for q in qs:
r = await self.con.query_json(q)
self.assertEqual(r, '[]')
with self.assertRaisesRegex(
edgedb.InterfaceError,
r'it does not return any data'):
await self.con.query_required_single('START TRANSACTION')
with self.assertRaisesRegex(
edgedb.InterfaceError,
r'it does not return any data'):
await self.con.query_required_single_json('START TRANSACTION')
async def test_server_proto_fetch_single_command_04(self):
with self.assertRaisesRegex(edgedb.ProtocolError,
'expected one statement'):
await self.con.query('''
SELECT 1;
SET MODULE blah;
''')
with self.assertRaisesRegex(edgedb.ProtocolError,
'expected one statement'):
await self.con.query_single('''
SELECT 1;
SET MODULE blah;
''')
with self.assertRaisesRegex(edgedb.ProtocolError,
'expected one statement'):
await self.con.query_json('''
SELECT 1;
SET MODULE blah;
''')
async def test_server_proto_set_reset_alias_01(self):
await self.con.execute('''
SET ALIAS foo AS MODULE std;
SET ALIAS bar AS MODULE std;
SET MODULE test;
''')
self.assertEqual(
await self.con.query('SELECT foo::min({1}) + bar::min({0})'),
[1])
self.assertEqual(
await self.con.query('''
SELECT count(
Tmp2 FILTER Tmp2.tmp = "test_server_set_reset_alias_01");
'''),
[0])
await self.con.execute('''
RESET ALIAS bar;
''')
self.assertEqual(
await self.con.query('SELECT foo::min({1})'),
[1])
with self.assertRaisesRegex(
edgedb.InvalidReferenceError,
"function 'bar::min' does not exist"):
await self.con.query('SELECT bar::min({1})')
await self.con.query('''
RESET ALIAS *;
''')
with self.assertRaisesRegex(
edgedb.InvalidReferenceError,
"function 'foo::min' does not exist"):
await self.con.query('SELECT foo::min({3})')
self.assertEqual(
await self.con.query('SELECT min({4})'),
[4])
with self.assertRaisesRegex(
edgedb.InvalidReferenceError,
"object type or alias 'default::Tmp2' does not exist"):
await self.con.query('''
SELECT count(
Tmp2 FILTER Tmp2.tmp = "test_server_set_reset_alias_01");
''')
async def test_server_proto_set_reset_alias_02(self):
await self.con.execute('''
SET ALIAS foo AS MODULE std;
SET ALIAS bar AS MODULE std;
SET MODULE test;
''')
self.assertEqual(
await self.con.query('''
SELECT count(
Tmp2 FILTER Tmp2.tmp = "test_server_set_reset_alias_01");
'''),
[0])
await self.con.execute('''
RESET MODULE;
''')
with self.assertRaisesRegex(
edgedb.InvalidReferenceError,
"object type or alias 'default::Tmp2' does not exist"):
await self.con.query('''
SELECT count(
Tmp2 FILTER Tmp2.tmp = "test_server_set_reset_alias_01");
''')
async def test_server_proto_set_reset_alias_03(self):
with self.assertRaisesRegex(
edgedb.UnknownModuleError, "module 'blahhhh' does not exist"):
await self.con.execute('''
SET ALIAS foo AS MODULE blahhhh;
''')
with self.assertRaisesRegex(
edgedb.UnknownModuleError, "module 'blahhhh' does not exist"):
await self.con.execute('''
SET MODULE blahhhh;
''')
# Test error recovery now
await self.con.execute('''
SET MODULE default;
''')
self.assertEqual(
await self.con.query('''
SELECT count(
Tmp FILTER Tmp.tmp = "test_server_set_reset_alias_01");
'''),
[0])
async def test_server_proto_set_reset_alias_04(self):
with self.assertRaisesRegex(
edgedb.ConfigurationError,
"unrecognized configuration parameter 'blahhhhhh'"):
await self.con.execute('''
SET ALIAS foo AS MODULE std;
CONFIGURE SESSION SET blahhhhhh := 123;
''')
with self.assertRaisesRegex(
edgedb.InvalidReferenceError,
"function 'foo::min' does not exist"):
await self.con.query('SELECT foo::min({3})')
async def test_server_proto_set_reset_alias_05(self):
# A regression test.
# The "DECLARE SAVEPOINT a1; ROLLBACK TO SAVEPOINT a1;" commands
# used to propagate the 'foo -> std' alias to the connection state
# which the failed to correctly revert it back on ROLLBACK.
await self.con.query('START TRANSACTION')
await self.con.execute('''
SET ALIAS foo AS MODULE std;
''')
await self.con.query('DECLARE SAVEPOINT a1')
await self.con.query('ROLLBACK TO SAVEPOINT a1')
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.execute('''
SELECT 1/0;
''')
await self.con.query('ROLLBACK')
with self.assertRaises(edgedb.InvalidReferenceError):
await self.con.execute('''
SELECT foo::len('aaa')
''')
async def test_server_proto_basic_datatypes_01(self):
for _ in range(10):
self.assertEqual(
await self.con.query_single(
'select ()'),
())
self.assertEqual(
await self.con.query(
'select (1,)'),
edgedb.Set([(1,)]))
async with self.con.transaction():
self.assertEqual(
await self.con.query_single(
'select <array<int64>>[]'),
[])
self.assertEqual(
await self.con.query(
'select ["a", "b"]'),
edgedb.Set([["a", "b"]]))
self.assertEqual(
await self.con.query('''
SELECT {(a := 1 + 1 + 40, world := ("hello", 32)),
(a:=1, world := ("yo", 10))};
'''),
edgedb.Set([
edgedb.NamedTuple(a=42, world=("hello", 32)),
edgedb.NamedTuple(a=1, world=("yo", 10)),
]))
with self.assertRaisesRegex(
edgedb.InterfaceError,
r'query_single\(\) as it returns a multiset'):
await self.con.query_single('SELECT {1, 2}')
await self.con.query_single('SELECT <int64>{}')
with self.assertRaisesRegex(
edgedb.NoDataError,
r'returned no data',
):
await self.con.query_required_single('SELECT <int64>{}')
async def test_server_proto_basic_datatypes_02(self):
self.assertEqual(
await self.con.query(
r'''select [b"\x00a", b"b", b'', b'\na', b'=A0']'''),
edgedb.Set([[b"\x00a", b"b", b'', b'\na', b'=A0']]))
self.assertEqual(
await self.con.query(
r'select <bytes>$0', b'he\x00llo'),
edgedb.Set([b'he\x00llo']))
async def test_server_proto_basic_datatypes_03(self):
for _ in range(10):
self.assertEqual(
await self.con.query_json(
'select ()'),
'[[]]')
self.assertEqual(
await self.con.query_json(
'select (1,)'),
'[[1]]')
self.assertEqual(
await self.con.query_json(
'select <array<int64>>[]'),
'[[]]')
self.assertEqual(
json.loads(
await self.con.query_json(
'select ["a", "b"]')),
[["a", "b"]])
self.assertEqual(
json.loads(
await self.con.query_single_json(
'select ["a", "b"]')),
["a", "b"])
self.assertEqual(
json.loads(
await self.con.query_json('''
SELECT {(a := 1 + 1 + 40, world := ("hello", 32)),
(a:=1, world := ("yo", 10))};
''')),
[
{"a": 42, "world": ["hello", 32]},
{"a": 1, "world": ["yo", 10]}
])
self.assertEqual(
json.loads(
await self.con.query_json('SELECT {1, 2}')),
[1, 2])
self.assertEqual(
json.loads(await self.con.query_json('SELECT <int64>{}')),
[])
with self.assertRaises(edgedb.NoDataError):
await self.con.query_required_single_json('SELECT <int64>{}')
self.assertEqual(self.con._get_last_status(), 'SELECT')
async def test_server_proto_basic_datatypes_04(self):
# A regression test for enum typedescs being improperly
# serialized and screwing up client's decoder.
d = await self.con.query_single('''
SELECT (<RGB>"RED", <RGB>"GREEN", [1], [<RGB>"GREEN"], [2])
''')
self.assertEqual(d[2], [1])
async def test_server_proto_basic_datatypes_05(self):
# A regression test to ensure that typedesc IDs are different
# for shapes with equal fields names bit of different kinds
# (e.g. in this test it's "@foo" vs "foo"; before fixing the
# bug the results of second query were with "@foo" key, not "foo")
for _ in range(5):
await self.assert_query_result(
r"""
WITH MODULE schema
SELECT ObjectType {
name,
properties: {
name,
@foo := 1
} ORDER BY .name LIMIT 1,
}
FILTER .name = 'default::Tmp';
""",
[{
'name': 'default::Tmp',
'properties': [{
'name': 'id',
'@foo': 1
}],
}]
)
for _ in range(5):
await self.assert_query_result(
r"""
WITH MODULE schema
SELECT ObjectType {
name,
properties: {
name,
foo := 1
} ORDER BY .name LIMIT 1,
}
FILTER .name = 'default::Tmp';
""",
[{
'name': 'default::Tmp',
'properties': [{
'name': 'id',
'foo': 1
}],
}]
)
async def test_server_proto_basic_datatypes_06(self):
# Test that field names are taken into account when
# typedesc id is computed.
for _ in range(5):
await self.assert_query_result(
r"""
WITH MODULE schema
SELECT ObjectType {
name,
properties: {
name,
foo1 := 1
} ORDER BY .name LIMIT 1,
}
FILTER .name = 'default::Tmp';
""",
[{
'name': 'default::Tmp',
'properties': [{
'name': 'id',
'foo1': 1
}],
}]
)
for _ in range(5):
await self.assert_query_result(
r"""
WITH MODULE schema
SELECT ObjectType {
name,
properties: {
name,
foo2 := 1
} ORDER BY .name LIMIT 1,
}
FILTER .name = 'default::Tmp';
""",
[{
'name': 'default::Tmp',
'properties': [{
'name': 'id',
'foo2': 1
}],
}]
)
async def test_server_proto_args_01(self):
self.assertEqual(
await self.con.query(
'select (<array<str>>$foo)[0] ++ (<array<str>>$bar)[0];',
foo=['aaa'], bar=['bbb']),
edgedb.Set(('aaabbb',)))
async def test_server_proto_args_02(self):
self.assertEqual(
await self.con.query(
'select (<array<str>>$0)[0] ++ (<array<str>>$1)[0];',
['aaa'], ['bbb']),
edgedb.Set(('aaabbb',)))
async def test_server_proto_args_03(self):
with self.assertRaisesRegex(edgedb.QueryError, r'missing \$0'):
await self.con.query('select <int64>$1;')
with self.assertRaisesRegex(edgedb.QueryError, r'missing \$1'):
await self.con.query('select <int64>$0 + <int64>$2;')
with self.assertRaisesRegex(edgedb.QueryError,
'combine positional and named parameters'):
await self.con.query('select <int64>$0 + <int64>$bar;')
async def test_server_proto_args_04(self):
self.assertEqual(
await self.con.query_json(
'select (<array<str>>$0)[0] ++ (<array<str>>$1)[0];',
['aaa'], ['bbb']),
'["aaabbb"]')
async def test_server_proto_args_05(self):
self.assertEqual(
await self.con.query_json(
'select (<array<str>>$foo)[0] ++ (<array<str>>$bar)[0];',
foo=['aaa'], bar=['bbb']),
'["aaabbb"]')
async def test_server_proto_args_06(self):
for _ in range(10):
self.assertEqual(
await self.con.query_single(
'select <int64>$你好 + 10',
你好=32),
42)
async def test_server_proto_args_07(self):
with self.assertRaisesRegex(edgedb.QueryError,
r'missing a type cast.*parameter'):
await self.con.query_single(
'select schema::Object {name} filter .id=$id', id='asd')
async def test_server_proto_args_08(self):
async with self._run_and_rollback():
await self.con.execute(
'''
CREATE TYPE str;
CREATE TYPE int64;
CREATE TYPE float64;
CREATE TYPE decimal;
CREATE TYPE bigint;
'''
)
self.assertEqual(
await self.con.query_single('select ("1", 1, 1.1, 1.1n, 1n)'),
('1', 1, 1.1, decimal.Decimal('1.1'), 1)
)
async def test_server_proto_args_09(self):
async with self._run_and_rollback():
self.assertEqual(
await self.con.query_single(
'WITH std AS MODULE math SELECT ("1", 1, 1.1, 1.1n, 1n)'
),
('1', 1, 1.1, decimal.Decimal('1.1'), 1)
)
async def test_server_proto_wait_cancel_01(self):
# Test that client protocol handles waits interrupted
# by closing.
lock_key = tb.gen_lock_key()
con2 = await self.connect(database=self.con.dbname)
await self.con.query('START TRANSACTION')
await self.con.query(
'select sys::_advisory_lock(<int64>$0)', lock_key)
try:
async with tg.TaskGroup() as g:
async def exec_to_fail():
with self.assertRaises(edgedb.ClientConnectionClosedError):
await con2.query(
'select sys::_advisory_lock(<int64>$0)', lock_key)
g.create_task(exec_to_fail())
await asyncio.sleep(0.1)
con2.terminate()
# Give the server some time to actually close the con2 connection.
await asyncio.sleep(2)
finally:
k = await self.con.query(
'select sys::_advisory_unlock(<int64>$0)', lock_key)
await self.con.query('ROLLBACK')
self.assertEqual(k, [True])
async def test_server_proto_log_message_01(self):
msgs = []
def on_log(con, msg):
msgs.append(msg)
self.con.add_log_listener(on_log)
try:
await self.con.query(
'configure system set __internal_restart := true;')
await asyncio.sleep(0.01) # allow the loop to call the callback
finally:
self.con.remove_log_listener(on_log)
for msg in msgs:
if (msg.get_severity_name() == 'NOTICE' and
'server restart is required' in str(msg)):
break
else:
raise AssertionError('a notice message was not delivered')
async def test_server_proto_tx_savepoint_01(self):
# Basic test that SAVEPOINTS actually work; test with DML.
typename = 'Savepoint_01'
query = f'SELECT {typename}.prop1'
con = self.con
# __internal_testmode should be ON
self.assertTrue(await self.is_testmode_on())
await con.query('START TRANSACTION')
await con.execute(f'''
CONFIGURE SESSION SET __internal_testmode := false;
''')
await con.query('DECLARE SAVEPOINT t1')
await con.execute(f'''
CREATE TYPE {typename} {{
CREATE REQUIRED PROPERTY prop1 -> std::str;
}};
''')
await con.query('DECLARE SAVEPOINT t1')
self.assertEqual(self.con._get_last_status(), 'DECLARE SAVEPOINT')
# Make sure that __internal_testmode was indeed updated.
self.assertFalse(await self.is_testmode_on())
# is_testmode_on call caused an error; rollback
await con.query('ROLLBACK TO SAVEPOINT t1')
try:
await con.execute(f'''
INSERT {typename} {{
prop1 := 'aaa'
}};
''')
await self.con.query('DECLARE SAVEPOINT t1')
await con.execute(f'''
INSERT {typename} {{
prop1 := 'bbb'
}};
''')
await self.con.query('DECLARE SAVEPOINT t2')
await con.execute(f'''
INSERT {typename} {{
prop1 := 'ccc'
}};
''')
await self.con.query('DECLARE SAVEPOINT t1')
await con.execute(f'''
INSERT {typename} {{
prop1 := 'ddd'
}};
''')
await self.con.query('DECLARE SAVEPOINT t3')
self.assertEqual(
await con.query(query),
edgedb.Set(('aaa', 'bbb', 'ccc', 'ddd')))
for _ in range(10):
await con.query('ROLLBACK TO SAVEPOINT t1')
self.assertEqual(
await con.query(query),
edgedb.Set(('aaa', 'bbb', 'ccc')))
await con.query('RELEASE SAVEPOINT t1')
self.assertEqual(
await con.query(query),
edgedb.Set(('aaa', 'bbb', 'ccc')))
for _ in range(5):
await con.query('ROLLBACK TO SAVEPOINT t1')
self.assertEqual(
await con.query(query),
edgedb.Set(('aaa',)))
await con.query('RELEASE SAVEPOINT t1')
await con.query('RELEASE SAVEPOINT t1')
await con.query('ROLLBACK TO SAVEPOINT t1')
with self.assertRaisesRegex(
edgedb.InvalidReferenceError,
".*Savepoint.*does not exist"):
await con.query(query)
finally:
await con.query('ROLLBACK')
# __internal_testmode should be ON, just as when the test method
# was called.
self.assertTrue(await self.is_testmode_on())
async def test_server_proto_tx_savepoint_02(self):
with self.assertRaisesRegex(
edgedb.TransactionError, 'savepoints can only be used in tra'):
await self.con.query('DECLARE SAVEPOINT t1')
with self.assertRaisesRegex(
edgedb.TransactionError, 'savepoints can only be used in tra'):
await self.con.query('DECLARE SAVEPOINT t1')
async def test_server_proto_tx_savepoint_03(self):
# Test that PARSE/EXECUTE/OPPORTUNISTIC-EXECUTE play nice
# with savepoints.
await self.con.query('START TRANSACTION')
await self.con.query('DECLARE SAVEPOINT t0')
try:
self.assertEqual(
await self.con.query('SELECT 1;'),
[1])
with self.assertRaisesRegex(
edgedb.TransactionError, "there is no 't1' savepoint"):
await self.con.query('''
RELEASE SAVEPOINT t1;
''')
with self.assertRaisesRegex(
edgedb.TransactionError, "current transaction is aborted"):
await self.con.query('SELECT 1;')
with self.assertRaisesRegex(
edgedb.TransactionError, "current transaction is aborted"):
await self.con.query_single('''
RELEASE SAVEPOINT t1;
''')
await self.con.query('''
ROLLBACK TO SAVEPOINT t0;
''')
self.assertEqual(
await self.con.query('SELECT 1;'),
[1])
with self.assertRaisesRegex(
edgedb.TransactionError, "there is no 't1' savepoint"):
await self.con.query('''
RELEASE SAVEPOINT t1;
''')
with self.assertRaisesRegex(
edgedb.TransactionError, "current transaction is aborted"):
await self.con.query('SELECT 1;')
with self.assertRaisesRegex(
edgedb.TransactionError, "current transaction is aborted"):
await self.con.query('''
RELEASE SAVEPOINT t1;
''')
finally:
await self.con.query('ROLLBACK')
self.assertEqual(
await self.con.query('SELECT 1;'),
[1])
async def test_server_proto_tx_savepoint_04(self):
# Test that PARSE/EXECUTE/OPPORTUNISTIC-EXECUTE play nice
# with savepoints.
await self.con.query('START TRANSACTION')
await self.con.query('DECLARE SAVEPOINT t0')
try:
self.assertEqual(
await self.con.query('SELECT 1;'),
[1])
with self.assertRaises(edgedb.DivisionByZeroError):
await self.con.query('''
SELECT 1 / 0;
''')
with self.assertRaisesRegex(