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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
|
import sqlalchemy as tsa
from sqlalchemy import column
from sqlalchemy import create_engine
from sqlalchemy import event
from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy import Integer
from sqlalchemy import literal
from sqlalchemy import MetaData
from sqlalchemy import pool
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy import TypeDecorator
from sqlalchemy.engine.base import Engine
from sqlalchemy.engine.mock import MockConnection
from sqlalchemy.testing import assert_raises
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import engines
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_false
from sqlalchemy.testing import is_true
from sqlalchemy.testing.mock import Mock
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
class SomeException(Exception):
pass
class CreateEngineTest(fixtures.TestBase):
def test_strategy_keyword_mock(self):
def executor(x, y):
pass
with testing.expect_deprecated(
"The create_engine.strategy keyword is deprecated, and the "
"only argument accepted is 'mock'"
):
e = create_engine(
"postgresql://", strategy="mock", executor=executor
)
assert isinstance(e, MockConnection)
def test_strategy_keyword_unknown(self):
with testing.expect_deprecated(
"The create_engine.strategy keyword is deprecated, and the "
"only argument accepted is 'mock'"
):
assert_raises_message(
tsa.exc.ArgumentError,
"unknown strategy: 'threadlocal'",
create_engine,
"postgresql://",
strategy="threadlocal",
)
class TransactionTest(fixtures.TestBase):
__backend__ = True
@classmethod
def setup_class(cls):
metadata = MetaData()
cls.users = Table(
"query_users",
metadata,
Column("user_id", Integer, primary_key=True),
Column("user_name", String(20)),
test_needs_acid=True,
)
cls.users.create(testing.db)
def teardown(self):
testing.db.execute(self.users.delete()).close()
@classmethod
def teardown_class(cls):
cls.users.drop(testing.db)
def test_transaction_container(self):
users = self.users
def go(conn, table, data):
for d in data:
conn.execute(table.insert(), d)
with testing.expect_deprecated(
r"The Engine.transaction\(\) method is deprecated"
):
testing.db.transaction(
go, users, [dict(user_id=1, user_name="user1")]
)
with testing.db.connect() as conn:
eq_(conn.execute(users.select()).fetchall(), [(1, "user1")])
with testing.expect_deprecated(
r"The Engine.transaction\(\) method is deprecated"
):
assert_raises(
tsa.exc.DBAPIError,
testing.db.transaction,
go,
users,
[
{"user_id": 2, "user_name": "user2"},
{"user_id": 1, "user_name": "user3"},
],
)
with testing.db.connect() as conn:
eq_(conn.execute(users.select()).fetchall(), [(1, "user1")])
class HandleInvalidatedOnConnectTest(fixtures.TestBase):
__requires__ = ("sqlite",)
def setUp(self):
e = create_engine("sqlite://")
connection = Mock(get_server_version_info=Mock(return_value="5.0"))
def connect(*args, **kwargs):
return connection
dbapi = Mock(
sqlite_version_info=(99, 9, 9),
version_info=(99, 9, 9),
sqlite_version="99.9.9",
paramstyle="named",
connect=Mock(side_effect=connect),
)
sqlite3 = e.dialect.dbapi
dbapi.Error = (sqlite3.Error,)
dbapi.ProgrammingError = sqlite3.ProgrammingError
self.dbapi = dbapi
self.ProgrammingError = sqlite3.ProgrammingError
class HandleErrorTest(fixtures.TestBase):
__requires__ = ("ad_hoc_engines",)
__backend__ = True
def tearDown(self):
Engine.dispatch._clear()
Engine._has_events = False
def test_legacy_dbapi_error(self):
engine = engines.testing_engine()
canary = Mock()
with testing.expect_deprecated(
r"The ConnectionEvents.dbapi_error\(\) event is deprecated"
):
event.listen(engine, "dbapi_error", canary)
with engine.connect() as conn:
try:
conn.execute("SELECT FOO FROM I_DONT_EXIST")
assert False
except tsa.exc.DBAPIError as e:
eq_(canary.mock_calls[0][1][5], e.orig)
eq_(canary.mock_calls[0][1][2], "SELECT FOO FROM I_DONT_EXIST")
def test_legacy_dbapi_error_no_ad_hoc_context(self):
engine = engines.testing_engine()
listener = Mock(return_value=None)
with testing.expect_deprecated(
r"The ConnectionEvents.dbapi_error\(\) event is deprecated"
):
event.listen(engine, "dbapi_error", listener)
nope = SomeException("nope")
class MyType(TypeDecorator):
impl = Integer
def process_bind_param(self, value, dialect):
raise nope
with engine.connect() as conn:
assert_raises_message(
tsa.exc.StatementError,
r"\(.*SomeException\) " r"nope\n\[SQL\: u?SELECT 1 ",
conn.execute,
select([1]).where(column("foo") == literal("bar", MyType())),
)
# no legacy event
eq_(listener.mock_calls, [])
def test_legacy_dbapi_error_non_dbapi_error(self):
engine = engines.testing_engine()
listener = Mock(return_value=None)
with testing.expect_deprecated(
r"The ConnectionEvents.dbapi_error\(\) event is deprecated"
):
event.listen(engine, "dbapi_error", listener)
nope = TypeError("I'm not a DBAPI error")
with engine.connect() as c:
c.connection.cursor = Mock(
return_value=Mock(execute=Mock(side_effect=nope))
)
assert_raises_message(
TypeError, "I'm not a DBAPI error", c.execute, "select "
)
# no legacy event
eq_(listener.mock_calls, [])
def MockDBAPI(): # noqa
def cursor():
return Mock()
def connect(*arg, **kw):
def close():
conn.closed = True
# mock seems like it might have an issue logging
# call_count correctly under threading, not sure.
# adding a side_effect for close seems to help.
conn = Mock(
cursor=Mock(side_effect=cursor),
close=Mock(side_effect=close),
closed=False,
)
return conn
def shutdown(value):
if value:
db.connect = Mock(side_effect=Exception("connect failed"))
else:
db.connect = Mock(side_effect=connect)
db.is_shutdown = value
db = Mock(
connect=Mock(side_effect=connect), shutdown=shutdown, is_shutdown=False
)
return db
class PoolTestBase(fixtures.TestBase):
def setup(self):
pool.clear_managers()
self._teardown_conns = []
def teardown(self):
for ref in self._teardown_conns:
conn = ref()
if conn:
conn.close()
@classmethod
def teardown_class(cls):
pool.clear_managers()
def _queuepool_fixture(self, **kw):
dbapi, pool = self._queuepool_dbapi_fixture(**kw)
return pool
def _queuepool_dbapi_fixture(self, **kw):
dbapi = MockDBAPI()
return (
dbapi,
pool.QueuePool(creator=lambda: dbapi.connect("foo.db"), **kw),
)
class ExplicitAutoCommitDeprecatedTest(fixtures.TestBase):
"""test the 'autocommit' flag on select() and text() objects.
Requires PostgreSQL so that we may define a custom function which
modifies the database. """
__only_on__ = "postgresql"
@classmethod
def setup_class(cls):
global metadata, foo
metadata = MetaData(testing.db)
foo = Table(
"foo",
metadata,
Column("id", Integer, primary_key=True),
Column("data", String(100)),
)
metadata.create_all()
testing.db.execute(
"create function insert_foo(varchar) "
"returns integer as 'insert into foo(data) "
"values ($1);select 1;' language sql"
)
def teardown(self):
foo.delete().execute().close()
@classmethod
def teardown_class(cls):
testing.db.execute("drop function insert_foo(varchar)")
metadata.drop_all()
def test_explicit_compiled(self):
conn1 = testing.db.connect()
conn2 = testing.db.connect()
with testing.expect_deprecated(
"The select.autocommit parameter is deprecated"
):
conn1.execute(select([func.insert_foo("data1")], autocommit=True))
assert conn2.execute(select([foo.c.data])).fetchall() == [("data1",)]
with testing.expect_deprecated(
r"The SelectBase.autocommit\(\) method is deprecated,"
):
conn1.execute(select([func.insert_foo("data2")]).autocommit())
assert conn2.execute(select([foo.c.data])).fetchall() == [
("data1",),
("data2",),
]
conn1.close()
conn2.close()
def test_explicit_text(self):
conn1 = testing.db.connect()
conn2 = testing.db.connect()
with testing.expect_deprecated(
"The text.autocommit parameter is deprecated"
):
conn1.execute(
text("select insert_foo('moredata')", autocommit=True)
)
assert conn2.execute(select([foo.c.data])).fetchall() == [
("moredata",)
]
conn1.close()
conn2.close()
class DeprecatedEngineFeatureTest(fixtures.TablesTest):
__backend__ = True
@classmethod
def define_tables(cls, metadata):
cls.table = Table(
"exec_test",
metadata,
Column("a", Integer),
Column("b", Integer),
test_needs_acid=True,
)
def _trans_fn(self, is_transaction=False):
def go(conn, x, value=None):
if is_transaction:
conn = conn.connection
conn.execute(self.table.insert().values(a=x, b=value))
return go
def _trans_rollback_fn(self, is_transaction=False):
def go(conn, x, value=None):
if is_transaction:
conn = conn.connection
conn.execute(self.table.insert().values(a=x, b=value))
raise SomeException("breakage")
return go
def _assert_no_data(self):
eq_(
testing.db.scalar(
select([func.count("*")]).select_from(self.table)
),
0,
)
def _assert_fn(self, x, value=None):
eq_(testing.db.execute(self.table.select()).fetchall(), [(x, value)])
def test_transaction_engine_fn_commit(self):
fn = self._trans_fn()
with testing.expect_deprecated(r"The Engine.transaction\(\) method"):
testing.db.transaction(fn, 5, value=8)
self._assert_fn(5, value=8)
def test_transaction_engine_fn_rollback(self):
fn = self._trans_rollback_fn()
with testing.expect_deprecated(
r"The Engine.transaction\(\) method is deprecated"
):
assert_raises_message(
Exception, "breakage", testing.db.transaction, fn, 5, value=8
)
self._assert_no_data()
def test_transaction_connection_fn_commit(self):
fn = self._trans_fn()
with testing.db.connect() as conn:
with testing.expect_deprecated(
r"The Connection.transaction\(\) method is deprecated"
):
conn.transaction(fn, 5, value=8)
self._assert_fn(5, value=8)
def test_transaction_connection_fn_rollback(self):
fn = self._trans_rollback_fn()
with testing.db.connect() as conn:
with testing.expect_deprecated(r""):
assert_raises(Exception, conn.transaction, fn, 5, value=8)
self._assert_no_data()
class DeprecatedReflectionTest(fixtures.TablesTest):
@classmethod
def define_tables(cls, metadata):
Table(
"user",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(50)),
)
Table(
"address",
metadata,
Column("id", Integer, primary_key=True),
Column("user_id", ForeignKey("user.id")),
Column("email", String(50)),
)
def test_exists(self):
dont_exist = Table("dont_exist", MetaData())
with testing.expect_deprecated(
r"The Table.exists\(\) method is deprecated"
):
is_false(dont_exist.exists(testing.db))
user = self.tables.user
with testing.expect_deprecated(
r"The Table.exists\(\) method is deprecated"
):
is_true(user.exists(testing.db))
def test_create_drop_explicit(self):
metadata = MetaData()
table = Table("test_table", metadata, Column("foo", Integer))
for bind in (testing.db, testing.db.connect()):
for args in [([], {"bind": bind}), ([bind], {})]:
metadata.create_all(*args[0], **args[1])
with testing.expect_deprecated(
r"The Table.exists\(\) method is deprecated"
):
assert table.exists(*args[0], **args[1])
metadata.drop_all(*args[0], **args[1])
table.create(*args[0], **args[1])
table.drop(*args[0], **args[1])
with testing.expect_deprecated(
r"The Table.exists\(\) method is deprecated"
):
assert not table.exists(*args[0], **args[1])
def test_create_drop_err_table(self):
metadata = MetaData()
table = Table("test_table", metadata, Column("foo", Integer))
with testing.expect_deprecated(
r"The Table.exists\(\) method is deprecated"
):
assert_raises_message(
tsa.exc.UnboundExecutionError,
(
"Table object 'test_table' is not bound to an Engine or "
"Connection."
),
table.exists,
)
def test_engine_has_table(self):
with testing.expect_deprecated(
r"The Engine.has_table\(\) method is deprecated"
):
is_false(testing.db.has_table("dont_exist"))
with testing.expect_deprecated(
r"The Engine.has_table\(\) method is deprecated"
):
is_true(testing.db.has_table("user"))
def test_engine_table_names(self):
metadata = self.metadata
with testing.expect_deprecated(
r"The Engine.table_names\(\) method is deprecated"
):
table_names = testing.db.table_names()
is_true(set(table_names).issuperset(metadata.tables))
|