-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest_delete.py
More file actions
483 lines (409 loc) · 15.1 KB
/
Copy pathtest_delete.py
File metadata and controls
483 lines (409 loc) · 15.1 KB
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
from sqlalchemy import and_
from sqlalchemy import delete
from sqlalchemy import exc
from sqlalchemy import exists
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import or_
from sqlalchemy import select
from sqlalchemy import String
from sqlalchemy import testing
from sqlalchemy.dialects import mysql
from sqlalchemy.dialects import postgresql
from sqlalchemy.engine import default
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import eq_
from sqlalchemy.testing import expect_deprecated
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
from .test_update import _FilterByDMLSuite
class _DeleteTestBase:
@classmethod
def define_tables(cls, metadata):
Table(
"mytable",
metadata,
Column("myid", Integer),
Column("name", String(30)),
Column("description", String(50)),
)
Table(
"myothertable",
metadata,
Column("otherid", Integer),
Column("othername", String(30)),
)
class DeleteTest(_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL):
__dialect__ = "default"
def test_delete_literal_binds(self):
table1 = self.tables.mytable
stmt = table1.delete().where(table1.c.name == "jill")
self.assert_compile(
stmt,
"DELETE FROM mytable WHERE mytable.name = 'jill'",
literal_binds=True,
)
def test_delete(self):
table1 = self.tables.mytable
self.assert_compile(
delete(table1).where(table1.c.myid == 7),
"DELETE FROM mytable WHERE mytable.myid = :myid_1",
)
self.assert_compile(
table1.delete().where(table1.c.myid == 7),
"DELETE FROM mytable WHERE mytable.myid = :myid_1",
)
self.assert_compile(
table1.delete()
.where(table1.c.myid == 7)
.where(table1.c.name == "somename"),
"DELETE FROM mytable "
"WHERE mytable.myid = :myid_1 "
"AND mytable.name = :name_1",
)
def test_where_empty(self):
table1 = self.tables.mytable
with expect_deprecated(
r"Invoking and_\(\) without arguments is deprecated"
):
self.assert_compile(
table1.delete().where(and_()), "DELETE FROM mytable"
)
with expect_deprecated(
r"Invoking or_\(\) without arguments is deprecated"
):
self.assert_compile(
table1.delete().where(or_()), "DELETE FROM mytable"
)
def test_prefix_with(self):
table1 = self.tables.mytable
stmt = (
table1.delete()
.prefix_with("A", "B", dialect="mysql")
.prefix_with("C", "D")
)
self.assert_compile(stmt, "DELETE C D FROM mytable")
self.assert_compile(
stmt, "DELETE A B C D FROM mytable", dialect=mysql.dialect()
)
def test_alias(self):
table1 = self.tables.mytable
talias1 = table1.alias("t1")
stmt = delete(talias1).where(talias1.c.myid == 7)
self.assert_compile(
stmt, "DELETE FROM mytable AS t1 WHERE t1.myid = :myid_1"
)
def test_non_correlated_select(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
# test a non-correlated WHERE clause
s = select(table2.c.othername).where(table2.c.otherid == 7)
self.assert_compile(
delete(table1).where(table1.c.name == s.scalar_subquery()),
"DELETE FROM mytable "
"WHERE mytable.name = ("
"SELECT myothertable.othername "
"FROM myothertable "
"WHERE myothertable.otherid = :otherid_1"
")",
)
def test_correlated_select(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
# test one that is actually correlated...
s = select(table2.c.othername).where(table2.c.otherid == table1.c.myid)
self.assert_compile(
table1.delete().where(table1.c.name == s.scalar_subquery()),
"DELETE FROM mytable "
"WHERE mytable.name = ("
"SELECT myothertable.othername "
"FROM myothertable "
"WHERE myothertable.otherid = mytable.myid"
")",
)
class DeleteFromCompileTest(
_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL
):
# DELETE FROM is also tested by individual dialects since there is no
# consistent syntax. here we use the StrSQLcompiler which has a fake
# syntax.
__dialect__ = "default_enhanced"
def test_delete_extra_froms(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = table1.delete().where(table1.c.myid == table2.c.otherid)
self.assert_compile(
stmt,
"DELETE FROM mytable , myothertable "
"WHERE mytable.myid = myothertable.otherid",
)
def test_delete_explicit_using(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = table1.delete().using(table2).where(table1.c.myid == 7)
self.assert_compile(
stmt,
"DELETE FROM mytable , myothertable "
"WHERE mytable.myid = :myid_1",
checkparams={"myid_1": 7},
)
def test_delete_explicit_using_dedupe_implicit_from(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = (
table1.delete()
.using(table2)
.where(table1.c.myid == table2.c.otherid)
)
self.assert_compile(
stmt,
"DELETE FROM mytable , myothertable "
"WHERE mytable.myid = myothertable.otherid",
)
def test_delete_explicit_using_join_mysql(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = (
table1.delete()
.using(table1.outerjoin(table2, table1.c.myid == table2.c.otherid))
.where(table2.c.othername == None)
)
self.assert_compile(
stmt,
"DELETE FROM mytable USING mytable LEFT OUTER JOIN myothertable "
"ON mytable.myid = myothertable.otherid "
"WHERE myothertable.othername IS NULL",
dialect=mysql.dialect(),
)
def test_delete_explicit_using_join_postgresql(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = (
table1.delete()
.using(table2.outerjoin(table1, table1.c.myid == table2.c.otherid))
.where(table2.c.othername == None)
)
self.assert_compile(
stmt,
"DELETE FROM mytable USING myothertable LEFT OUTER JOIN mytable "
"ON mytable.myid = myothertable.otherid "
"WHERE myothertable.othername IS NULL",
dialect=postgresql.dialect(),
)
def test_correlation_to_extra(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = (
table1.delete()
.where(table1.c.myid == table2.c.otherid)
.where(
~exists()
.where(table2.c.otherid == table1.c.myid)
.where(table2.c.othername == "x")
.correlate(table2)
)
)
self.assert_compile(
stmt,
"DELETE FROM mytable , myothertable WHERE mytable.myid = "
"myothertable.otherid AND NOT (EXISTS "
"(SELECT * FROM mytable WHERE myothertable.otherid = "
"mytable.myid AND myothertable.othername = :othername_1))",
)
def test_dont_correlate_to_extra(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = (
table1.delete()
.where(table1.c.myid == table2.c.otherid)
.where(
~exists()
.where(table2.c.otherid == table1.c.myid)
.where(table2.c.othername == "x")
.correlate()
)
)
self.assert_compile(
stmt,
"DELETE FROM mytable , myothertable WHERE mytable.myid = "
"myothertable.otherid AND NOT (EXISTS "
"(SELECT * FROM myothertable, mytable "
"WHERE myothertable.otherid = "
"mytable.myid AND myothertable.othername = :othername_1))",
)
def test_autocorrelate_error(self):
table1, table2 = self.tables.mytable, self.tables.myothertable
stmt = (
table1.delete()
.where(table1.c.myid == table2.c.otherid)
.where(
~exists()
.where(table2.c.otherid == table1.c.myid)
.where(table2.c.othername == "x")
)
)
assert_raises_message(
exc.InvalidRequestError,
".*returned no FROM clauses due to auto-correlation.*",
stmt.compile,
dialect=default.StrCompileDialect(),
)
class DeleteFromRoundTripTest(fixtures.TablesTest):
__sparse_driver_backend__ = True
@classmethod
def define_tables(cls, metadata):
Table(
"mytable",
metadata,
Column("myid", Integer),
Column("name", String(30)),
Column("description", String(50)),
)
Table(
"myothertable",
metadata,
Column("otherid", Integer),
Column("othername", String(30)),
)
Table(
"users",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("name", String(30), nullable=False),
)
Table(
"addresses",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("user_id", None, ForeignKey("users.id")),
Column("name", String(30), nullable=False),
Column("email_address", String(50), nullable=False),
)
Table(
"dingalings",
metadata,
Column(
"id", Integer, primary_key=True, test_needs_autoincrement=True
),
Column("address_id", None, ForeignKey("addresses.id")),
Column("data", String(30)),
)
Table(
"update_w_default",
metadata,
Column("id", Integer, primary_key=True),
Column("x", Integer),
Column("ycol", Integer, key="y"),
Column("data", String(30), onupdate=lambda: "hi"),
)
@classmethod
def fixtures(cls):
return dict(
users=(
("id", "name"),
(7, "jack"),
(8, "ed"),
(9, "fred"),
(10, "chuck"),
),
addresses=(
("id", "user_id", "name", "email_address"),
(1, 7, "x", "jack@bean.com"),
(2, 8, "x", "ed@wood.com"),
(3, 8, "x", "ed@bettyboop.com"),
(4, 8, "x", "ed@lala.com"),
(5, 9, "x", "fred@fred.com"),
),
dingalings=(
("id", "address_id", "data"),
(1, 2, "ding 1/2"),
(2, 5, "ding 2/5"),
),
)
@testing.requires.delete_using
def test_exec_two_table(self, connection):
users, addresses = self.tables.users, self.tables.addresses
dingalings = self.tables.dingalings
connection.execute(dingalings.delete()) # fk violation otherwise
connection.execute(
addresses.delete()
.where(users.c.id == addresses.c.user_id)
.where(users.c.name == "ed")
)
expected = [
(1, 7, "x", "jack@bean.com"),
(5, 9, "x", "fred@fred.com"),
]
self._assert_table(connection, addresses, expected)
@testing.requires.delete_using
def test_exec_three_table(self, connection):
users = self.tables.users
addresses = self.tables.addresses
dingalings = self.tables.dingalings
connection.execute(
dingalings.delete()
.where(users.c.id == addresses.c.user_id)
.where(users.c.name == "ed")
.where(addresses.c.id == dingalings.c.address_id)
)
expected = [(2, 5, "ding 2/5")]
self._assert_table(connection, dingalings, expected)
@testing.requires.delete_using
def test_exec_two_table_plus_alias(self, connection):
users, addresses = self.tables.users, self.tables.addresses
dingalings = self.tables.dingalings
connection.execute(dingalings.delete()) # fk violation otherwise
a1 = addresses.alias()
connection.execute(
addresses.delete()
.where(users.c.id == addresses.c.user_id)
.where(users.c.name == "ed")
.where(a1.c.id == addresses.c.id)
)
expected = [(1, 7, "x", "jack@bean.com"), (5, 9, "x", "fred@fred.com")]
self._assert_table(connection, addresses, expected)
@testing.requires.delete_using
def test_exec_alias_plus_table(self, connection):
users, addresses = self.tables.users, self.tables.addresses
dingalings = self.tables.dingalings
d1 = dingalings.alias()
connection.execute(
delete(d1)
.where(users.c.id == addresses.c.user_id)
.where(users.c.name == "ed")
.where(addresses.c.id == d1.c.address_id)
)
expected = [(2, 5, "ding 2/5")]
self._assert_table(connection, dingalings, expected)
def _assert_table(self, connection, table, expected):
stmt = table.select().order_by(table.c.id)
eq_(connection.execute(stmt).fetchall(), expected)
class DeleteFilterByTest(_FilterByDMLSuite):
@testing.fixture
def one_table_statement(self):
users = self.tables.users
return users.delete()
@testing.fixture
def two_table_statement(self):
users = self.tables.users
addresses = self.tables.addresses
return users.delete().where(users.c.id == addresses.c.user_id)
@testing.fixture
def three_table_statement(self):
users = self.tables.users
addresses = self.tables.addresses
dingalings = self.tables.dingalings
return (
users.delete()
.where(users.c.id == addresses.c.user_id)
.where(addresses.c.id == dingalings.c.address_id)
)
@testing.fixture
def four_table_statement(self):
users = self.tables.users
addresses = self.tables.addresses
dingalings = self.tables.dingalings
departments = self.tables.departments
return (
users.delete()
.where(users.c.id == addresses.c.user_id)
.where(addresses.c.id == dingalings.c.address_id)
.where(departments.c.id == users.c.department_id)
)