-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathtest_cursors.py
More file actions
327 lines (279 loc) · 9.56 KB
/
Copy pathtest_cursors.py
File metadata and controls
327 lines (279 loc) · 9.56 KB
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
from typing import Any
import pytest
from asynch.connection import Connection
from asynch.cursors import DictCursor
from asynch.errors import TypeMismatchError
from asynch.proto import constants
@pytest.mark.asyncio
async def test_dict_cursor_repr(conn):
repstr = "<DictCursor(connection={conn}, echo={echo}) object at 0x{cid:x};"
echo = True
async with conn.cursor(cursor=DictCursor, echo=echo) as cursor:
repstr = repstr.format(conn=conn, echo=echo, cid=id(cursor))
repstr = repstr + " status: {status}>"
assert repr(cursor) == repstr.format(status="ready")
await cursor.execute("SELECT 1")
assert repr(cursor) == repstr.format(status="finished")
ret = await cursor.fetchone()
assert ret == {"1": 1}
assert repr(cursor) == repstr.format(status="closed")
@pytest.mark.asyncio
@pytest.mark.parametrize(
("stmt", "answer"),
[
("SELECT 42", [{"42": 42}]),
("SELECT -21 WHERE 1 != 1", []),
],
)
async def test_cursor_async_for(
stmt: str,
answer: list[dict[str, Any]],
conn: Connection,
):
result: list[dict[str, Any]] = []
async with conn:
async with conn.cursor(cursor=DictCursor) as cursor:
cursor.set_stream_results(stream_results=True, max_row_buffer=1000)
await cursor.execute(stmt)
result = [row async for row in cursor]
assert result == answer
@pytest.mark.asyncio
async def test_fetchone(conn: Connection):
async with conn.cursor() as cursor:
await cursor.execute("SELECT 1")
ret = await cursor.fetchone()
assert ret == (1,)
await cursor.execute("SELECT {val}", args={"val": 2})
ret = await cursor.fetchone()
assert ret == (2,)
await cursor.execute("SELECT * FROM system.tables")
ret = await cursor.fetchall()
assert isinstance(ret, list)
@pytest.mark.asyncio
async def test_fetchall(conn: Connection):
async with conn.cursor() as cursor:
await cursor.execute("SELECT 1")
ret = await cursor.fetchall()
assert ret == [(1,)]
await cursor.execute("SELECT {val}", args={"val": 2})
ret = await cursor.fetchall()
assert ret == [(2,)]
@pytest.mark.asyncio
async def test_dict_cursor(conn: Connection):
async with conn.cursor(cursor=DictCursor) as cursor:
await cursor.execute("SELECT 1")
ret = await cursor.fetchall()
assert ret == [{"1": 1}]
await cursor.execute("SELECT {val}", args={"val": 2})
ret = await cursor.fetchall()
assert ret == [{"2": 2}]
@pytest.mark.asyncio
async def test_insert_dict(conn: Connection):
async with conn.cursor(cursor=DictCursor) as cursor:
rows = await cursor.execute(
"""INSERT INTO test.asynch(id,decimal,date,datetime,float,uuid,string,ipv4,ipv6,bool) VALUES""",
[
{
"id": 1,
"decimal": 1,
"date": "2020-08-08",
"datetime": "2020-08-08 00:00:00",
"float": 1,
"uuid": "59e182c4-545d-4f30-8b32-cefea2d0d5ba",
"string": "1",
"ipv4": "0.0.0.0",
"ipv6": "::",
"bool": True,
}
],
)
assert rows == 1
@pytest.mark.asyncio
async def test_nullable_insert_dict(conn: Connection):
async with conn.cursor(cursor=DictCursor) as cursor:
rows = await cursor.execute(
"""INSERT INTO test.asynch_nullable("""
"""id,cnt,decimal,date,datetime,float,uuid,string,ipv4,ipv6,bool) VALUES""",
[
{
"id": 1,
"cnt": None,
"decimal": None,
"date": None,
"datetime": None,
"float": None,
"uuid": None,
"string": None,
"ipv4": None,
"ipv6": None,
"bool": None,
}
],
)
assert rows == 1
await cursor.execute("SELECT * FROM test.asynch_nullable")
result = await cursor.fetchone()
del result["id"]
assert len([item for item in result.values() if item is not None]) == 0
@pytest.mark.asyncio
async def test_nullable_in_non_nullable_insert_dict(conn: Connection):
async with conn.cursor(cursor=DictCursor) as cursor:
try:
await cursor.execute(
"""INSERT INTO test.asynch(id,string) VALUES""",
[
{
"id": 1,
"string": None,
}
],
)
assert False
except TypeMismatchError:
assert True
try:
await cursor.execute(
"""INSERT INTO test.asynch(id,decimal) VALUES""",
[
{
"id": 1,
"decimal": None,
}
],
)
assert False
except TypeMismatchError:
assert True
try:
await cursor.execute(
"""INSERT INTO test.asynch(id,float) VALUES""",
[
{
"id": 1,
"float": None,
}
],
)
assert False
except TypeMismatchError:
assert True
@pytest.mark.asyncio
async def test_insert_tuple(conn: Connection):
async with conn.cursor(cursor=DictCursor) as cursor:
rows = await cursor.execute(
"""INSERT INTO test.asynch(id,decimal,date,datetime,float,uuid,string,ipv4,ipv6,bool) VALUES""",
[
(
1,
1,
"2020-08-08",
"2020-08-08 00:00:00",
1,
"59e182c4-545d-4f30-8b32-cefea2d0d5ba",
"1",
"0.0.0.0",
"::",
True,
)
],
)
assert rows == 1
@pytest.mark.asyncio
async def test_executemany(conn: Connection):
async with conn.cursor(cursor=DictCursor) as cursor:
rows = await cursor.executemany(
"""INSERT INTO test.asynch(id,decimal,date,datetime,float,uuid,string,ipv4,ipv6,bool) VALUES""",
[
(
1,
1,
"2020-08-08",
"2020-08-08 00:00:00",
1,
"59e182c4-545d-4f30-8b32-cefea2d0d5ba",
"1",
"0.0.0.0",
"::",
True,
),
(
1,
1,
"2020-08-08",
"2020-08-08 00:00:00",
1,
"59e182c4-545d-4f30-8b32-cefea2d0d5ba",
"1",
"0.0.0.0",
"::",
True,
),
],
)
assert rows == 2
@pytest.mark.asyncio
async def test_table_ddl(conn: Connection):
async with conn.cursor() as cursor:
await cursor.execute("drop table if exists test.alter_table")
create_table_sql = """
CREATE TABLE test.alter_table
(
`id` Int32
)
ENGINE = MergeTree
ORDER BY id
"""
await cursor.execute(create_table_sql)
add_column_sql = """alter table test.alter_table add column c String"""
await cursor.execute(add_column_sql)
show_table_sql = """show create table test.alter_table"""
await cursor.execute(show_table_sql)
assert await cursor.fetchone() == (
"CREATE TABLE test.alter_table\n(\n `id` Int32,\n `c` String\n)\nENGINE = MergeTree\nORDER BY id\nSETTINGS index_granularity = 8192",
)
await cursor.execute("drop table test.alter_table")
@pytest.mark.asyncio
async def test_insert_buffer_overflow(conn: Connection):
old_buffer_size = constants.BUFFER_SIZE
constants.BUFFER_SIZE = 2**6 + 1
async with conn.cursor() as cursor:
await cursor.execute("DROP TABLE if exists test.test")
create_table_sql = """CREATE TABLE test.test
(
`i` Int32,
`c1` String,
`c2` String,
`c3` String,
`c4` String
) ENGINE = MergeTree ORDER BY i"""
await cursor.execute(create_table_sql)
await cursor.execute("INSERT INTO test.test VALUES", [(1, "t", "t", "t", "t")])
await cursor.execute("DROP TABLE if exists test.test")
constants.BUFFER_SIZE = old_buffer_size
@pytest.mark.asyncio
@pytest.mark.parametrize(
"size, expected_size, with_select",
[
[0, 0, True],
[10, 10, True],
[100, 0, False],
],
ids=[
"empty",
"10 elements",
"without select",
],
)
async def test_cursror_iter(conn, size, expected_size, with_select):
async with conn.cursor() as cursor:
await cursor.execute("DROP TABLE IF EXISTS test.test")
await cursor.execute("CREATE TABLE test.test (a UInt8) ENGINE=Memory")
data = [(v,) for v in range(size)]
await cursor.execute("INSERT INTO test.test (a) VALUES", data)
if with_select:
await cursor.execute("SELECT * FROM test.test")
index = 0
async for one in cursor:
assert one == data[index]
index += 1
assert expected_size == index