Skip to content

Commit

Permalink
feat: add filtered subquery update test (#1838)
Browse files Browse the repository at this point in the history
fixes #1802
  • Loading branch information
Invisi authored Jan 4, 2025
1 parent 7afc90e commit 0aee52d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion tests/test_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
Event,
IntFields,
JSONFields,
Reporter,
Service,
SmallIntFields,
SourceFieldPk,
Expand All @@ -21,7 +22,7 @@
)
from tortoise.contrib import test
from tortoise.contrib.test.condition import In, NotEQ
from tortoise.expressions import Case, F, Q, When
from tortoise.expressions import Case, F, Q, Subquery, When
from tortoise.functions import Function, Upper


Expand Down Expand Up @@ -246,3 +247,18 @@ async def test_update_with_function_annotation(self):
.update(name=F("upped_name"))
)
self.assertEqual((await Tournament.get(pk=tournament.pk)).name, "AAA")

async def test_update_with_filter_subquery(self):
t1 = await Tournament.create(name="1")
r1 = await Reporter.create(name="1")
e1 = await Event.create(name="1", tournament=t1, reporter=r1)

# NOTE: this is intentionally written with Subquery and known PKs to test
# whether subqueries are parameterized correctly.
await Event.filter(
tournament_id__in=Subquery(Tournament.filter(pk__in=[t1.pk]).values("id")),
reporter_id__in=Subquery(Reporter.filter(pk__in=[r1.pk]).values("id")),
).update(token="hello_world")

await e1.refresh_from_db(fields=["token"])
self.assertEqual(e1.token, "hello_world")

0 comments on commit 0aee52d

Please sign in to comment.