Skip to content

Commit

Permalink
Fix use of deprecated SQLAlchemy functionality in tests (#814)
Browse files Browse the repository at this point in the history
Fixes the following deprecation warning:
SADeprecationWarning: The FromClause.count() method is deprecated, and will be removed in a future release.   Please use the count function available from the func namespace.

References:
https://docs.sqlalchemy.org/en/13/core/metadata.html#sqlalchemy.schema.Table.count
https://docs.sqlalchemy.org/en/13/core/functions.html#sqlalchemy.sql.functions.count
  • Loading branch information
Nothing4You committed Jul 9, 2022
1 parent 572b5a3 commit 57d5b8e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
4 changes: 2 additions & 2 deletions tests/sa/test_sa_connection.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from unittest import mock

import pytest
from sqlalchemy import MetaData, Table, Column, Integer, String
from sqlalchemy import MetaData, Table, Column, Integer, String, func, select
from sqlalchemy.schema import DropTable, CreateTable
from sqlalchemy.sql.expression import bindparam

Expand Down Expand Up @@ -130,7 +130,7 @@ async def test_execute_sa_insert_positional_params(sa_connect):
@pytest.mark.run_loop
async def test_scalar(sa_connect):
conn = await sa_connect()
res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 1 == res


Expand Down
46 changes: 23 additions & 23 deletions tests/sa/test_sa_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from unittest import mock

import pytest
from sqlalchemy import MetaData, Table, Column, Integer, String
from sqlalchemy import MetaData, Table, Column, Integer, String, func, select

from aiomysql import sa

Expand Down Expand Up @@ -61,12 +61,12 @@ async def test_without_transactions(sa_connect):
await start(conn1)

conn2 = await sa_connect()
res1 = await conn1.scalar(tbl.count())
res1 = await conn1.scalar(select([func.count()]).select_from(tbl))
assert 1 == res1

await conn2.execute(tbl.delete())

res2 = await conn1.scalar(tbl.count())
res2 = await conn1.scalar(select([func.count()]).select_from(tbl))
assert 0 == res2
await conn1.close()
await conn2.close()
Expand All @@ -91,14 +91,14 @@ async def test_root_transaction(sa_connect):
assert tr.is_active
await conn1.execute(tbl.delete())

res1 = await conn2.scalar(tbl.count())
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res1

await tr.commit()

assert not tr.is_active
assert not conn1.in_transaction
res2 = await conn2.scalar(tbl.count())
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 0 == res2
await conn1.close()
await conn2.close()
Expand All @@ -114,13 +114,13 @@ async def test_root_transaction_rollback(sa_connect):
assert tr.is_active
await conn1.execute(tbl.delete())

res1 = await conn2.scalar(tbl.count())
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res1

await tr.rollback()

assert not tr.is_active
res2 = await conn2.scalar(tbl.count())
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res2
await conn1.close()
await conn2.close()
Expand All @@ -136,13 +136,13 @@ async def test_root_transaction_close(sa_connect):
assert tr.is_active
await conn1.execute(tbl.delete())

res1 = await conn2.scalar(tbl.count())
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res1

await tr.close()

assert not tr.is_active
res2 = await conn2.scalar(tbl.count())
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res2
await conn1.close()
await conn2.close()
Expand All @@ -157,12 +157,12 @@ async def test_rollback_on_connection_close(sa_connect):
tr = await conn1.begin()
await conn1.execute(tbl.delete())

res1 = await conn2.scalar(tbl.count())
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res1

await conn1.close()

res2 = await conn2.scalar(tbl.count())
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
assert 1 == res2
del tr
await conn1.close()
Expand Down Expand Up @@ -239,7 +239,7 @@ async def test_inner_transaction_rollback(sa_connect):
assert not tr2.is_active
assert not tr1.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 1 == res
await conn.close()

Expand All @@ -258,7 +258,7 @@ async def test_inner_transaction_close(sa_connect):
assert tr1.is_active
await tr1.commit()

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res
await conn.close()

Expand All @@ -277,14 +277,14 @@ async def test_nested_transaction_commit(sa_connect):
assert not tr2.is_active
assert tr1.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res

await tr1.commit()
assert not tr2.is_active
assert not tr1.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res
await conn.close()

Expand All @@ -305,7 +305,7 @@ async def test_nested_transaction_commit_twice(sa_connect):
assert not tr2.is_active
assert tr1.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res

await tr1.close()
Expand All @@ -326,14 +326,14 @@ async def test_nested_transaction_rollback(sa_connect):
assert not tr2.is_active
assert tr1.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 1 == res

await tr1.commit()
assert not tr2.is_active
assert not tr1.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 1 == res
await conn.close()

Expand All @@ -355,7 +355,7 @@ async def test_nested_transaction_rollback_twice(sa_connect):
assert tr1.is_active

await tr1.commit()
res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 1 == res
await conn.close()

Expand All @@ -374,7 +374,7 @@ async def test_twophase_transaction_commit(sa_connect):
await tr.commit()
assert not tr.is_active

res = await conn.scalar(tbl.count())
res = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res
await conn.close()

Expand Down Expand Up @@ -405,7 +405,7 @@ async def test_transactions_sequence(sa_connect):
tr1 = await conn.begin()
assert tr1 is conn._transaction
await conn.execute(tbl.insert().values(name='a'))
res1 = await conn.scalar(tbl.count())
res1 = await conn.scalar(select([func.count()]).select_from(tbl))
assert 1 == res1

await tr1.commit()
Expand All @@ -414,15 +414,15 @@ async def test_transactions_sequence(sa_connect):
tr2 = await conn.begin()
assert tr2 is conn._transaction
await conn.execute(tbl.insert().values(name='b'))
res2 = await conn.scalar(tbl.count())
res2 = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res2
await tr2.rollback()
assert conn._transaction is None

tr3 = await conn.begin()
assert tr3 is conn._transaction
await conn.execute(tbl.insert().values(name='b'))
res3 = await conn.scalar(tbl.count())
res3 = await conn.scalar(select([func.count()]).select_from(tbl))
assert 2 == res3
await tr3.commit()
assert conn._transaction is None
Expand Down
10 changes: 5 additions & 5 deletions tests/test_async_with.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pytest

from aiomysql import sa, create_pool, DictCursor, Cursor
from sqlalchemy import MetaData, Table, Column, Integer, String
from sqlalchemy import MetaData, Table, Column, Integer, String, func, select


meta = MetaData()
Expand Down Expand Up @@ -165,23 +165,23 @@ async def test_sa_connection(table, mysql_params, loop):
async def test_sa_transaction(table, mysql_params, loop):
async with sa.create_engine(loop=loop, **mysql_params) as engine:
async with engine.acquire() as connection:
cnt = await connection.scalar(tbl.count())
cnt = await connection.scalar(select([func.count()]).select_from(tbl))
assert 3 == cnt

async with (await connection.begin()) as tr:
assert tr.is_active
await connection.execute(tbl.delete())

assert not tr.is_active
cnt = await connection.scalar(tbl.count())
cnt = await connection.scalar(select([func.count()]).select_from(tbl))
assert 0 == cnt


@pytest.mark.run_loop
async def test_sa_transaction_rollback(loop, mysql_params, table):
async with sa.create_engine(loop=loop, **mysql_params) as engine:
async with engine.acquire() as conn:
cnt = await conn.scalar(tbl.count())
cnt = await conn.scalar(select([func.count()]).select_from(tbl))
assert 3 == cnt

with pytest.raises(RuntimeError) as ctx:
Expand All @@ -191,7 +191,7 @@ async def test_sa_transaction_rollback(loop, mysql_params, table):
raise RuntimeError("Exit")
assert str(ctx.value) == "Exit"
assert not tr.is_active
cnt = await conn.scalar(tbl.count())
cnt = await conn.scalar(select([func.count()]).select_from(tbl))
assert 3 == cnt


Expand Down

0 comments on commit 57d5b8e

Please sign in to comment.