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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
|
import re
import sqlalchemy as tsa
from sqlalchemy import create_engine
from sqlalchemy import event
from sqlalchemy import ForeignKey
from sqlalchemy import func
from sqlalchemy import inspect
from sqlalchemy import INT
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import pool
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy import VARCHAR
from sqlalchemy.engine import reflection
from sqlalchemy.engine.base import Connection
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 config
from sqlalchemy.testing import engines
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import is_
from sqlalchemy.testing import is_false
from sqlalchemy.testing import is_instance_of
from sqlalchemy.testing import is_true
from sqlalchemy.testing.engines import testing_engine
from sqlalchemy.testing.mock import Mock
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
def _string_deprecation_expect():
return testing.expect_deprecated_20(
r"Passing a string to Connection.execute\(\) is deprecated "
r"and will be removed in version 2.0"
)
class SomeException(Exception):
pass
class ConnectionlessDeprecationTest(fixtures.TestBase):
"""test various things associated with "connectionless" executions."""
def check_usage(self, inspector):
with inspector._operation_context() as conn:
is_instance_of(conn, Connection)
def test_inspector_constructor_engine(self):
with testing.expect_deprecated(
r"The __init__\(\) method on Inspector is deprecated and will "
r"be removed in a future release."
):
i1 = reflection.Inspector(testing.db)
is_(i1.bind, testing.db)
self.check_usage(i1)
def test_inspector_constructor_connection(self):
with testing.db.connect() as conn:
with testing.expect_deprecated(
r"The __init__\(\) method on Inspector is deprecated and "
r"will be removed in a future release."
):
i1 = reflection.Inspector(conn)
is_(i1.bind, conn)
is_(i1.engine, testing.db)
self.check_usage(i1)
def test_inspector_from_engine(self):
with testing.expect_deprecated(
r"The from_engine\(\) method on Inspector is deprecated and will "
r"be removed in a future release."
):
i1 = reflection.Inspector.from_engine(testing.db)
is_(i1.bind, testing.db)
self.check_usage(i1)
def test_bind_close_conn(self):
e = testing.db
conn = e.connect()
with testing.expect_deprecated_20(
r"The Connection.connect\(\) function/method is considered",
r"The .close\(\) method on a so-called 'branched' connection is "
r"deprecated as of 1.4, as are 'branched' connections overall, "
r"and will be removed in a future release.",
):
with conn.connect() as c2:
assert not c2.closed
assert not conn.closed
assert c2.closed
@testing.provide_metadata
def test_explicit_connectionless_execute(self):
table = Table("t", self.metadata, Column("a", Integer))
table.create(testing.db)
stmt = table.insert().values(a=1)
with testing.expect_deprecated_20(
r"The Engine.execute\(\) function/method is considered legacy",
):
testing.db.execute(stmt)
stmt = select([table])
with testing.expect_deprecated_20(
r"The Engine.execute\(\) function/method is considered legacy",
):
eq_(testing.db.execute(stmt).fetchall(), [(1,)])
@testing.provide_metadata
def test_implicit_execute(self):
table = Table("t", self.metadata, Column("a", Integer))
table.create(testing.db)
stmt = table.insert().values(a=1)
with testing.expect_deprecated_20(
r"The Executable.execute\(\) function/method is considered legacy",
):
stmt.execute()
stmt = select([table])
with testing.expect_deprecated_20(
r"The Executable.execute\(\) function/method is considered legacy",
):
eq_(stmt.execute().fetchall(), [(1,)])
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",
)
def test_empty_in_keyword(self):
with testing.expect_deprecated(
"The create_engine.empty_in_strategy keyword is deprecated, "
"and no longer has any effect."
):
create_engine(
"postgresql://",
empty_in_strategy="static",
module=Mock(),
_initialize=False,
)
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):
with testing.db.connect() as conn:
conn.execute(self.users.delete())
@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
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),
)
def select1(db):
return str(select([1]).compile(dialect=db.dialect))
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):
with testing.db.connect() as conn:
eq_(
conn.scalar(select(func.count("*")).select_from(self.table)),
0,
)
def _assert_fn(self, x, value=None):
with testing.db.connect() as conn:
eq_(conn.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()
def test_execute_plain_string(self):
with _string_deprecation_expect():
testing.db.execute(select1(testing.db)).scalar()
def test_scalar_plain_string(self):
with _string_deprecation_expect():
testing.db.scalar(select1(testing.db))
# Tests for the warning when non dict params are used
# @testing.combinations(42, (42,))
# def test_execute_positional_non_dicts(self, args):
# with testing.expect_deprecated(
# r"Usage of tuple or scalars as positional arguments of "
# ):
# testing.db.execute(text(select1(testing.db)), args).scalar()
# @testing.combinations(42, (42,))
# def test_scalar_positional_non_dicts(self, args):
# with testing.expect_deprecated(
# r"Usage of tuple or scalars as positional arguments of "
# ):
# testing.db.scalar(text(select1(testing.db)), args)
class DeprecatedConnectionFeatureTest(fixtures.TablesTest):
__backend__ = True
def test_execute_plain_string(self):
with _string_deprecation_expect():
with testing.db.connect() as conn:
conn.execute(select1(testing.db)).scalar()
def test_scalar_plain_string(self):
with _string_deprecation_expect():
with testing.db.connect() as conn:
conn.scalar(select1(testing.db))
# Tests for the warning when non dict params are used
# @testing.combinations(42, (42,))
# def test_execute_positional_non_dicts(self, args):
# with testing.expect_deprecated(
# r"Usage of tuple or scalars as positional arguments of "
# ):
# with testing.db.connect() as conn:
# conn.execute(text(select1(testing.db)), args).scalar()
# @testing.combinations(42, (42,))
# def test_scalar_positional_non_dicts(self, args):
# with testing.expect_deprecated(
# r"Usage of tuple or scalars as positional arguments of "
# ):
# with testing.db.connect() as conn:
# conn.scalar(text(select1(testing.db)), args)
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))
def test_reflecttable(self):
inspector = inspect(testing.db)
metadata = self.metadata
table = Table("user", metadata)
with testing.expect_deprecated_20(
r"The Inspector.reflecttable\(\) function/method is considered "
):
res = inspector.reflecttable(table, None)
exp = inspector.reflect_table(table, None)
eq_(res, exp)
class ExecutionOptionsTest(fixtures.TestBase):
def test_branched_connection_execution_options(self):
engine = engines.testing_engine("sqlite://")
conn = engine.connect()
c2 = conn.execution_options(foo="bar")
with testing.expect_deprecated_20(
r"The Connection.connect\(\) function/method is considered "
):
c2_branch = c2.connect()
eq_(c2_branch._execution_options, {"foo": "bar"})
class RawExecuteTest(fixtures.TablesTest):
__backend__ = True
@classmethod
def define_tables(cls, metadata):
Table(
"users",
metadata,
Column("user_id", INT, primary_key=True, autoincrement=False),
Column("user_name", VARCHAR(20)),
)
Table(
"users_autoinc",
metadata,
Column(
"user_id", INT, primary_key=True, test_needs_autoincrement=True
),
Column("user_name", VARCHAR(20)),
)
@testing.fails_on(
"postgresql+pg8000",
"pg8000 still doesn't allow single paren without params",
)
def test_no_params_option(self, connection):
stmt = (
"SELECT '%'"
+ testing.db.dialect.statement_compiler(
testing.db.dialect, None
).default_from()
)
with _string_deprecation_expect():
result = (
connection.execution_options(no_parameters=True)
.execute(stmt)
.scalar()
)
eq_(result, "%")
@testing.requires.qmark_paramstyle
def test_raw_qmark(self, connection):
conn = connection
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (?, ?)",
(1, "jack"),
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (?, ?)",
[2, "fred"],
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (?, ?)",
[3, "ed"],
[4, "horse"],
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (?, ?)",
(5, "barney"),
(6, "donkey"),
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (?, ?)",
7,
"sally",
)
with _string_deprecation_expect():
res = conn.execute("select * from users order by user_id")
assert res.fetchall() == [
(1, "jack"),
(2, "fred"),
(3, "ed"),
(4, "horse"),
(5, "barney"),
(6, "donkey"),
(7, "sally"),
]
for multiparam, param in [
(("jack", "fred"), {}),
((["jack", "fred"],), {}),
]:
with _string_deprecation_expect():
res = conn.execute(
"select * from users where user_name=? or "
"user_name=? order by user_id",
*multiparam,
**param
)
assert res.fetchall() == [(1, "jack"), (2, "fred")]
with _string_deprecation_expect():
res = conn.execute("select * from users where user_name=?", "jack")
assert res.fetchall() == [(1, "jack")]
@testing.requires.format_paramstyle
def test_raw_sprintf(self, connection):
conn = connection
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (%s, %s)",
[1, "jack"],
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (%s, %s)",
[2, "ed"],
[3, "horse"],
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) " "values (%s, %s)",
4,
"sally",
)
with _string_deprecation_expect():
conn.execute("insert into users (user_id) values (%s)", 5)
with _string_deprecation_expect():
res = conn.execute("select * from users order by user_id")
assert res.fetchall() == [
(1, "jack"),
(2, "ed"),
(3, "horse"),
(4, "sally"),
(5, None),
]
for multiparam, param in [
(("jack", "ed"), {}),
((["jack", "ed"],), {}),
]:
with _string_deprecation_expect():
res = conn.execute(
"select * from users where user_name=%s or "
"user_name=%s order by user_id",
*multiparam,
**param
)
assert res.fetchall() == [(1, "jack"), (2, "ed")]
with _string_deprecation_expect():
res = conn.execute(
"select * from users where user_name=%s", "jack"
)
assert res.fetchall() == [(1, "jack")]
@testing.requires.pyformat_paramstyle
def test_raw_python(self, connection):
conn = connection
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) "
"values (%(id)s, %(name)s)",
{"id": 1, "name": "jack"},
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) "
"values (%(id)s, %(name)s)",
{"id": 2, "name": "ed"},
{"id": 3, "name": "horse"},
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) "
"values (%(id)s, %(name)s)",
id=4,
name="sally",
)
with _string_deprecation_expect():
res = conn.execute("select * from users order by user_id")
assert res.fetchall() == [
(1, "jack"),
(2, "ed"),
(3, "horse"),
(4, "sally"),
]
@testing.requires.named_paramstyle
def test_raw_named(self, connection):
conn = connection
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) "
"values (:id, :name)",
{"id": 1, "name": "jack"},
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) "
"values (:id, :name)",
{"id": 2, "name": "ed"},
{"id": 3, "name": "horse"},
)
with _string_deprecation_expect():
conn.execute(
"insert into users (user_id, user_name) "
"values (:id, :name)",
id=4,
name="sally",
)
with _string_deprecation_expect():
res = conn.execute("select * from users order by user_id")
assert res.fetchall() == [
(1, "jack"),
(2, "ed"),
(3, "horse"),
(4, "sally"),
]
class EngineEventsTest(fixtures.TestBase):
__requires__ = ("ad_hoc_engines",)
__backend__ = True
def tearDown(self):
Engine.dispatch._clear()
Engine._has_events = False
def _assert_stmts(self, expected, received):
list(received)
for stmt, params, posn in expected:
if not received:
assert False, "Nothing available for stmt: %s" % stmt
while received:
teststmt, testparams, testmultiparams = received.pop(0)
teststmt = (
re.compile(r"[\n\t ]+", re.M).sub(" ", teststmt).strip()
)
if teststmt.startswith(stmt) and (
testparams == params or testparams == posn
):
break
def test_retval_flag(self):
canary = []
def tracker(name):
def go(conn, *args, **kw):
canary.append(name)
return go
def execute(conn, clauseelement, multiparams, params):
canary.append("execute")
return clauseelement, multiparams, params
def cursor_execute(
conn, cursor, statement, parameters, context, executemany
):
canary.append("cursor_execute")
return statement, parameters
engine = engines.testing_engine()
assert_raises(
tsa.exc.ArgumentError,
event.listen,
engine,
"begin",
tracker("begin"),
retval=True,
)
event.listen(engine, "before_execute", execute, retval=True)
event.listen(
engine, "before_cursor_execute", cursor_execute, retval=True
)
with testing.expect_deprecated(
r"The argument signature for the "
r"\"ConnectionEvents.before_execute\" event listener",
):
engine.execute(select([1]))
eq_(canary, ["execute", "cursor_execute"])
def test_argument_format_execute(self):
def before_execute(conn, clauseelement, multiparams, params):
assert isinstance(multiparams, (list, tuple))
assert isinstance(params, dict)
def after_execute(conn, clauseelement, multiparams, params, result):
assert isinstance(multiparams, (list, tuple))
assert isinstance(params, dict)
e1 = testing_engine(config.db_url)
event.listen(e1, "before_execute", before_execute)
event.listen(e1, "after_execute", after_execute)
with testing.expect_deprecated(
r"The argument signature for the "
r"\"ConnectionEvents.before_execute\" event listener",
r"The argument signature for the "
r"\"ConnectionEvents.after_execute\" event listener",
):
e1.execute(select([1]))
|