Skip to content

Commit 0aee52d

Browse files
authored
feat: add filtered subquery update test (#1838)
fixes #1802
1 parent 7afc90e commit 0aee52d

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

tests/test_update.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
Event,
1414
IntFields,
1515
JSONFields,
16+
Reporter,
1617
Service,
1718
SmallIntFields,
1819
SourceFieldPk,
@@ -21,7 +22,7 @@
2122
)
2223
from tortoise.contrib import test
2324
from tortoise.contrib.test.condition import In, NotEQ
24-
from tortoise.expressions import Case, F, Q, When
25+
from tortoise.expressions import Case, F, Q, Subquery, When
2526
from tortoise.functions import Function, Upper
2627

2728

@@ -246,3 +247,18 @@ async def test_update_with_function_annotation(self):
246247
.update(name=F("upped_name"))
247248
)
248249
self.assertEqual((await Tournament.get(pk=tournament.pk)).name, "AAA")
250+
251+
async def test_update_with_filter_subquery(self):
252+
t1 = await Tournament.create(name="1")
253+
r1 = await Reporter.create(name="1")
254+
e1 = await Event.create(name="1", tournament=t1, reporter=r1)
255+
256+
# NOTE: this is intentionally written with Subquery and known PKs to test
257+
# whether subqueries are parameterized correctly.
258+
await Event.filter(
259+
tournament_id__in=Subquery(Tournament.filter(pk__in=[t1.pk]).values("id")),
260+
reporter_id__in=Subquery(Reporter.filter(pk__in=[r1.pk]).values("id")),
261+
).update(token="hello_world")
262+
263+
await e1.refresh_from_db(fields=["token"])
264+
self.assertEqual(e1.token, "hello_world")

0 commit comments

Comments
 (0)