Skip to content

Commit 57d5b8e

Browse files
authored
Fix use of deprecated SQLAlchemy functionality in tests (#814)
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
1 parent 572b5a3 commit 57d5b8e

File tree

3 files changed

+30
-30
lines changed

3 files changed

+30
-30
lines changed

tests/sa/test_sa_connection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import mock
22

33
import pytest
4-
from sqlalchemy import MetaData, Table, Column, Integer, String
4+
from sqlalchemy import MetaData, Table, Column, Integer, String, func, select
55
from sqlalchemy.schema import DropTable, CreateTable
66
from sqlalchemy.sql.expression import bindparam
77

@@ -130,7 +130,7 @@ async def test_execute_sa_insert_positional_params(sa_connect):
130130
@pytest.mark.run_loop
131131
async def test_scalar(sa_connect):
132132
conn = await sa_connect()
133-
res = await conn.scalar(tbl.count())
133+
res = await conn.scalar(select([func.count()]).select_from(tbl))
134134
assert 1 == res
135135

136136

tests/sa/test_sa_transaction.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from unittest import mock
44

55
import pytest
6-
from sqlalchemy import MetaData, Table, Column, Integer, String
6+
from sqlalchemy import MetaData, Table, Column, Integer, String, func, select
77

88
from aiomysql import sa
99

@@ -61,12 +61,12 @@ async def test_without_transactions(sa_connect):
6161
await start(conn1)
6262

6363
conn2 = await sa_connect()
64-
res1 = await conn1.scalar(tbl.count())
64+
res1 = await conn1.scalar(select([func.count()]).select_from(tbl))
6565
assert 1 == res1
6666

6767
await conn2.execute(tbl.delete())
6868

69-
res2 = await conn1.scalar(tbl.count())
69+
res2 = await conn1.scalar(select([func.count()]).select_from(tbl))
7070
assert 0 == res2
7171
await conn1.close()
7272
await conn2.close()
@@ -91,14 +91,14 @@ async def test_root_transaction(sa_connect):
9191
assert tr.is_active
9292
await conn1.execute(tbl.delete())
9393

94-
res1 = await conn2.scalar(tbl.count())
94+
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
9595
assert 1 == res1
9696

9797
await tr.commit()
9898

9999
assert not tr.is_active
100100
assert not conn1.in_transaction
101-
res2 = await conn2.scalar(tbl.count())
101+
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
102102
assert 0 == res2
103103
await conn1.close()
104104
await conn2.close()
@@ -114,13 +114,13 @@ async def test_root_transaction_rollback(sa_connect):
114114
assert tr.is_active
115115
await conn1.execute(tbl.delete())
116116

117-
res1 = await conn2.scalar(tbl.count())
117+
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
118118
assert 1 == res1
119119

120120
await tr.rollback()
121121

122122
assert not tr.is_active
123-
res2 = await conn2.scalar(tbl.count())
123+
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
124124
assert 1 == res2
125125
await conn1.close()
126126
await conn2.close()
@@ -136,13 +136,13 @@ async def test_root_transaction_close(sa_connect):
136136
assert tr.is_active
137137
await conn1.execute(tbl.delete())
138138

139-
res1 = await conn2.scalar(tbl.count())
139+
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
140140
assert 1 == res1
141141

142142
await tr.close()
143143

144144
assert not tr.is_active
145-
res2 = await conn2.scalar(tbl.count())
145+
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
146146
assert 1 == res2
147147
await conn1.close()
148148
await conn2.close()
@@ -157,12 +157,12 @@ async def test_rollback_on_connection_close(sa_connect):
157157
tr = await conn1.begin()
158158
await conn1.execute(tbl.delete())
159159

160-
res1 = await conn2.scalar(tbl.count())
160+
res1 = await conn2.scalar(select([func.count()]).select_from(tbl))
161161
assert 1 == res1
162162

163163
await conn1.close()
164164

165-
res2 = await conn2.scalar(tbl.count())
165+
res2 = await conn2.scalar(select([func.count()]).select_from(tbl))
166166
assert 1 == res2
167167
del tr
168168
await conn1.close()
@@ -239,7 +239,7 @@ async def test_inner_transaction_rollback(sa_connect):
239239
assert not tr2.is_active
240240
assert not tr1.is_active
241241

242-
res = await conn.scalar(tbl.count())
242+
res = await conn.scalar(select([func.count()]).select_from(tbl))
243243
assert 1 == res
244244
await conn.close()
245245

@@ -258,7 +258,7 @@ async def test_inner_transaction_close(sa_connect):
258258
assert tr1.is_active
259259
await tr1.commit()
260260

261-
res = await conn.scalar(tbl.count())
261+
res = await conn.scalar(select([func.count()]).select_from(tbl))
262262
assert 2 == res
263263
await conn.close()
264264

@@ -277,14 +277,14 @@ async def test_nested_transaction_commit(sa_connect):
277277
assert not tr2.is_active
278278
assert tr1.is_active
279279

280-
res = await conn.scalar(tbl.count())
280+
res = await conn.scalar(select([func.count()]).select_from(tbl))
281281
assert 2 == res
282282

283283
await tr1.commit()
284284
assert not tr2.is_active
285285
assert not tr1.is_active
286286

287-
res = await conn.scalar(tbl.count())
287+
res = await conn.scalar(select([func.count()]).select_from(tbl))
288288
assert 2 == res
289289
await conn.close()
290290

@@ -305,7 +305,7 @@ async def test_nested_transaction_commit_twice(sa_connect):
305305
assert not tr2.is_active
306306
assert tr1.is_active
307307

308-
res = await conn.scalar(tbl.count())
308+
res = await conn.scalar(select([func.count()]).select_from(tbl))
309309
assert 2 == res
310310

311311
await tr1.close()
@@ -326,14 +326,14 @@ async def test_nested_transaction_rollback(sa_connect):
326326
assert not tr2.is_active
327327
assert tr1.is_active
328328

329-
res = await conn.scalar(tbl.count())
329+
res = await conn.scalar(select([func.count()]).select_from(tbl))
330330
assert 1 == res
331331

332332
await tr1.commit()
333333
assert not tr2.is_active
334334
assert not tr1.is_active
335335

336-
res = await conn.scalar(tbl.count())
336+
res = await conn.scalar(select([func.count()]).select_from(tbl))
337337
assert 1 == res
338338
await conn.close()
339339

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

357357
await tr1.commit()
358-
res = await conn.scalar(tbl.count())
358+
res = await conn.scalar(select([func.count()]).select_from(tbl))
359359
assert 1 == res
360360
await conn.close()
361361

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

377-
res = await conn.scalar(tbl.count())
377+
res = await conn.scalar(select([func.count()]).select_from(tbl))
378378
assert 2 == res
379379
await conn.close()
380380

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

411411
await tr1.commit()
@@ -414,15 +414,15 @@ async def test_transactions_sequence(sa_connect):
414414
tr2 = await conn.begin()
415415
assert tr2 is conn._transaction
416416
await conn.execute(tbl.insert().values(name='b'))
417-
res2 = await conn.scalar(tbl.count())
417+
res2 = await conn.scalar(select([func.count()]).select_from(tbl))
418418
assert 2 == res2
419419
await tr2.rollback()
420420
assert conn._transaction is None
421421

422422
tr3 = await conn.begin()
423423
assert tr3 is conn._transaction
424424
await conn.execute(tbl.insert().values(name='b'))
425-
res3 = await conn.scalar(tbl.count())
425+
res3 = await conn.scalar(select([func.count()]).select_from(tbl))
426426
assert 2 == res3
427427
await tr3.commit()
428428
assert conn._transaction is None

tests/test_async_with.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55

66
from aiomysql import sa, create_pool, DictCursor, Cursor
7-
from sqlalchemy import MetaData, Table, Column, Integer, String
7+
from sqlalchemy import MetaData, Table, Column, Integer, String, func, select
88

99

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

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

175175
assert not tr.is_active
176-
cnt = await connection.scalar(tbl.count())
176+
cnt = await connection.scalar(select([func.count()]).select_from(tbl))
177177
assert 0 == cnt
178178

179179

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

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

197197

0 commit comments

Comments
 (0)