-
Notifications
You must be signed in to change notification settings - Fork 415
/
Copy pathtest_execute.py
349 lines (292 loc) · 11.9 KB
/
test_execute.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
# Copyright (C) 2016-present the asyncpg authors and contributors
# <see AUTHORS file>
#
# This module is part of asyncpg and is released under
# the Apache 2.0 License: http://www.apache.org/licenses/LICENSE-2.0
import asyncio
import asyncpg
from asyncpg import _testbase as tb
from asyncpg import exceptions
class TestExecuteScript(tb.ConnectedTestCase):
async def test_execute_script_1(self):
self.assertEqual(self.con._protocol.queries_count, 0)
status = await self.con.execute('''
SELECT 1;
SELECT true FROM pg_type WHERE false = true;
SELECT generate_series(0, 9);
''')
self.assertEqual(self.con._protocol.queries_count, 1)
self.assertEqual(status, 'SELECT 10')
async def test_execute_script_2(self):
status = await self.con.execute('''
CREATE TABLE mytab (a int);
''')
self.assertEqual(status, 'CREATE TABLE')
try:
status = await self.con.execute('''
INSERT INTO mytab (a) VALUES ($1), ($2)
''', 10, 20)
self.assertEqual(status, 'INSERT 0 2')
finally:
await self.con.execute('DROP TABLE mytab')
async def test_execute_script_3(self):
with self.assertRaisesRegex(asyncpg.PostgresSyntaxError,
'cannot insert multiple commands'):
await self.con.execute('''
CREATE TABLE mytab (a int);
INSERT INTO mytab (a) VALUES ($1), ($2);
''', 10, 20)
async def test_execute_script_check_transactionality(self):
with self.assertRaises(asyncpg.PostgresError):
await self.con.execute('''
CREATE TABLE mytab (a int);
SELECT * FROM mytab WHERE 1 / 0 = 1;
''')
with self.assertRaisesRegex(asyncpg.PostgresError,
'"mytab" does not exist'):
await self.con.prepare('''
SELECT * FROM mytab
''')
async def test_execute_exceptions_1(self):
with self.assertRaisesRegex(asyncpg.PostgresError,
'relation "__dne__" does not exist'):
await self.con.execute('select * from __dne__')
async def test_execute_script_interrupted_close(self):
fut = self.loop.create_task(
self.con.execute('''SELECT pg_sleep(10)'''))
await asyncio.sleep(0.2)
self.assertFalse(self.con.is_closed())
await self.con.close()
self.assertTrue(self.con.is_closed())
with self.assertRaises(asyncpg.QueryCanceledError):
await fut
async def test_execute_script_interrupted_terminate(self):
fut = self.loop.create_task(
self.con.execute('''SELECT pg_sleep(10)'''))
await asyncio.sleep(0.2)
self.assertFalse(self.con.is_closed())
self.con.terminate()
self.assertTrue(self.con.is_closed())
with self.assertRaisesRegex(asyncpg.ConnectionDoesNotExistError,
'closed in the middle'):
await fut
self.con.terminate()
class TestExecuteMany(tb.ConnectedTestCase):
def setUp(self):
super().setUp()
self.loop.run_until_complete(self.con.execute(
'CREATE TABLE exmany (a text, b int PRIMARY KEY)'))
def tearDown(self):
self.loop.run_until_complete(self.con.execute('DROP TABLE exmany'))
super().tearDown()
async def test_executemany_basic(self):
result = await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
self.assertIsNone(result)
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
# Empty set
await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', ())
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
async def test_executemany_returning(self):
result = await self.con.fetchmany('''
INSERT INTO exmany VALUES($1, $2) RETURNING a, b
''', [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
# Empty set
await self.con.fetchmany('''
INSERT INTO exmany VALUES($1, $2) RETURNING a, b
''', ())
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
# Without "RETURNING"
result = await self.con.fetchmany('''
INSERT INTO exmany VALUES($1, $2)
''', [('e', 5), ('f', 6)])
self.assertEqual(result, [])
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5), ('f', 6)
])
async def test_executemany_bad_input(self):
with self.assertRaisesRegex(
exceptions.DataError,
r"invalid input in executemany\(\) argument sequence element #1: "
r"expected a sequence",
):
await self.con.executemany('''
INSERT INTO exmany (b) VALUES($1)
''', [(0,), {1: 0}])
with self.assertRaisesRegex(
exceptions.DataError,
r"invalid input for query argument \$1 in element #1 of "
r"executemany\(\) sequence: 'bad'",
):
await self.con.executemany('''
INSERT INTO exmany (b) VALUES($1)
''', [(0,), ("bad",)])
async def test_executemany_error_in_input_gen(self):
bad_data = ([1 / 0] for v in range(10))
with self.assertRaises(ZeroDivisionError):
async with self.con.transaction():
await self.con.executemany('''
INSERT INTO exmany (b)VALUES($1)
''', bad_data)
good_data = ([v] for v in range(10))
async with self.con.transaction():
await self.con.executemany('''
INSERT INTO exmany (b)VALUES($1)
''', good_data)
async def test_executemany_server_failure(self):
with self.assertRaises(exceptions.UniqueViolationError):
await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', [
('a', 1), ('b', 2), ('c', 2), ('d', 4)
])
result = await self.con.fetch('SELECT * FROM exmany')
self.assertEqual(result, [])
async def test_executemany_server_failure_after_writes(self):
with self.assertRaises(exceptions.UniqueViolationError):
await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', [('a' * 32768, x) for x in range(10)] + [
('b', 12), ('c', 12), ('d', 14)
])
result = await self.con.fetch('SELECT b FROM exmany')
self.assertEqual(result, [])
async def test_executemany_server_failure_during_writes(self):
# failure at the beginning, server error detected in the middle
pos = 0
def gen():
nonlocal pos
while pos < 128:
pos += 1
if pos < 3:
yield ('a', 0)
else:
yield 'a' * 32768, pos
with self.assertRaises(exceptions.UniqueViolationError):
await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', gen())
result = await self.con.fetch('SELECT b FROM exmany')
self.assertEqual(result, [])
self.assertLess(pos, 128, 'should stop early')
async def test_executemany_client_failure_after_writes(self):
with self.assertRaises(ZeroDivisionError):
await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', (('a' * 32768, y + y / y) for y in range(10, -1, -1)))
result = await self.con.fetch('SELECT b FROM exmany')
self.assertEqual(result, [])
async def test_executemany_timeout(self):
with self.assertRaises(asyncio.TimeoutError):
await self.con.executemany('''
INSERT INTO exmany VALUES(pg_sleep(0.1) || $1, $2)
''', [('a' * 32768, x) for x in range(128)], timeout=0.5)
result = await self.con.fetch('SELECT * FROM exmany')
self.assertEqual(result, [])
async def test_executemany_timeout_flow_control(self):
event = asyncio.Event()
async def locker():
test_func = getattr(self, self._testMethodName).__func__
opts = getattr(test_func, '__connect_options__', {})
conn = await self.connect(**opts)
try:
tx = conn.transaction()
await tx.start()
await conn.execute("UPDATE exmany SET a = '1' WHERE b = 10")
event.set()
await asyncio.sleep(1)
await tx.rollback()
finally:
event.set()
await conn.close()
await self.con.executemany('''
INSERT INTO exmany VALUES(NULL, $1)
''', [(x,) for x in range(128)])
fut = asyncio.ensure_future(locker())
await event.wait()
with self.assertRaises(asyncio.TimeoutError):
await self.con.executemany('''
UPDATE exmany SET a = $1 WHERE b = $2
''', [('a' * 32768, x) for x in range(128)], timeout=0.5)
await fut
result = await self.con.fetch(
'SELECT * FROM exmany WHERE a IS NOT NULL')
self.assertEqual(result, [])
async def test_executemany_client_failure_in_transaction(self):
tx = self.con.transaction()
await tx.start()
with self.assertRaises(ZeroDivisionError):
await self.con.executemany('''
INSERT INTO exmany VALUES($1, $2)
''', (('a' * 32768, y + y / y) for y in range(10, -1, -1)))
result = await self.con.fetch('SELECT b FROM exmany')
# only 2 batches executed (2 x 4)
self.assertEqual(
[x[0] for x in result], [y + 1 for y in range(10, 2, -1)])
await tx.rollback()
result = await self.con.fetch('SELECT b FROM exmany')
self.assertEqual(result, [])
async def test_executemany_client_server_failure_conflict(self):
self.con._transport.set_write_buffer_limits(65536 * 64, 16384 * 64)
with self.assertRaises(exceptions.UniqueViolationError):
await self.con.executemany('''
INSERT INTO exmany VALUES($1, 0)
''', (('a' * 32768,) for y in range(4, -1, -1) if y / y))
result = await self.con.fetch('SELECT b FROM exmany')
self.assertEqual(result, [])
async def test_executemany_prepare(self):
stmt = await self.con.prepare('''
INSERT INTO exmany VALUES($1, $2)
''')
result = await stmt.executemany([
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
self.assertIsNone(result)
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])
# Empty set
await stmt.executemany(())
result = await self.con.fetch('''
SELECT * FROM exmany
''')
self.assertEqual(result, [
('a', 1), ('b', 2), ('c', 3), ('d', 4)
])