-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtest_engine.py
429 lines (340 loc) · 13.5 KB
/
test_engine.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
import asyncio
import logging
from datetime import datetime
import asyncpg
from asyncpg.exceptions import InvalidCatalogNameError
from gino import UninitializedError
import pytest
from sqlalchemy.exc import ObjectNotExecutableError
import sqlalchemy as sa
from .models import db, User, PG_URL, qsize
pytestmark = pytest.mark.asyncio
async def test_basic(engine):
init_size = qsize(engine)
async with engine.acquire() as conn:
assert isinstance(conn.raw_connection, asyncpg.Connection)
assert init_size == qsize(engine)
assert isinstance(await engine.scalar('select now()'), datetime)
assert isinstance(await engine.scalar(sa.text('select now()')), datetime)
assert isinstance((await engine.first('select now()'))[0], datetime)
assert isinstance((await engine.all('select now()'))[0][0], datetime)
status, result = await engine.status('select now()')
assert status == 'SELECT 1'
assert isinstance(result[0][0], datetime)
with pytest.raises(ObjectNotExecutableError):
await engine.all(object())
async def test_issue_79():
import gino
e = await gino.create_engine(PG_URL + '_non_exist', min_size=0)
with pytest.raises(InvalidCatalogNameError):
async with e.acquire():
pass # pragma: no cover
# noinspection PyProtectedMember
assert len(e._ctx.get([])) == 0
async def test_reuse(engine):
init_size = qsize(engine)
async with engine.acquire(reuse=True) as conn1:
assert qsize(engine) == init_size - 1
async with engine.acquire(reuse=True) as conn2:
assert qsize(engine) == init_size - 1
assert conn1.raw_connection is conn2.raw_connection
assert await engine.scalar('select now()')
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size
async with engine.acquire(reuse=False) as conn1:
assert qsize(engine) == init_size - 1
async with engine.acquire(reuse=True) as conn2:
assert qsize(engine) == init_size - 1
assert conn1.raw_connection is conn2.raw_connection
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size
async with engine.acquire(reuse=True) as conn1:
assert qsize(engine) == init_size - 1
async with engine.acquire(reuse=False) as conn2:
assert qsize(engine) == init_size - 2
assert conn1.raw_connection is not conn2.raw_connection
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size
async with engine.acquire(reuse=False) as conn1:
assert qsize(engine) == init_size - 1
async with engine.acquire(reuse=False) as conn2:
assert qsize(engine) == init_size - 2
assert conn1.raw_connection is not conn2.raw_connection
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size
async with engine.acquire(reuse=False) as conn1:
assert qsize(engine) == init_size - 1
async with engine.acquire(reuse=True) as conn2:
assert qsize(engine) == init_size - 1
assert conn1.raw_connection is conn2.raw_connection
async with engine.acquire(reuse=False) as conn3:
assert qsize(engine) == init_size - 2
assert conn1.raw_connection is not conn3.raw_connection
async with engine.acquire(reuse=True) as conn4:
assert qsize(engine) == init_size - 2
assert conn3.raw_connection is conn4.raw_connection
assert qsize(engine) == init_size - 2
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size - 1
assert qsize(engine) == init_size
async def test_compile(engine):
stmt, params = engine.compile(User.query.where(User.id == 3))
assert params[0] == 3
async def test_logging(mocker):
import gino
mocker.patch('logging.Logger._log')
sql = 'SELECT NOW() AS test_logging'
e = await gino.create_engine(PG_URL, echo=False)
await e.scalar(sql)
await e.close()
# noinspection PyProtectedMember,PyUnresolvedReferences
logging.Logger._log.assert_not_called()
e = await gino.create_engine(PG_URL, echo=True)
await e.scalar(sql)
await e.close()
# noinspection PyProtectedMember,PyUnresolvedReferences
logging.Logger._log.assert_any_call(logging.INFO, sql, ())
async def test_set_isolation_level():
import gino
with pytest.raises(sa.exc.ArgumentError):
await gino.create_engine(PG_URL, isolation_level='non')
e = await gino.create_engine(PG_URL,
isolation_level='READ_UNCOMMITTED')
async with e.acquire() as conn:
assert await e.dialect.get_isolation_level(
conn.raw_connection) == 'READ UNCOMMITTED'
async with e.transaction(isolation='serializable') as tx:
assert await e.dialect.get_isolation_level(
tx.connection.raw_connection) == 'SERIALIZABLE'
async def test_too_many_engine_args():
import gino
with pytest.raises(TypeError):
await gino.create_engine(PG_URL, non_exist=None)
# noinspection PyUnusedLocal
async def test_scalar_return_none(bind):
assert await User.query.where(
User.nickname == 'nonexist').gino.scalar() is None
async def test_asyncpg_0120(bind, mocker):
# for asyncpg 0.12.0
assert await bind.first('rollback') is None
orig = getattr(asyncpg.Connection, '_do_execute')
class Stmt:
def __init__(self, stmt):
self._stmt = stmt
def _get_attributes(self):
raise TypeError
async def new(*args, **kwargs):
result, stmt = await orig(*args, **kwargs)
return result, Stmt(stmt)
mocker.patch('asyncpg.Connection._do_execute', new=new)
assert await bind.first('rollback') is None
async def test_asyncpg_0120_iterate(bind, mocker):
async with bind.transaction():
gen = await db.iterate('rollback')
assert await gen.next() is None
mocker.patch('asyncpg.prepared_stmt.'
'PreparedStatement.get_attributes').side_effect = TypeError
async with bind.transaction():
gen = await db.iterate('rollback')
assert await gen.next() is None
async def test_async_metadata():
import gino
db_ = await gino.Gino(PG_URL)
assert isinstance((await db_.scalar('select now()')), datetime)
await db_.pop_bind().close()
with pytest.raises(UninitializedError):
db.bind.first()
# noinspection PyUnreachableCode
async def test_acquire_timeout():
import gino
e = await gino.create_engine(PG_URL, min_size=1, max_size=1)
async with e.acquire():
with pytest.raises(asyncio.TimeoutError):
async with e.acquire(timeout=0.1):
assert False, 'Should not reach here'
loop = asyncio.get_event_loop()
f1 = loop.create_future()
async def first():
async with e.acquire() as conn:
f1.set_result(None)
await asyncio.sleep(0.2)
# noinspection PyProtectedMember
return conn.raw_connection._con
async def second():
async with e.acquire(lazy=True) as conn:
conn = conn.execution_options(timeout=0.1)
with pytest.raises(asyncio.TimeoutError):
assert await conn.scalar('select 1')
async def third():
async with e.acquire(reuse=True, timeout=0.4) as conn:
# noinspection PyProtectedMember
return conn.raw_connection._con
t1 = loop.create_task(first())
await f1
loop.create_task(second())
t3 = loop.create_task(third())
assert await t1 is await t3
# noinspection PyProtectedMember
async def test_lazy(mocker):
import gino
engine = await gino.create_engine(PG_URL, min_size=1, max_size=1)
init_size = qsize(engine)
async with engine.acquire(lazy=True):
assert qsize(engine) == init_size
assert len(engine._ctx.get()) == 1
assert engine._ctx.get() is None
assert qsize(engine) == init_size
async with engine.acquire(lazy=True):
assert qsize(engine) == init_size
assert len(engine._ctx.get()) == 1
assert await engine.scalar('select 1')
assert qsize(engine) == init_size - 1
assert len(engine._ctx.get()) == 1
assert engine._ctx.get() is None
assert qsize(engine) == init_size
loop = asyncio.get_event_loop()
fut = loop.create_future()
async def block():
async with engine.acquire():
fut.set_result(None)
await asyncio.sleep(0.3)
blocker = loop.create_task(block())
await fut
init_size_2 = qsize(engine)
ctx = engine.acquire(lazy=True)
conn = await ctx.__aenter__()
t1 = loop.create_task(
conn.execution_options(timeout=0.1).scalar('select 1'))
t2 = loop.create_task(ctx.__aexit__(None, None, None))
with pytest.raises(asyncio.TimeoutError):
await t1
assert not await t2
assert qsize(engine) == init_size_2
await blocker
assert qsize(engine) == init_size
fut = loop.create_future()
blocker = loop.create_task(block())
await fut
init_size_2 = qsize(engine)
async def acquire_failed(*args, **kwargs):
await asyncio.sleep(0.1)
raise ValueError()
mocker.patch('asyncpg.pool.Pool.acquire', new=acquire_failed)
ctx = engine.acquire(lazy=True)
conn = await ctx.__aenter__()
t1 = loop.create_task(conn.scalar('select 1'))
t2 = loop.create_task(conn.release(permanent=False))
with pytest.raises(ValueError):
await t1
assert not await t2
assert qsize(engine) == init_size_2
await conn.release(permanent=False)
assert qsize(engine) == init_size_2
await blocker
assert qsize(engine) == init_size
async def test_release(engine):
init_size = qsize(engine)
async with engine.acquire() as conn:
assert await conn.scalar('select 8') == 8
await conn.release(permanent=False)
assert await conn.scalar('select 8') == 8
await conn.release(permanent=False)
with pytest.raises(ValueError, match='released permanently'):
await conn.scalar('select 8')
with pytest.raises(ValueError, match='already released'):
await conn.release()
conn = await engine.acquire()
assert await conn.scalar('select 8') == 8
await conn.release(permanent=False)
assert await conn.scalar('select 8') == 8
await conn.release()
with pytest.raises(ValueError, match='released permanently'):
await conn.scalar('select 8')
conn1 = await engine.acquire()
conn2 = await engine.acquire(reuse=True)
conn3 = await engine.acquire()
conn4 = await engine.acquire(reuse=True)
assert await conn1.scalar('select 8') == 8
assert await conn2.scalar('select 8') == 8
assert await conn3.scalar('select 8') == 8
assert await conn4.scalar('select 8') == 8
await conn1.release(permanent=False)
assert await conn2.scalar('select 8') == 8
await conn2.release(permanent=False)
assert await conn2.scalar('select 8') == 8
await conn1.release()
with pytest.raises(ValueError, match='released permanently'):
await conn2.scalar('select 8')
assert await conn4.scalar('select 8') == 8
await conn4.release()
with pytest.raises(ValueError, match='released permanently'):
await conn4.scalar('select 8')
assert await conn3.scalar('select 8') == 8
await conn3.release(permanent=False)
assert await conn3.scalar('select 8') == 8
assert init_size - 1 == qsize(engine)
await conn3.release(permanent=False)
assert init_size == qsize(engine)
await conn3.release()
assert init_size == qsize(engine)
conn1 = await engine.acquire()
conn2 = await engine.acquire()
conn3 = await engine.acquire()
assert engine.current_connection is conn3
await conn2.release()
assert engine.current_connection is conn3
await conn1.release()
assert engine.current_connection is conn3
await conn3.release()
assert engine.current_connection is None
assert init_size == qsize(engine)
async def test_ssl():
import ssl
import gino
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
e = await gino.create_engine(PG_URL, ssl=ctx)
await e.close()
async def test_issue_313(bind):
assert bind._ctx.get() is None
async with db.acquire():
pass
assert bind._ctx.get() is None
async def task():
async with db.acquire(reuse=True):
await db.scalar('SELECT now()')
await asyncio.gather(*[task() for _ in range(5)])
assert bind._ctx.get() is None
async def task():
async with db.transaction():
await db.scalar('SELECT now()')
await asyncio.gather(*[task() for _ in range(5)])
assert bind._ctx.get() is None
async def test_issue_412(bind):
sql = 'SELECT now()'
@sa.event.listens_for(bind._sa_engine, 'after_execute')
def after_exec(conn, clauseelement, multiparams, params, result):
nonlocal rowcount
rowcount = result.rowcount
for i in range(4):
rowcount = None
await bind.all(sql)
assert rowcount == 1
rowcount = None
await bind.first(sql)
assert rowcount == 0
rowcount = None
await bind.all(sql, [(), ()])
assert rowcount == 0
async with bind.transaction() as tx:
rowcount = None
async for _ in bind.iterate(sql):
assert rowcount == 0
rowcount = None
stmt = await tx.connection.prepare(sql)
assert rowcount == 0
rowcount = None
await stmt.all()
assert rowcount is None