forked from maximdanilchenko/aiochclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
964 lines (819 loc) · 34.3 KB
/
tests.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
import datetime as dt
from decimal import Decimal
from ipaddress import IPv4Address, IPv6Address
from uuid import uuid4
import aiohttp
import httpx
import pytest
from aiochclient import ChClient, ChClientError
pytestmark = pytest.mark.asyncio
@pytest.fixture
def uuid():
return uuid4()
@pytest.fixture
def rows(uuid):
return [
[
1,
1000,
10000,
12_345_678_910,
-4,
-453,
21322,
-32123,
23.432,
-56754.564_542,
"hello man",
"hello fixed man".ljust(32, " "),
dt.date(2018, 9, 21),
dt.datetime(2018, 9, 21, 10, 32, 23),
"hello",
"world",
[1, 2, 3, 4],
(4, "hello"),
0,
["hello", "world"],
["hello", "world"],
["hello", None],
"'\b\f\r\n\t\\",
uuid,
[uuid, uuid, uuid],
["hello", "world", "hello"],
[dt.date(2018, 9, 21), dt.date(2018, 9, 22)],
[
dt.datetime(2018, 9, 21, 10, 32, 23),
dt.datetime(2018, 9, 21, 10, 32, 24),
],
"hello man",
"hello man",
777,
dt.date(1994, 9, 7),
dt.datetime(2018, 9, 21, 10, 32, 23),
Decimal('1234.5678'),
Decimal('1234.56'),
Decimal('1234.56'),
Decimal('123.56'),
[[1, 2, 3], [1, 2], [6, 7]],
IPv4Address('116.253.40.133'),
IPv6Address('2001:44c8:129:2632:33:0:252:2'),
dt.datetime(2018, 9, 21, 10, 32, 23),
True,
{"hello": "world"},
{"hello": {"inner": "world"}},
],
[
2,
1000,
10000,
12_345_678_910,
-4,
-453,
21322,
-32123,
23.432,
-56754.564_542,
"hello man",
"hello fixed man".ljust(32, " "),
None,
None,
"hello",
"world",
[1, 2, 3, 4],
(4, "hello"),
None,
[],
[],
[],
"'\b\f\r\n\t\\",
None,
[],
[],
[],
[],
"hello man",
None,
777,
dt.date(1994, 9, 7),
dt.datetime(2018, 9, 21, 10, 32, 23),
Decimal('1234.5678'),
Decimal('1234.56'),
Decimal('1234.56'),
Decimal('123.56'),
[],
None,
None,
1546300800000,
False,
{"hello": "world"},
{"hello": {"inner": "world"}},
],
]
@pytest.fixture(params=[aiohttp.ClientSession, httpx.AsyncClient])
def http_client(request):
return request.param
@pytest.fixture(
params=[
{
"compress_response": True,
"user": "default",
"password": "",
"database": "default",
"allow_suspicious_low_cardinality_types": 1,
},
{"allow_suspicious_low_cardinality_types": 1},
]
)
async def chclient(request, http_client):
async with ChClient(http_client(), **request.param) as chclient:
yield chclient
@pytest.fixture
async def all_types_db(chclient, rows):
await chclient.execute("DROP TABLE IF EXISTS all_types")
await chclient.execute("DROP TABLE IF EXISTS test_cache")
await chclient.execute("DROP TABLE IF EXISTS test_cache_mv")
await chclient.execute(
"""
CREATE TABLE all_types (uint8 UInt8,
uint16 UInt16,
uint32 UInt32,
uint64 UInt64,
int8 Int8,
int16 Int16,
int32 Int32,
int64 Int64,
float32 Float32,
float64 Float64,
string String,
fixed_string FixedString(32),
date Nullable(Date),
datetime Nullable(DateTime),
enum8 Enum8('hello' = 1, 'world' = 2),
enum16 Enum16('hello' = 1000, 'world' = 2000),
array_uint8 Array(UInt8),
tuple Tuple(UInt8, String),
nullable Nullable(Int8),
array_string Array(String),
array_low_cardinality_string Array(LowCardinality(String)),
array_nullable_string Array(Nullable(String)),
escape_string String,
uuid Nullable(UUID),
array_uuid Array(UUID),
array_enum Array(Enum8('hello' = 1, 'world' = 2)),
array_date Array(Date),
array_datetime Array(DateTime),
low_cardinality_str LowCardinality(String),
low_cardinality_nullable_str LowCardinality(Nullable(String)),
low_cardinality_int LowCardinality(Int32),
low_cardinality_date LowCardinality(Date),
low_cardinality_datetime LowCardinality(DateTime),
decimal32 Decimal32(4),
decimal64 Decimal64(2),
decimal128 Decimal128(6),
decimal Decimal(6, 3),
array_array_int Array(Array(Int32)),
ipv4 Nullable(IPv4),
ipv6 Nullable(IPv6),
datetime64 DateTime64(3, 'Europe/Moscow'),
bool Bool,
map Map(String, String),
map_map Map(String, Map(String, String))
) ENGINE = Memory
"""
)
await chclient.execute(
"""
CREATE TABLE test_cache (
key String,
int32Cache AggregateFunction(avg, Int32),
float32Cache SimpleAggregateFunction(sum, Float64))
ENGINE = AggregatingMergeTree()
ORDER BY key
"""
)
await chclient.execute(
"""
CREATE MATERIALIZED VIEW test_cache_mv TO test_cache AS
SELECT avgState(int32), sum(float32) FROM all_types
"""
)
await chclient.execute("INSERT INTO all_types VALUES", *rows)
@pytest.fixture
def class_chclient(chclient, all_types_db, rows, request):
request.cls.ch = chclient
cls_rows = rows
cls_rows[1][40] = dt.datetime(
2019, 1, 1, 3, 0
) # DateTime64 always returns datetime type
request.cls.rows = [tuple(r) for r in cls_rows]
@pytest.mark.client
@pytest.mark.usefixtures("class_chclient")
class TestClient:
async def test_is_alive(self):
assert await self.ch.is_alive() is True
async def test_bad_query(self):
with pytest.raises(ChClientError):
await self.ch.execute("SELE")
async def test_bad_select(self):
with pytest.raises(ChClientError):
await self.ch.execute("SELECT * FROM all_types WHERE", 1, 2, 3, 4)
@pytest.mark.types
@pytest.mark.usefixtures("class_chclient")
class TestTypes:
async def select_field(self, field):
return await self.ch.fetchval(f"SELECT {field} FROM all_types WHERE uint8=1")
async def select_record(self, field):
return await self.ch.fetchrow(f"SELECT {field} FROM all_types WHERE uint8=1")
async def select_field_bytes(self, field):
return await self.ch.fetchval(
f"SELECT {field} FROM all_types WHERE uint8=1", decode=False
)
async def select_record_bytes(self, field):
return await self.ch.fetchrow(
f"SELECT {field} FROM all_types WHERE uint8=1", decode=False
)
async def test_uint8(self):
result = 1
assert await self.select_field("uint8") == result
record = await self.select_record("uint8")
assert record[0] == result
assert record["uint8"] == result
result = b"1"
assert await self.select_field_bytes("uint8") == result
record = await self.select_record_bytes("uint8")
assert record[0] == result
assert record["uint8"] == result
async def test_uint16(self):
result = 1000
assert await self.select_field("uint16") == result
record = await self.select_record("uint16")
assert record[0] == result
assert record["uint16"] == result
result = b"1000"
assert await self.select_field_bytes("uint16") == result
record = await self.select_record_bytes("uint16")
assert record[0] == result
assert record["uint16"] == result
async def test_uint32(self):
result = 10000
assert await self.select_field("uint32") == result
record = await self.select_record("uint32")
assert record[0] == result
assert record["uint32"] == result
result = b"10000"
assert await self.select_field_bytes("uint32") == result
record = await self.select_record_bytes("uint32")
assert record[0] == result
assert record["uint32"] == result
async def test_uint64(self):
result = 12_345_678_910
assert await self.select_field("uint64") == result
record = await self.select_record("uint64")
assert record[0] == result
assert record["uint64"] == result
result = b"12345678910"
assert await self.select_field_bytes("uint64") == result
record = await self.select_record_bytes("uint64")
assert record[0] == result
assert record["uint64"] == result
async def test_int8(self):
result = -4
assert await self.select_field("int8") == result
record = await self.select_record("int8")
assert record[0] == result
assert record["int8"] == result
result = b"-4"
assert await self.select_field_bytes("int8") == result
record = await self.select_record_bytes("int8")
assert record[0] == result
assert record["int8"] == result
async def test_int16(self):
result = -453
assert await self.select_field("int16") == result
record = await self.select_record("int16")
assert record[0] == result
assert record["int16"] == result
result = b"-453"
assert await self.select_field_bytes("int16") == result
record = await self.select_record_bytes("int16")
assert record[0] == result
assert record["int16"] == result
async def test_int32(self):
result = 21322
assert await self.select_field("int32") == result
record = await self.select_record("int32")
assert record[0] == result
assert record["int32"] == result
result = b"21322"
assert await self.select_field_bytes("int32") == result
record = await self.select_record_bytes("int32")
assert record[0] == result
assert record["int32"] == result
async def test_int64(self):
result = -32123
assert await self.select_field("int64") == result
record = await self.select_record("int64")
assert record[0] == result
assert record["int64"] == result
result = b"-32123"
assert await self.select_field_bytes("int64") == result
record = await self.select_record_bytes("int64")
assert record[0] == result
assert record["int64"] == result
async def test_float32(self):
result = 23.432
assert await self.select_field("float32") == result
record = await self.select_record("float32")
assert record[0] == result
assert record["float32"] == result
result = b"23.432"
assert await self.select_field_bytes("float32") == result
record = await self.select_record_bytes("float32")
assert record[0] == result
assert record["float32"] == result
async def test_float64(self):
result = -56754.564_542
assert await self.select_field("float64") == result
record = await self.select_record("float64")
assert record[0] == result
assert record["float64"] == result
result = b"-56754.564542"
assert await self.select_field_bytes("float64") == result
record = await self.select_record_bytes("float64")
assert record[0] == result
assert record["float64"] == result
async def test_string(self):
result = "hello man"
assert await self.select_field("string") == result
record = await self.select_record("string")
assert record[0] == result
assert record["string"] == result
result = b"hello man"
assert await self.select_field_bytes("string") == result
record = await self.select_record_bytes("string")
assert record[0] == result
assert record["string"] == result
async def test_fixed_string(self):
result = "hello fixed man".ljust(32, " ")
assert await self.select_field("fixed_string") == result
record = await self.select_record("fixed_string")
assert record[0] == result
assert record["fixed_string"] == result
result = b"hello fixed man".ljust(32, b" ")
assert await self.select_field_bytes("fixed_string") == result
record = await self.select_record_bytes("fixed_string")
assert record[0] == result
assert record["fixed_string"] == result
async def test_date(self):
result = dt.date(2018, 9, 21)
assert await self.select_field("date") == result
record = await self.select_record("date")
assert record[0] == result
assert record["date"] == result
result = b"2018-09-21"
assert await self.select_field_bytes("date") == result
record = await self.select_record_bytes("date")
assert record[0] == result
assert record["date"] == result
async def test_datetime(self):
result = dt.datetime(2018, 9, 21, 10, 32, 23)
assert await self.select_field("datetime") == result
record = await self.select_record("datetime")
assert record[0] == result
assert record["datetime"] == result
result = b"2018-09-21 10:32:23"
assert await self.select_field_bytes("datetime") == result
record = await self.select_record_bytes("datetime")
assert record[0] == result
assert record["datetime"] == result
async def test_enum8(self):
result = "hello"
assert await self.select_field("enum8") == result
record = await self.select_record("enum8")
assert record[0] == result
assert record["enum8"] == result
result = b"hello"
assert await self.select_field_bytes("enum8") == result
record = await self.select_record_bytes("enum8")
assert record[0] == result
assert record["enum8"] == result
async def test_enum16(self):
result = "world"
assert await self.select_field("enum16") == result
record = await self.select_record("enum16")
assert record[0] == result
assert record["enum16"] == result
result = b"world"
assert await self.select_field_bytes("enum16") == result
record = await self.select_record_bytes("enum16")
assert record[0] == result
assert record["enum16"] == result
async def test_array_uint8(self):
result = [1, 2, 3, 4]
assert await self.select_field("array_uint8") == result
record = await self.select_record("array_uint8")
assert record[0] == result
assert record["array_uint8"] == result
result = b"[1,2,3,4]"
assert await self.select_field_bytes("array_uint8") == result
record = await self.select_record_bytes("array_uint8")
assert record[0] == result
assert record["array_uint8"] == result
async def test_tuple(self):
result = (4, "hello")
assert await self.select_field("tuple") == result
record = await self.select_record("tuple")
assert record[0] == result
assert record["tuple"] == result
result = b"(4,'hello')"
assert await self.select_field_bytes("tuple") == result
record = await self.select_record_bytes("tuple")
assert record[0] == result
assert record["tuple"] == result
async def test_map(self):
result = {"hello": "world"}
assert await self.select_field("map") == result
record = await self.select_record("map")
assert record[0] == result
assert record["map"] == result
result = b"{'hello':'world'}"
assert await self.select_field_bytes("map") == result
record = await self.select_record_bytes("map")
assert record[0] == result
assert record["map"] == result
async def test_map_map(self):
result = {"hello": {"inner": "world"}}
assert await self.select_field("map_map") == result
record = await self.select_record("map_map")
assert record[0] == result
assert record["map_map"] == result
result = b"{'hello':{'inner':'world'}}"
assert await self.select_field_bytes("map_map") == result
record = await self.select_record_bytes("map_map")
assert record[0] == result
assert record["map_map"] == result
async def test_nullable(self):
result = 0
assert await self.select_field("nullable") == result
record = await self.select_record("nullable")
assert record[0] == result
assert record["nullable"] == result
result = b"0"
assert await self.select_field_bytes("nullable") == result
record = await self.select_record_bytes("nullable")
assert record[0] == result
assert record["nullable"] == result
async def test_array_string(self):
result = ["hello", "world"]
assert await self.select_field("array_string") == result
record = await self.select_record("array_string")
assert record[0] == result
assert record["array_string"] == result
result = b"['hello','world']"
assert await self.select_field_bytes("array_string") == result
record = await self.select_record_bytes("array_string")
assert record[0] == result
assert record["array_string"] == result
async def test_array_low_cardinality_string(self):
result = ["hello", "world"]
assert await self.select_field("array_low_cardinality_string") == result
record = await self.select_record("array_low_cardinality_string")
assert record[0] == result
assert record["array_low_cardinality_string"] == result
result = b"['hello','world']"
assert await self.select_field_bytes("array_low_cardinality_string") == result
record = await self.select_record_bytes("array_low_cardinality_string")
assert record[0] == result
assert record["array_low_cardinality_string"] == result
async def test_array_nullable_string(self):
result = ["hello", None]
assert await self.select_field("array_nullable_string") == result
record = await self.select_record("array_nullable_string")
assert record[0] == result
assert record["array_nullable_string"] == result
result = b"['hello',NULL]"
assert await self.select_field_bytes("array_nullable_string") == result
record = await self.select_record_bytes("array_nullable_string")
assert record[0] == result
assert record["array_nullable_string"] == result
async def test_escape_string(self):
result = "'\b\f\r\n\t\\"
assert await self.select_field("escape_string") == result
record = await self.select_record("escape_string")
assert record[0] == result
assert record["escape_string"] == result
result = b"\\'\\b\\f\\r\\n\\t\\\\"
assert await self.select_field_bytes("escape_string") == result
record = await self.select_record_bytes("escape_string")
assert record[0] == result
assert record["escape_string"] == result
async def test_uuid(self, uuid):
result = uuid
assert await self.select_field("uuid") == result
record = await self.select_record("uuid")
assert record[0] == result
assert record["uuid"] == result
result = str(uuid).encode()
assert await self.select_field_bytes("uuid") == result
record = await self.select_record_bytes("uuid")
assert record[0] == result
assert record["uuid"] == result
async def test_array_uuid(self, uuid):
result = [uuid, uuid, uuid]
assert await self.select_field("array_uuid") == result
record = await self.select_record("array_uuid")
assert record[0] == result
assert record["array_uuid"] == result
result = str([str(uuid), str(uuid), str(uuid)]).replace(" ", "").encode()
assert await self.select_field_bytes("array_uuid") == result
record = await self.select_record_bytes("array_uuid")
assert record[0] == result
assert record["array_uuid"] == result
async def test_array_enum(self):
result = ["hello", "world", "hello"]
assert await self.select_field("array_enum ") == result
record = await self.select_record("array_enum ")
assert record[0] == result
assert record["array_enum"] == result
result = b"['hello','world','hello']"
assert await self.select_field_bytes("array_enum ") == result
record = await self.select_record_bytes("array_enum ")
assert record[0] == result
assert record["array_enum"] == result
async def test_array_date(self):
assert await self.select_field("array_date ") == [
dt.date(2018, 9, 21),
dt.date(2018, 9, 22),
]
assert await self.select_field_bytes("array_date ") == (
b"['2018-09-21','2018-09-22']"
)
async def test_array_datetime(self):
assert await self.select_field("array_datetime ") == [
dt.datetime(2018, 9, 21, 10, 32, 23),
dt.datetime(2018, 9, 21, 10, 32, 24),
]
assert await self.select_field_bytes("array_datetime ") == (
b"['2018-09-21 10:32:23','2018-09-21 10:32:24']"
)
async def test_low_cardinality_str(self):
result = "hello man"
assert await self.select_field("low_cardinality_str") == result
record = await self.select_record("low_cardinality_str")
assert record[0] == result
assert record["low_cardinality_str"] == result
result = b"hello man"
assert await self.select_field_bytes("low_cardinality_str") == result
record = await self.select_record_bytes("low_cardinality_str")
assert record[0] == result
assert record["low_cardinality_str"] == result
async def test_low_cardinality_nullable_str(self):
result = "hello man"
assert await self.select_field("low_cardinality_nullable_str") == result
record = await self.select_record("low_cardinality_nullable_str")
assert record[0] == result
assert record["low_cardinality_nullable_str"] == result
result = b"hello man"
assert await self.select_field_bytes("low_cardinality_nullable_str") == result
record = await self.select_record_bytes("low_cardinality_nullable_str")
assert record[0] == result
assert record["low_cardinality_nullable_str"] == result
async def test_low_cardinality_int(self):
result = 777
assert await self.select_field("low_cardinality_int") == result
record = await self.select_record("low_cardinality_int")
assert record[0] == result
assert record["low_cardinality_int"] == result
result = b"777"
assert await self.select_field_bytes("low_cardinality_int") == result
record = await self.select_record_bytes("low_cardinality_int")
assert record[0] == result
assert record["low_cardinality_int"] == result
async def test_low_cardinality_date(self):
result = dt.date(1994, 9, 7)
assert await self.select_field("low_cardinality_date") == result
record = await self.select_record("low_cardinality_date")
assert record[0] == result
assert record["low_cardinality_date"] == result
result = b"1994-09-07"
assert await self.select_field_bytes("low_cardinality_date") == result
record = await self.select_record_bytes("low_cardinality_date")
assert record[0] == result
assert record["low_cardinality_date"] == result
async def test_low_cardinality_datetime(self):
assert await self.select_field("low_cardinality_datetime") == dt.datetime(
2018, 9, 21, 10, 32, 23
)
assert (
await self.select_field_bytes("low_cardinality_datetime")
== b"2018-09-21 10:32:23"
)
async def test_decimal(self):
assert await self.select_field("decimal") == Decimal("123.56")
assert await self.select_field_bytes("decimal") == b"123.56"
async def test_decimal32(self):
assert await self.select_field("decimal32") == Decimal("1234.5678")
assert await self.select_field_bytes("decimal32") == b"1234.5678"
async def test_decimal64(self):
assert await self.select_field("decimal64") == Decimal("1234.56")
assert await self.select_field_bytes("decimal64") == b"1234.56"
async def test_decimal128(self):
assert await self.select_field("decimal128") == Decimal("1234.56")
assert await self.select_field_bytes("decimal128") == b"1234.56"
async def test_array_of_arrays(self):
assert await self.select_field("array_array_int") == [[1, 2, 3], [1, 2], [6, 7]]
assert (
await self.select_field_bytes("array_array_int") == b"[[1,2,3],[1,2],[6,7]]"
)
async def test_ipv4(self):
assert await self.select_field("ipv4") == IPv4Address("116.253.40.133")
assert await self.select_field_bytes("ipv4") == b"116.253.40.133"
async def test_ipv6(self):
assert await self.select_field("ipv6") == IPv6Address(
'2001:44c8:129:2632:33:0:252:2'
)
assert await self.select_field_bytes("ipv6") == b"2001:44c8:129:2632:33:0:252:2"
async def test_datetime64(self):
result = dt.datetime(2018, 9, 21, 10, 32, 23)
assert await self.select_field("datetime") == result
record = await self.select_record("datetime")
assert record[0] == result
assert record["datetime"] == result
result = b"2018-09-21 10:32:23"
assert await self.select_field_bytes("datetime") == result
record = await self.select_record_bytes("datetime")
assert record[0] == result
assert record["datetime"] == result
async def test_named_tuples(self):
"""Named tuples are used for example in geohash functions
https://clickhouse.com/docs/en/sql-reference/data-types/tuple/#addressing-tuple-elements
"""
result = await self.ch.fetchval(
f"SELECT (1.0, 2.0)::Tuple(x Float64, y Float64)"
)
assert round(result[0]) == 1
assert round(result[1]) == 2
@pytest.mark.fetching
@pytest.mark.usefixtures("class_chclient")
class TestFetching:
async def test_fetchrow_full(self):
assert (await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=1"))[
:
] == self.rows[0]
async def test_fetchrow_full_with_params(self):
assert (
await self.ch.fetchrow(
"SELECT * FROM all_types WHERE uint8={u8}", params={"u8": 1}
)
)[:] == self.rows[0]
async def test_fetchrow_with_empties(self):
assert (await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=2"))[
:
] == self.rows[1]
async def test_fetchrow_none_result(self):
assert (
await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=42")
) is None
async def test_fetchrow_none_result_with_params(self):
assert (
await self.ch.fetchrow(
"SELECT * FROM all_types WHERE uint8={u8}", params={'u8': 42}
)
) is None
async def test_fetchone_full(self):
assert (await self.ch.fetchone("SELECT * FROM all_types WHERE uint8=1"))[
:
] == self.rows[0]
async def test_fetchone_with_empties(self):
assert (await self.ch.fetchone("SELECT * FROM all_types WHERE uint8=2"))[
:
] == self.rows[1]
async def test_fetchone_none_result(self):
assert (
await self.ch.fetchone("SELECT * FROM all_types WHERE uint8=42")
) is None
async def test_fetchval_none_result(self):
assert (
await self.ch.fetchval("SELECT uint8 FROM all_types WHERE uint8=42")
) is None
async def test_fetchval_none_result_with_params(self):
assert (
await self.ch.fetchval(
"SELECT uint8 FROM all_types WHERE uint8={u8}", params={"u8": 42}
)
) is None
async def test_fetch(self):
rows = await self.ch.fetch("SELECT * FROM all_types")
assert [row[:] for row in rows] == self.rows
async def test_cursor(self):
assert [
row[:] async for row in self.ch.cursor("SELECT * FROM all_types")
] == self.rows
async def test_iterate(self):
assert [
row[:] async for row in self.ch.iterate("SELECT * FROM all_types")
] == self.rows
async def test_select_with_execute(self):
assert (await self.ch.execute("SELECT * FROM all_types WHERE uint8=1")) is None
async def test_describe_with_fetch(self):
described_columns = await self.ch.fetch("DESCRIBE TABLE all_types", json=True)
assert described_columns is not None
assert 'type' in described_columns[0]
assert 'name' in described_columns[0]
async def test_show_tables_with_fetch(self):
tables = await self.ch.fetch("SHOW TABLES")
assert len(tables) == 3
assert tables[0]._row.decode() == 'all_types'
async def test_aggr_merge_tree(self):
avg_value = await self.ch.execute("SELECT avg(int32) FROM all_types")
avg_cache = await self.ch.execute("SELECT avgMerge(int32Cache) FROM test_cache")
assert avg_value == avg_cache
async def test_exists_table(self):
exists = await self.ch.fetchrow("EXISTS TABLE all_types")
assert exists == {'result': 1}
async def test_no_params(self):
"""It should be possible to have the aliases we want if we don't use any params"""
res = await self.ch.fetchrow('SELECT 1 AS "{not_a_param}" FROM all_types')
assert res["{not_a_param}"] == 1
@pytest.mark.record
@pytest.mark.usefixtures("class_chclient")
class TestRecord:
async def test_common_objects(self):
records = await self.ch.fetch("SELECT * FROM all_types")
assert id(records[0]._converters) == id(records[1]._converters)
assert id(records[0]._names) == id(records[1]._names)
async def test_lazy_decoding(self):
record = await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=2")
assert type(record._row) == bytes
record[0]
assert type(record._row) == tuple
assert type(record._row[0]) == int
async def test_mapping(self):
record = await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=2")
assert list(record.values())[0] == 2
assert list(record.keys())[0] == "uint8"
assert list(record.items())[0] == ("uint8", 2)
assert record.get("uint8") == 2
assert record.get(0) == 2
async def test_bool(self):
records = await self.ch.fetch(
"SELECT uniq(array_string) FROM all_types GROUP BY array_string WITH TOTALS"
)
assert bool(records[-2]) is False
async def test_len(self):
record = await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=2")
assert len(record) == len(self.rows[1])
async def test_index_error(self):
record = await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=2")
with pytest.raises(IndexError):
record[len(self.rows[1])]
records = await self.ch.fetch(
"SELECT uniq(array_string) FROM all_types GROUP BY array_string WITH TOTALS"
)
with pytest.raises(IndexError):
records[-2][0]
@pytest.mark.skip
async def test_empty_string(self):
await self.ch.execute("INSERT INTO all_types (uint8, string) VALUES", (6, ''))
result = await self.ch.fetch("SELECT string FROM all_types WHERE uint8=6")
assert len(result) == 1
record = result[0]
assert record['string'] == ''
async def test_key_error(self):
record = await self.ch.fetchrow("SELECT * FROM all_types WHERE uint8=2")
with pytest.raises(KeyError):
record["no_such_key"]
records = await self.ch.fetch(
"SELECT uniq(array_string) FROM all_types GROUP BY array_string WITH TOTALS"
)
with pytest.raises(KeyError):
records[-2]["a"]
@pytest.mark.usefixtures("class_chclient")
class TestJson:
async def test_json_insert_select(self):
sql = "INSERT INTO all_types FORMAT JSONEachRow"
records = [
{"decimal32": 32},
]
await self.ch.execute(sql, *records)
sql = "INSERT INTO all_types"
records = [
{"fixed_string": "simple string", "low_cardinality_str": "meow test"},
]
await self.ch.execute(sql, *records, json=True)
result = await self.ch.fetch(
"SELECT * FROM all_types WHERE decimal32 = 32 FORMAT JSONEachRow"
)
assert len(result) == 1
result = await self.ch.fetch(
"SELECT fixed_string, low_cardinality_str FROM all_types "
"WHERE low_cardinality_str = 'meow test'",
json=True,
)
assert result == [
{
"fixed_string": "simple string\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
"low_cardinality_str": "meow test",
}
]