|
| 1 | +"""One relation reached twice must not collide in the prefetch. |
| 2 | +
|
| 3 | +An interface query carries a sibling fragment per implementing type, so the |
| 4 | +same field is walked more than once and each pass builds its own queryset. |
| 5 | +Django compares prefetch querysets by identity and rejects a repeated target, |
| 6 | +which surfaced as ``'x' lookup was already seen with a different queryset``. |
| 7 | +""" |
| 8 | + |
| 9 | +import pytest |
| 10 | +import strawberry |
| 11 | + |
| 12 | +from strawberry_orm import StrawberryORM |
| 13 | +from strawberry_orm.types import auto |
| 14 | +from tests.backends.sqlalchemy.models import Post as SAPost |
| 15 | +from tests.backends.sqlalchemy.models import User as SAUser |
| 16 | + |
| 17 | + |
| 18 | +def _orm(): |
| 19 | + return StrawberryORM.for_sqlalchemy( |
| 20 | + dialect="sqlite", warn_missing_scope=False, lazy_resolution="off" |
| 21 | + ) |
| 22 | + |
| 23 | + |
| 24 | +class TestDuplicateLookups: |
| 25 | + @pytest.fixture(autouse=True) |
| 26 | + def _session(self, sa_session, seed): |
| 27 | + self._s = sa_session |
| 28 | + |
| 29 | + def test_the_same_on_field_under_two_fragments(self): |
| 30 | + orm = _orm() |
| 31 | + |
| 32 | + @orm.type(SAPost) |
| 33 | + class PostType: |
| 34 | + id: auto |
| 35 | + title: auto |
| 36 | + |
| 37 | + @orm.type(SAUser) |
| 38 | + class UserType: |
| 39 | + id: auto |
| 40 | + name: auto |
| 41 | + published: list[PostType] = orm.field.eager( |
| 42 | + on="posts", |
| 43 | + scope=lambda qs, info: qs.where(SAPost.is_published.is_(True)), |
| 44 | + ) |
| 45 | + |
| 46 | + @strawberry.type |
| 47 | + class Query: |
| 48 | + users: list[UserType] = orm.field.eager() |
| 49 | + |
| 50 | + # The field is named twice in one selection, which is what an |
| 51 | + # interface's sibling fragments produce. |
| 52 | + result = orm.schema(query=Query).execute_sync( |
| 53 | + """ |
| 54 | + { |
| 55 | + users { |
| 56 | + published { title } |
| 57 | + ... on UserType { published { title } } |
| 58 | + } |
| 59 | + } |
| 60 | + """, |
| 61 | + context_value={"session": self._s}, |
| 62 | + ) |
| 63 | + assert result.errors is None, result.errors |
| 64 | + titles = {p["title"] for u in result.data["users"] for p in u["published"]} |
| 65 | + assert "Draft Post" not in titles |
| 66 | + |
| 67 | + def test_an_on_field_also_named_in_using(self): |
| 68 | + """The optimizer reaches it twice: once selected, once as a hint.""" |
| 69 | + orm = _orm() |
| 70 | + |
| 71 | + @orm.type(SAPost) |
| 72 | + class PostType: |
| 73 | + id: auto |
| 74 | + title: auto |
| 75 | + |
| 76 | + @orm.type(SAUser) |
| 77 | + class UserType: |
| 78 | + id: auto |
| 79 | + name: auto |
| 80 | + published: list[PostType] = orm.field.eager( |
| 81 | + on="posts", |
| 82 | + scope=lambda qs, info: qs.where(SAPost.is_published.is_(True)), |
| 83 | + ) |
| 84 | + |
| 85 | + @orm.field.lazy(using=["posts"]) |
| 86 | + def post_count(self, info: strawberry.Info) -> int: |
| 87 | + return len(list(self.posts)) |
| 88 | + |
| 89 | + @strawberry.type |
| 90 | + class Query: |
| 91 | + users: list[UserType] = orm.field.eager() |
| 92 | + |
| 93 | + result = orm.schema(query=Query).execute_sync( |
| 94 | + "{ users { published { title } postCount } }", |
| 95 | + context_value={"session": self._s}, |
| 96 | + ) |
| 97 | + assert result.errors is None, result.errors |
0 commit comments