-
Notifications
You must be signed in to change notification settings - Fork 482
Expand file tree
/
Copy pathtest_sql.py
More file actions
318 lines (273 loc) · 12.5 KB
/
test_sql.py
File metadata and controls
318 lines (273 loc) · 12.5 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
import re
import pytest
from tests.testmodels import CharPkModel, Drink, Event, IntFields, Tournament
from tortoise import connections
from tortoise.backends.psycopg.client import PsycopgClient
from tortoise.expressions import F
from tortoise.functions import Coalesce, Concat
@pytest.fixture
def sql_context(db):
"""Fixture providing database connection, dialect and psycopg flag."""
db_conn = connections.get("models")
dialect = db_conn.schema_generator.DIALECT
is_psycopg = isinstance(db_conn, PsycopgClient)
return db_conn, dialect, is_psycopg
def test_filter(sql_context):
db, dialect, is_psycopg = sql_context
sql = CharPkModel.all().filter(id="123").sql()
if dialect == "mysql":
expected = "SELECT `id` FROM `charpkmodel` WHERE `id`=%s"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=%s'
else:
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=$1'
else:
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=?'
assert sql == expected
def test_filter_with_limit_offset(sql_context):
db, dialect, is_psycopg = sql_context
sql = CharPkModel.all().filter(id="123").limit(10).offset(0).sql()
if dialect == "mysql":
expected = "SELECT `id` FROM `charpkmodel` WHERE `id`=%s LIMIT %s OFFSET %s"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=%s LIMIT %s OFFSET %s'
else:
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=$1 LIMIT $2 OFFSET $3'
elif dialect == "mssql":
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=? ORDER BY (SELECT 0) OFFSET ? ROWS FETCH NEXT ? ROWS ONLY'
else:
expected = 'SELECT "id" FROM "charpkmodel" WHERE "id"=? LIMIT ? OFFSET ?'
assert sql == expected
def test_group_by(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.all().group_by("intnum").values("intnum").sql()
if dialect == "mysql":
expected = "SELECT `intnum` `intnum` FROM `intfields` GROUP BY `intnum`"
else:
expected = 'SELECT "intnum" "intnum" FROM "intfields" GROUP BY "intnum"'
assert sql == expected
def test_annotate(sql_context):
db, dialect, is_psycopg = sql_context
sql = CharPkModel.all().annotate(id_plus_one=Concat(F("id"), "_postfix")).sql()
if dialect == "mysql":
expected = "SELECT `id`,CONCAT(`id`,%s) `id_plus_one` FROM `charpkmodel`"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT "id",CONCAT("id"::text,%s::text) "id_plus_one" FROM "charpkmodel"'
else:
expected = 'SELECT "id",CONCAT("id"::text,$1::text) "id_plus_one" FROM "charpkmodel"'
else:
expected = 'SELECT "id",CONCAT("id",?) "id_plus_one" FROM "charpkmodel"'
assert sql == expected
def test_annotate_concat_fields(sql_context):
db, dialect, is_psycopg = sql_context
sql = CharPkModel.all().annotate(id_double=Concat(F("id"), F("id"))).sql()
if dialect == "mysql":
expected = "SELECT `id`,CONCAT(`id`,`id`) `id_double` FROM `charpkmodel`"
elif dialect == "postgres":
expected = 'SELECT "id",CONCAT("id"::text,"id"::text) "id_double" FROM "charpkmodel"'
else:
expected = 'SELECT "id",CONCAT("id","id") "id_double" FROM "charpkmodel"'
assert sql == expected
def test_annotate_coalesce_field_expression(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.all().annotate(num=Coalesce("intnum", F("intnum_null"))).values("num").sql()
if dialect == "mysql":
expected = "SELECT COALESCE(`intnum`,`intnum_null`) `num` FROM `intfields`"
elif dialect == "postgres":
expected = 'SELECT COALESCE("intnum","intnum_null") "num" FROM "intfields"'
else:
expected = 'SELECT COALESCE("intnum","intnum_null") "num" FROM "intfields"'
assert sql == expected
def test_annotate_function_join_expression(sql_context):
db, dialect, is_psycopg = sql_context
qset = Event.all().annotate(full_name=Concat("name", F("tournament__name"))).values("full_name")
sql = qset.sql()
join_match = (
r'LEFT OUTER JOIN [`"]tournament[`"] [`"]event__tournament[`"] ON '
r'[`"]event__tournament[`"]\.[`"]id[`"]=[`"]event[`"]\.[`"]tournament_id[`"]'
)
assert re.search(join_match, sql)
concat_match = (
r"CONCAT\(`?event`?\.`?name`?(?:::text)?\s*,\s*`?event__tournament`?\.`?name`?"
r"(?:::text)?\)"
r'|CONCAT\("event"\."name"(?:::text)?\s*,\s*"event__tournament"\."name"'
r"(?:::text)?\)"
)
assert re.search(concat_match, sql)
def test_values(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.filter(intnum=1).values("intnum").sql()
if dialect == "mysql":
expected = "SELECT `intnum` `intnum` FROM `intfields` WHERE `intnum`=%s"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT "intnum" "intnum" FROM "intfields" WHERE "intnum"=%s'
else:
expected = 'SELECT "intnum" "intnum" FROM "intfields" WHERE "intnum"=$1'
else:
expected = 'SELECT "intnum" "intnum" FROM "intfields" WHERE "intnum"=?'
assert sql == expected
def test_values_list(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.filter(intnum=1).values_list("intnum").sql()
if dialect == "mysql":
expected = "SELECT `intnum` `0` FROM `intfields` WHERE `intnum`=%s"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT "intnum" "0" FROM "intfields" WHERE "intnum"=%s'
else:
expected = 'SELECT "intnum" "0" FROM "intfields" WHERE "intnum"=$1'
else:
expected = 'SELECT "intnum" "0" FROM "intfields" WHERE "intnum"=?'
assert sql == expected
def test_exists(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.filter(intnum=1).exists().sql()
if dialect == "mysql":
expected = "SELECT 1 FROM `intfields` WHERE `intnum`=%s LIMIT %s"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT 1 FROM "intfields" WHERE "intnum"=%s LIMIT %s'
else:
expected = 'SELECT 1 FROM "intfields" WHERE "intnum"=$1 LIMIT $2'
elif dialect == "mssql":
expected = 'SELECT 1 FROM "intfields" WHERE "intnum"=? ORDER BY (SELECT 0) OFFSET 0 ROWS FETCH NEXT ? ROWS ONLY'
else:
expected = 'SELECT 1 FROM "intfields" WHERE "intnum"=? LIMIT ?'
assert sql == expected
def test_count(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.all().filter(intnum=1).count().sql()
if dialect == "mysql":
expected = "SELECT COUNT(*) FROM `intfields` WHERE `intnum`=%s"
elif dialect == "postgres":
if is_psycopg:
expected = 'SELECT COUNT(*) FROM "intfields" WHERE "intnum"=%s'
else:
expected = 'SELECT COUNT(*) FROM "intfields" WHERE "intnum"=$1'
else:
expected = 'SELECT COUNT(*) FROM "intfields" WHERE "intnum"=?'
assert sql == expected
def test_update(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.filter(intnum=2).update(intnum=1).sql()
if dialect == "mysql":
expected = "UPDATE `intfields` SET `intnum`=%s WHERE `intnum`=%s"
elif dialect == "postgres":
if is_psycopg:
expected = 'UPDATE "intfields" SET "intnum"=%s WHERE "intnum"=%s'
else:
expected = 'UPDATE "intfields" SET "intnum"=$1 WHERE "intnum"=$2'
else:
expected = 'UPDATE "intfields" SET "intnum"=? WHERE "intnum"=?'
assert sql == expected
def test_delete(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.filter(intnum=2).delete().sql()
if dialect == "mysql":
expected = "DELETE FROM `intfields` WHERE `intnum`=%s"
elif dialect == "postgres":
if is_psycopg:
expected = 'DELETE FROM "intfields" WHERE "intnum"=%s'
else:
expected = 'DELETE FROM "intfields" WHERE "intnum"=$1'
else:
expected = 'DELETE FROM "intfields" WHERE "intnum"=?'
assert sql == expected
@pytest.mark.asyncio
async def test_bulk_update(sql_context):
db, dialect, is_psycopg = sql_context
obj1 = await IntFields.create(intnum=1)
obj2 = await IntFields.create(intnum=2)
obj1.intnum = obj1.intnum + 1
obj2.intnum = obj2.intnum + 1
sql = IntFields.bulk_update([obj1, obj2], fields=["intnum"]).sql()
if dialect == "mysql":
expected = "UPDATE `intfields` SET `intnum`=CASE WHEN `id`=%s THEN %s WHEN `id`=%s THEN %s END WHERE `id` IN (%s,%s)"
elif dialect == "postgres":
if is_psycopg:
expected = 'UPDATE "intfields" SET "intnum"=CASE WHEN "id"=%s THEN CAST(%s AS INT) WHEN "id"=%s THEN CAST(%s AS INT) END WHERE "id" IN (%s,%s)'
else:
expected = 'UPDATE "intfields" SET "intnum"=CASE WHEN "id"=$1 THEN CAST($2 AS INT) WHEN "id"=$3 THEN CAST($4 AS INT) END WHERE "id" IN ($5,$6)'
else:
expected = 'UPDATE "intfields" SET "intnum"=CASE WHEN "id"=? THEN ? WHEN "id"=? THEN ? END WHERE "id" IN (?,?)'
assert sql == expected
@pytest.mark.asyncio
async def test_bulk_create_autogenerated_pk(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.bulk_create(
[IntFields(intnum=1, intnum_null=2), IntFields(intnum=3, intnum_null=4)]
).sql()
if dialect == "mysql":
expected = "INSERT INTO `intfields` (`intnum`,`intnum_null`) VALUES (%s,%s)"
elif dialect == "postgres":
if is_psycopg:
expected = (
'INSERT INTO "intfields" ("intnum","intnum_null") VALUES (%s,%s) RETURNING "id"'
)
else:
expected = (
'INSERT INTO "intfields" ("intnum","intnum_null") VALUES ($1,$2) RETURNING "id"'
)
else:
expected = 'INSERT INTO "intfields" ("intnum","intnum_null") VALUES (?,?)'
assert sql == expected
@pytest.mark.asyncio
async def test_bulk_create_specified_pk(sql_context):
db, dialect, is_psycopg = sql_context
sql = IntFields.bulk_create([IntFields(id=1, intnum=1), IntFields(id=2, intnum=2)]).sql()
if dialect == "mysql":
expected = "INSERT INTO `intfields` (`id`,`intnum`,`intnum_null`) VALUES (%s,%s,%s)"
elif dialect == "postgres":
if is_psycopg:
expected = 'INSERT INTO "intfields" ("id","intnum","intnum_null") VALUES (%s,%s,%s)'
else:
expected = 'INSERT INTO "intfields" ("id","intnum","intnum_null") VALUES ($1,$2,$3)'
else:
expected = 'INSERT INTO "intfields" ("id","intnum","intnum_null") VALUES (?,?,?)'
assert sql == expected
def test_m2m_filter_two_relations_same_target_produces_aliased_joins(sql_context):
"""Filtering on two M2M relations to the same target table should produce distinct aliased JOINs."""
db, dialect, is_psycopg = sql_context
sql = Drink.filter(flavors__name="vanilla", toppings__name="mint").sql()
if dialect == "mysql":
# MySQL uses backtick quoting
assert "`drink_flavor`" in sql
assert "`drink_topping`" in sql
assert "`drink__flavors`" in sql
assert "`drink__toppings`" in sql
else:
# Postgres and SQLite use double-quote quoting
assert '"drink_flavor"' in sql
assert '"drink_topping"' in sql
assert '"drink__flavors"' in sql
assert '"drink__toppings"' in sql
def test_update_with_like_filter(sql_context):
"""LIKE wildcards (%) in update queries must not conflict with %s parameter placeholders.
The LIKE pattern is passed as a parameterized value, so it never appears
inline in the SQL template and cannot conflict with driver-level %s
substitution (see https://github.com/tortoise/tortoise-orm/issues/1225).
"""
db, dialect, is_psycopg = sql_context
sql = Tournament.filter(name__contains="test").update(desc="updated").sql()
if dialect == "mysql":
expected = "UPDATE `tournament` SET `desc`=%s WHERE CAST(`name` AS CHAR) LIKE %s"
elif dialect == "postgres":
if is_psycopg:
expected = (
'UPDATE "tournament" SET "desc"=%s'
" WHERE CAST(\"name\" AS VARCHAR) LIKE %s ESCAPE '\\'"
)
else:
expected = (
'UPDATE "tournament" SET "desc"=$1'
" WHERE CAST(\"name\" AS VARCHAR) LIKE $2 ESCAPE '\\'"
)
else:
expected = (
'UPDATE "tournament" SET "desc"=? WHERE CAST("name" AS VARCHAR) LIKE ? ESCAPE \'\\\''
)
assert sql == expected