|
| 1 | +"""The 0.14 name for the row-scoping hook must not be ignored in silence. |
| 2 | +
|
| 3 | +``get_queryset`` became ``scope_rows`` in 0.15. A class still carrying the old |
| 4 | +name looks scoped to its author and is not, so every row it was meant to hide |
| 5 | +is returned. The upgrade path has to fail loudly rather than quietly widen |
| 6 | +what a caller can read. |
| 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 | + |
| 16 | + |
| 17 | +def _orm(**kwargs): |
| 18 | + return StrawberryORM.for_sqlalchemy( |
| 19 | + dialect="sqlite", lazy_resolution="off", **kwargs |
| 20 | + ) |
| 21 | + |
| 22 | + |
| 23 | +class TestRenamedScopeHook: |
| 24 | + @pytest.fixture(autouse=True) |
| 25 | + def _session(self, sa_session, seed): |
| 26 | + self._s = sa_session |
| 27 | + |
| 28 | + def _build(self, orm): |
| 29 | + @orm.type(SAPost) |
| 30 | + class PostType: |
| 31 | + id: auto |
| 32 | + title: auto |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def get_queryset(cls, query, info): |
| 36 | + return query.where(SAPost.is_published.is_(True)) |
| 37 | + |
| 38 | + @strawberry.type |
| 39 | + class Query: |
| 40 | + posts: list[PostType] = orm.field.eager() |
| 41 | + |
| 42 | + return orm.schema(query=Query) |
| 43 | + |
| 44 | + def test_the_old_name_is_refused(self): |
| 45 | + with pytest.raises(ValueError, match="get_queryset"): |
| 46 | + self._build(_orm(warn_missing_scope=False)) |
| 47 | + |
| 48 | + def test_the_message_names_the_replacement(self): |
| 49 | + with pytest.raises(ValueError, match="scope_rows"): |
| 50 | + self._build(_orm(warn_missing_scope=False)) |
| 51 | + |
| 52 | + def test_it_is_refused_even_with_the_warning_turned_off(self): |
| 53 | + """warn_missing_scope covers an absent hook, not a misnamed one.""" |
| 54 | + with pytest.raises(ValueError): |
| 55 | + self._build(_orm(warn_missing_scope=False)) |
| 56 | + |
| 57 | + def test_the_new_name_still_works(self): |
| 58 | + orm = _orm(warn_missing_scope=False) |
| 59 | + |
| 60 | + @orm.type(SAPost) |
| 61 | + class PostType: |
| 62 | + id: auto |
| 63 | + title: auto |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def scope_rows(cls, query, info): |
| 67 | + return query.where(SAPost.is_published.is_(True)) |
| 68 | + |
| 69 | + @strawberry.type |
| 70 | + class Query: |
| 71 | + posts: list[PostType] = orm.field.eager() |
| 72 | + |
| 73 | + result = orm.schema(query=Query).execute_sync( |
| 74 | + "{ posts { title } }", context_value={"session": self._s} |
| 75 | + ) |
| 76 | + assert result.errors is None, result.errors |
| 77 | + titles = [p["title"] for p in result.data["posts"]] |
| 78 | + assert "Draft Post" not in titles, titles |
0 commit comments