Skip to content

Commit

Permalink
Fix tests to pass under Python 3.7 and 3.8
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz authored Jun 1, 2020
2 parents f9b86aa + e4cba8f commit d842ec3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: python

python:
- 3.6
- 3.7
- 3.8

env:
matrix:
Expand Down
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
uvloop = None


@pytest.fixture
def disable_gc():
gc_enabled = gc.isenabled()
if gc_enabled:
gc.disable()
gc.collect()
yield
if gc_enabled:
gc.collect()
gc.enable()


@pytest.fixture(scope='session')
def unused_port():
def f():
Expand Down
33 changes: 20 additions & 13 deletions tests/test_async_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,11 @@ async def test_create_pool_deprecations(mysql_params, loop):
warnings.simplefilter("always")
async with pool.get() as conn:
pass
assert issubclass(w[-1].category, DeprecationWarning)
# The first warning emitted is expected to be DeprecationWarning:
# in the past, we used to check for the last one but this assumption
# breaks under Python 3.7 that also emits a `ResourceWarning` when
# executed with `PYTHONASYNCIODEBUG=1`.
assert issubclass(w[0].category, DeprecationWarning)
assert conn.closed

async with create_pool(loop=loop, **mysql_params) as pool:
Expand All @@ -149,9 +153,10 @@ async def test_sa_connection(table, mysql_params, loop):
connection = await engine.acquire()
assert not connection.closed
async with connection:
ret = []
async for i in connection.execute(tbl.select()):
ret.append(i)
async with connection.execute(tbl.select()) as cursor:
ret = []
async for i in cursor:
ret.append(i)
assert [(1, 'a'), (2, 'b'), (3, 'c')] == ret
assert connection.closed

Expand Down Expand Up @@ -194,21 +199,23 @@ async def test_sa_transaction_rollback(loop, mysql_params, table):
async def test_create_engine(loop, mysql_params, table):
async with sa.create_engine(loop=loop, **mysql_params) as engine:
async with engine.acquire() as conn:
ret = []
async for i in conn.execute(tbl.select()):
ret.append(i)
assert [(1, 'a'), (2, 'b'), (3, 'c')] == ret
async with conn.execute(tbl.select()) as cursor:
ret = []
async for i in cursor:
ret.append(i)
assert [(1, 'a'), (2, 'b'), (3, 'c')] == ret


@pytest.mark.run_loop
async def test_engine(loop, mysql_params, table):
engine = await sa.create_engine(loop=loop, **mysql_params)
async with engine:
async with engine.acquire() as conn:
ret = []
async for i in conn.execute(tbl.select()):
ret.append(i)
assert [(1, 'a'), (2, 'b'), (3, 'c')] == ret
async with conn.execute(tbl.select()) as cursor:
ret = []
async for i in cursor:
ret.append(i)
assert [(1, 'a'), (2, 'b'), (3, 'c')] == ret


@pytest.mark.run_loop
Expand All @@ -218,7 +225,7 @@ async def test_transaction_context_manager(loop, mysql_params, table):
async with conn.begin() as tr:
async with conn.execute(tbl.select()) as cursor:
ret = []
async for i in conn.execute(tbl.select()):
async for i in cursor:
ret.append(i)
assert [(1, 'a'), (2, 'b'), (3, 'c')] == ret
assert cursor.closed
Expand Down
1 change: 1 addition & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ async def test_connection_double_ensure_closed(connection_creator):


@pytest.mark.run_loop
@pytest.mark.usefixtures("disable_gc")
async def test___del__(connection_creator):
conn = await connection_creator()
with pytest.warns(ResourceWarning):
Expand Down

0 comments on commit d842ec3

Please sign in to comment.