forked from maximdanilchenko/aiochclient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
634 lines (545 loc) · 21.5 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
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"],
"'\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),
],
[
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,
],
]
@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(
"""
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),
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')
) ENGINE = Memory
"""
)
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][38] = 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 test_uint8(self):
assert await self.select_field("uint8") == 1
record = await self.select_record("uint8")
assert record[0] == 1
assert record["uint8"] == 1
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
async def test_array_date(self):
assert await self.select_field("array_date ") == [
dt.date(2018, 9, 21),
dt.date(2018, 9, 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),
]
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
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
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
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
async def test_low_cardinality_datetime(self):
assert await self.select_field("low_cardinality_datetime") == dt.datetime(
2018, 9, 21, 10, 32, 23
)
async def test_decimal(self):
assert await self.select_field("decimal") == Decimal('123.56')
async def test_decimal32(self):
assert await self.select_field("decimal32") == Decimal('1234.5678')
async def test_decimal64(self):
assert await self.select_field("decimal64") == Decimal('1234.56')
async def test_decimal128(self):
assert await self.select_field("decimal128") == Decimal('1234.56')
async def test_array_of_arrays(self):
assert await self.select_field("array_array_int") == [[1, 2, 3], [1, 2], [6, 7]]
async def test_ipv4(self):
assert await self.select_field("ipv4") == IPv4Address('116.253.40.133')
async def test_ipv6(self):
assert await self.select_field("ipv6") == IPv6Address(
'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
@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) == 1
assert tables[0]._row.decode() == 'all_types'
@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[42]
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},
{"fixed_string": "simple string", "low_cardinality_str": "meow test"},
]
await self.ch.execute(sql, *records)
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",
}
]