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
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
|
from unittest import mock
from unittest.mock import Mock
import sqlalchemy as tsa
from sqlalchemy import create_engine
from sqlalchemy import create_mock_engine
from sqlalchemy import event
from sqlalchemy import Index
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy import text
from sqlalchemy.schema import AddConstraint
from sqlalchemy.schema import CheckConstraint
from sqlalchemy.schema import DDL
from sqlalchemy.schema import DropConstraint
from sqlalchemy.schema import ForeignKeyConstraint
from sqlalchemy.schema import Sequence
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import config
from sqlalchemy.testing import engines
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.provision import normalize_sequence
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
class DDLEventTest(fixtures.TestBase):
def setup_test(self):
self.bind = engines.mock_engine()
self.metadata = MetaData()
self.table = Table("t", self.metadata, Column("id", Integer))
def test_table_create_before(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "before_create", canary.before_create)
table.create(bind)
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.before_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
)
],
)
def test_table_create_after(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "after_create", canary.after_create)
table.create(bind)
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.after_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
)
],
)
def test_table_create_both(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "before_create", canary.before_create)
event.listen(table, "after_create", canary.after_create)
table.create(bind)
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.before_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
mock.call.after_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
],
)
def test_table_drop_before(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "before_drop", canary.before_drop)
table.create(bind)
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.before_drop(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
)
],
)
def test_table_drop_after(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "after_drop", canary.after_drop)
table.create(bind)
canary.state = "skipped"
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.after_drop(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
)
],
)
def test_table_drop_both(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "before_drop", canary.before_drop)
event.listen(table, "after_drop", canary.after_drop)
table.create(bind)
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.before_drop(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
mock.call.after_drop(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
],
)
def test_table_all(self):
table, bind = self.table, self.bind
canary = mock.Mock()
event.listen(table, "before_create", canary.before_create)
event.listen(table, "after_create", canary.after_create)
event.listen(table, "before_drop", canary.before_drop)
event.listen(table, "after_drop", canary.after_drop)
table.create(bind)
table.drop(bind)
eq_(
canary.mock_calls,
[
mock.call.before_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
mock.call.after_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
mock.call.before_drop(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
mock.call.after_drop(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
),
],
)
def test_metadata_create_before(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, "before_create", canary.before_create)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.before_create(
# checkfirst is False because of the MockConnection
# used in the current testing strategy.
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
)
],
)
def test_metadata_create_after(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, "after_create", canary.after_create)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.after_create(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
)
],
)
def test_metadata_create_both(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, "before_create", canary.before_create)
event.listen(metadata, "after_create", canary.after_create)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.before_create(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
),
mock.call.after_create(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
),
],
)
def test_metadata_drop_before(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, "before_drop", canary.before_drop)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.before_drop(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
)
],
)
def test_metadata_drop_after(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, "after_drop", canary.after_drop)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.after_drop(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
)
],
)
def test_metadata_drop_both(self):
metadata, bind = self.metadata, self.bind
canary = mock.Mock()
event.listen(metadata, "before_drop", canary.before_drop)
event.listen(metadata, "after_drop", canary.after_drop)
metadata.create_all(bind)
metadata.drop_all(bind)
eq_(
canary.mock_calls,
[
mock.call.before_drop(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
),
mock.call.after_drop(
metadata,
self.bind,
checkfirst=False,
tables=list(metadata.tables.values()),
_ddl_runner=mock.ANY,
),
],
)
def test_metadata_table_isolation(self):
metadata, table = self.metadata, self.table
table_canary = mock.Mock()
metadata_canary = mock.Mock()
event.listen(table, "before_create", table_canary.before_create)
event.listen(metadata, "before_create", metadata_canary.before_create)
self.table.create(self.bind)
eq_(
table_canary.mock_calls,
[
mock.call.before_create(
table,
self.bind,
checkfirst=False,
_ddl_runner=mock.ANY,
_is_metadata_operation=mock.ANY,
)
],
)
eq_(metadata_canary.mock_calls, [])
class DDLEventHarness:
creates_implicitly_with_table = True
drops_implicitly_with_table = True
@testing.fixture
def produce_subject(self):
raise NotImplementedError()
@testing.fixture
def produce_event_target(self, produce_subject, connection):
"""subclasses may want to override this for cases where the target
sent to the event is not the same object as that which was
listened on.
the example here is for :class:`.SchemaType` objects like
:class:`.Enum` that produce a dialect-specific implementation
which is where the actual CREATE/DROP happens.
"""
return produce_subject
@testing.fixture
def produce_table_integrated_subject(self, metadata, produce_subject):
raise NotImplementedError()
def test_table_integrated(
self,
metadata,
connection,
produce_subject,
produce_table_integrated_subject,
produce_event_target,
):
subject = produce_subject
assert_subject = produce_event_target
canary = mock.Mock()
event.listen(subject, "before_create", canary.before_create)
event.listen(subject, "after_create", canary.after_create)
event.listen(subject, "before_drop", canary.before_drop)
event.listen(subject, "after_drop", canary.after_drop)
metadata.create_all(connection, checkfirst=False)
if self.creates_implicitly_with_table:
create_calls = []
else:
create_calls = [
mock.call.before_create(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
mock.call.after_create(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
]
eq_(canary.mock_calls, create_calls)
metadata.drop_all(connection, checkfirst=False)
if self.drops_implicitly_with_table:
eq_(canary.mock_calls, create_calls + [])
else:
eq_(
canary.mock_calls,
create_calls
+ [
mock.call.before_drop(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
mock.call.after_drop(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
],
)
class DDLEventWCreateHarness(DDLEventHarness):
requires_table_to_exist = True
def test_straight_create_drop(
self,
metadata,
connection,
produce_subject,
produce_table_integrated_subject,
produce_event_target,
):
subject = produce_subject
assert_subject = produce_event_target
if self.requires_table_to_exist:
metadata.create_all(connection, checkfirst=False)
subject.drop(connection)
canary = mock.Mock()
event.listen(subject, "before_create", canary.before_create)
event.listen(subject, "after_create", canary.after_create)
event.listen(subject, "before_drop", canary.before_drop)
event.listen(subject, "after_drop", canary.after_drop)
subject.create(connection)
eq_(
canary.mock_calls,
[
mock.call.before_create(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
mock.call.after_create(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
],
)
subject.drop(connection)
eq_(
canary.mock_calls,
[
mock.call.before_create(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
mock.call.after_create(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
mock.call.before_drop(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
mock.call.after_drop(
assert_subject,
connection,
_ddl_runner=mock.ANY,
),
],
)
class SequenceDDLEventTest(DDLEventWCreateHarness, fixtures.TestBase):
__requires__ = ("sequences",)
creates_implicitly_with_table = False
drops_implicitly_with_table = False
supports_standalone_create = True
@testing.fixture
def produce_subject(self):
return normalize_sequence(config, Sequence("my_seq"))
@testing.fixture
def produce_table_integrated_subject(self, metadata, produce_subject):
return Table(
"t",
metadata,
Column("id", Integer, produce_subject, primary_key=True),
)
class IndexDDLEventTest(DDLEventWCreateHarness, fixtures.TestBase):
creates_implicitly_with_table = False
drops_implicitly_with_table = True
supports_standalone_create = False
@testing.fixture
def produce_subject(self):
return Index("my_idx", "key")
@testing.fixture
def produce_table_integrated_subject(self, metadata, produce_subject):
return Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("key", String(50)),
produce_subject,
)
class ForeignKeyConstraintDDLEventTest(DDLEventHarness, fixtures.TestBase):
creates_implicitly_with_table = True
drops_implicitly_with_table = True
supports_standalone_create = False
@testing.fixture
def produce_subject(self):
return ForeignKeyConstraint(["related_id"], ["related.id"], name="fkc")
@testing.fixture
def produce_table_integrated_subject(self, metadata, produce_subject):
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("related_id", Integer),
produce_subject,
)
Table("related", metadata, Column("id", Integer, primary_key=True))
class DDLExecutionTest(AssertsCompiledSQL, fixtures.TestBase):
def setup_test(self):
self.engine = engines.mock_engine()
self.metadata = MetaData()
self.users = Table(
"users",
self.metadata,
Column("user_id", Integer, primary_key=True),
Column("user_name", String(40)),
)
def test_table_standalone(self):
users, engine = self.users, self.engine
event.listen(users, "before_create", DDL("mxyzptlk"))
event.listen(users, "after_create", DDL("klptzyxm"))
event.listen(users, "before_drop", DDL("xyzzy"))
event.listen(users, "after_drop", DDL("fnord"))
users.create(self.engine)
strings = [str(x) for x in engine.mock]
assert "mxyzptlk" in strings
assert "klptzyxm" in strings
assert "xyzzy" not in strings
assert "fnord" not in strings
del engine.mock[:]
users.drop(self.engine)
strings = [str(x) for x in engine.mock]
assert "mxyzptlk" not in strings
assert "klptzyxm" not in strings
assert "xyzzy" in strings
assert "fnord" in strings
def test_table_by_metadata(self):
metadata, users, engine = self.metadata, self.users, self.engine
event.listen(users, "before_create", DDL("mxyzptlk"))
event.listen(users, "after_create", DDL("klptzyxm"))
event.listen(users, "before_drop", DDL("xyzzy"))
event.listen(users, "after_drop", DDL("fnord"))
metadata.create_all(self.engine)
strings = [str(x) for x in engine.mock]
assert "mxyzptlk" in strings
assert "klptzyxm" in strings
assert "xyzzy" not in strings
assert "fnord" not in strings
del engine.mock[:]
metadata.drop_all(self.engine)
strings = [str(x) for x in engine.mock]
assert "mxyzptlk" not in strings
assert "klptzyxm" not in strings
assert "xyzzy" in strings
assert "fnord" in strings
def test_metadata(self):
metadata, engine = self.metadata, self.engine
event.listen(metadata, "before_create", DDL("mxyzptlk"))
event.listen(metadata, "after_create", DDL("klptzyxm"))
event.listen(metadata, "before_drop", DDL("xyzzy"))
event.listen(metadata, "after_drop", DDL("fnord"))
metadata.create_all(self.engine)
strings = [str(x) for x in engine.mock]
assert "mxyzptlk" in strings
assert "klptzyxm" in strings
assert "xyzzy" not in strings
assert "fnord" not in strings
del engine.mock[:]
metadata.drop_all(self.engine)
strings = [str(x) for x in engine.mock]
assert "mxyzptlk" not in strings
assert "klptzyxm" not in strings
assert "xyzzy" in strings
assert "fnord" in strings
def test_conditional_constraint(self):
metadata, users = self.metadata, self.users
nonpg_mock = engines.mock_engine(dialect_name="sqlite")
pg_mock = engines.mock_engine(dialect_name="postgresql")
constraint = CheckConstraint(
"a < b", name="my_test_constraint", table=users
)
# by placing the constraint in an Add/Drop construct, the
# 'inline_ddl' flag is set to False
event.listen(
users,
"after_create",
AddConstraint(constraint).execute_if(dialect="postgresql"),
)
event.listen(
users,
"before_drop",
DropConstraint(constraint).execute_if(dialect="postgresql"),
)
metadata.create_all(bind=nonpg_mock)
strings = " ".join(str(x) for x in nonpg_mock.mock)
assert "my_test_constraint" not in strings
metadata.drop_all(bind=nonpg_mock)
strings = " ".join(str(x) for x in nonpg_mock.mock)
assert "my_test_constraint" not in strings
metadata.create_all(bind=pg_mock)
strings = " ".join(str(x) for x in pg_mock.mock)
assert "my_test_constraint" in strings
metadata.drop_all(bind=pg_mock)
strings = " ".join(str(x) for x in pg_mock.mock)
assert "my_test_constraint" in strings
@testing.combinations(("dialect",), ("callable",), ("callable_w_state",))
def test_inline_ddl_if_dialect_name(self, ddl_if_type):
nonpg_mock = engines.mock_engine(dialect_name="sqlite")
pg_mock = engines.mock_engine(dialect_name="postgresql")
metadata = MetaData()
capture_mock = Mock()
state = object()
if ddl_if_type == "dialect":
ddl_kwargs = dict(dialect="postgresql")
elif ddl_if_type == "callable":
def is_pg(ddl, target, bind, **kw):
capture_mock.is_pg(ddl, target, bind, **kw)
return kw["dialect"].name == "postgresql"
ddl_kwargs = dict(callable_=is_pg)
elif ddl_if_type == "callable_w_state":
def is_pg(ddl, target, bind, **kw):
capture_mock.is_pg(ddl, target, bind, **kw)
return kw["dialect"].name == "postgresql"
ddl_kwargs = dict(callable_=is_pg, state=state)
else:
assert False
data_col = Column("data", String)
t = Table(
"a",
metadata,
Column("id", Integer, primary_key=True),
Column("num", Integer),
data_col,
Index("my_pg_index", data_col).ddl_if(**ddl_kwargs),
CheckConstraint("num > 5").ddl_if(**ddl_kwargs),
)
metadata.create_all(nonpg_mock)
eq_(len(nonpg_mock.mock), 1)
self.assert_compile(
nonpg_mock.mock[0],
"CREATE TABLE a (id INTEGER NOT NULL, num INTEGER, "
"data VARCHAR, PRIMARY KEY (id))",
dialect=nonpg_mock.dialect,
)
metadata.create_all(pg_mock)
eq_(len(pg_mock.mock), 2)
self.assert_compile(
pg_mock.mock[0],
"CREATE TABLE a (id SERIAL NOT NULL, num INTEGER, "
"data VARCHAR, PRIMARY KEY (id), CHECK (num > 5))",
dialect=pg_mock.dialect,
)
self.assert_compile(
pg_mock.mock[1],
"CREATE INDEX my_pg_index ON a (data)",
dialect="postgresql",
)
the_index = list(t.indexes)[0]
the_constraint = list(
c for c in t.constraints if isinstance(c, CheckConstraint)
)[0]
if ddl_if_type in ("callable", "callable_w_state"):
if ddl_if_type == "callable":
check_state = None
else:
check_state = state
eq_(
capture_mock.mock_calls,
[
mock.call.is_pg(
mock.ANY,
the_index,
mock.ANY,
state=check_state,
dialect=nonpg_mock.dialect,
compiler=None,
),
mock.call.is_pg(
mock.ANY,
the_constraint,
None,
state=check_state,
dialect=nonpg_mock.dialect,
compiler=mock.ANY,
),
mock.call.is_pg(
mock.ANY,
the_index,
mock.ANY,
state=check_state,
dialect=pg_mock.dialect,
compiler=None,
),
mock.call.is_pg(
mock.ANY,
the_constraint,
None,
state=check_state,
dialect=pg_mock.dialect,
compiler=mock.ANY,
),
],
)
@testing.requires.sqlite
def test_ddl_execute(self):
engine = create_engine("sqlite:///")
cx = engine.connect()
cx.begin()
ddl = DDL("SELECT 1")
r = cx.execute(ddl)
eq_(list(r), [(1,)])
def test_platform_escape(self):
"""test the escaping of % characters in the DDL construct."""
default_from = testing.db.dialect.statement_compiler(
testing.db.dialect, None
).default_from()
# We're abusing the DDL()
# construct here by pushing a SELECT through it
# so that we can verify the round trip.
# the DDL() will trigger autocommit, which prohibits
# some DBAPIs from returning results (pyodbc), so we
# run in an explicit transaction.
with testing.db.begin() as conn:
eq_(
conn.execute(
text("select 'foo%something'" + default_from)
).scalar(),
"foo%something",
)
eq_(
conn.execute(
DDL("select 'foo%%something'" + default_from)
).scalar(),
"foo%something",
)
class DDLTransactionTest(fixtures.TestBase):
"""test DDL transactional behavior as of SQLAlchemy 1.4."""
@testing.fixture
def metadata_fixture(self):
m = MetaData()
Table("t1", m, Column("q", Integer))
Table("t2", m, Column("q", Integer))
try:
yield m
finally:
m.drop_all(testing.db)
@testing.fixture
def listening_engine_fixture(self):
eng = engines.testing_engine()
m1 = mock.Mock()
event.listen(eng, "begin", m1.begin)
event.listen(eng, "commit", m1.commit)
event.listen(eng, "rollback", m1.rollback)
@event.listens_for(eng, "before_cursor_execute")
def before_cursor_execute(
conn, cursor, statement, parameters, context, executemany
):
if "CREATE TABLE" in statement:
m1.cursor_execute("CREATE TABLE ...")
eng.connect().close()
return eng, m1
def test_ddl_engine(self, metadata_fixture, listening_engine_fixture):
eng, m1 = listening_engine_fixture
metadata_fixture.create_all(eng)
eq_(
m1.mock_calls,
[
mock.call.begin(mock.ANY),
mock.call.cursor_execute("CREATE TABLE ..."),
mock.call.cursor_execute("CREATE TABLE ..."),
mock.call.commit(mock.ANY),
],
)
def test_ddl_connection_autobegin_transaction(
self, metadata_fixture, listening_engine_fixture
):
eng, m1 = listening_engine_fixture
with eng.connect() as conn:
metadata_fixture.create_all(conn)
conn.commit()
eq_(
m1.mock_calls,
[
mock.call.begin(mock.ANY),
mock.call.cursor_execute("CREATE TABLE ..."),
mock.call.cursor_execute("CREATE TABLE ..."),
mock.call.commit(mock.ANY),
],
)
def test_ddl_connection_explicit_begin_transaction(
self, metadata_fixture, listening_engine_fixture
):
eng, m1 = listening_engine_fixture
with eng.connect() as conn:
with conn.begin():
metadata_fixture.create_all(conn)
eq_(
m1.mock_calls,
[
mock.call.begin(mock.ANY),
mock.call.cursor_execute("CREATE TABLE ..."),
mock.call.cursor_execute("CREATE TABLE ..."),
mock.call.commit(mock.ANY),
],
)
class DDLTest(fixtures.TestBase, AssertsCompiledSQL):
def mock_engine(self):
def executor(*a, **kw):
return None
engine = create_mock_engine(testing.db.name + "://", executor)
# fmt: off
engine.dialect.identifier_preparer = \
tsa.sql.compiler.IdentifierPreparer(
engine.dialect
)
# fmt: on
return engine
def test_tokens(self):
m = MetaData()
sane_alone = Table("t", m, Column("id", Integer))
sane_schema = Table("t", m, Column("id", Integer), schema="s")
insane_alone = Table("t t", m, Column("id", Integer))
insane_schema = Table("t t", m, Column("id", Integer), schema="s s")
ddl = DDL("%(schema)s-%(table)s-%(fullname)s")
dialect = self.mock_engine().dialect
self.assert_compile(ddl.against(sane_alone), "-t-t", dialect=dialect)
self.assert_compile(
ddl.against(sane_schema), "s-t-s.t", dialect=dialect
)
self.assert_compile(
ddl.against(insane_alone), '-"t t"-"t t"', dialect=dialect
)
self.assert_compile(
ddl.against(insane_schema),
'"s s"-"t t"-"s s"."t t"',
dialect=dialect,
)
# overrides are used piece-meal and verbatim.
ddl = DDL(
"%(schema)s-%(table)s-%(fullname)s-%(bonus)s",
context={"schema": "S S", "table": "T T", "bonus": "b"},
)
self.assert_compile(
ddl.against(sane_alone), "S S-T T-t-b", dialect=dialect
)
self.assert_compile(
ddl.against(sane_schema), "S S-T T-s.t-b", dialect=dialect
)
self.assert_compile(
ddl.against(insane_alone), 'S S-T T-"t t"-b', dialect=dialect
)
self.assert_compile(
ddl.against(insane_schema),
'S S-T T-"s s"."t t"-b',
dialect=dialect,
)
def test_filter(self):
cx = self.mock_engine()
tbl = Table("t", MetaData(), Column("id", Integer))
target = cx.name
assert DDL("")._should_execute(tbl, cx)
assert DDL("").execute_if(dialect=target)._should_execute(tbl, cx)
assert not DDL("").execute_if(dialect="bogus")._should_execute(tbl, cx)
assert (
DDL("")
.execute_if(callable_=lambda d, y, z, **kw: True)
._should_execute(tbl, cx)
)
assert (
DDL("")
.execute_if(
callable_=lambda d, y, z, **kw: z.engine.name != "bogus"
)
._should_execute(tbl, cx)
)
|