Skip to content

Commit f421254

Browse files
committed
Improve tests
1 parent 629eec3 commit f421254

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

pyproject.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ classifiers = [
3232
[project.urls]
3333
"Repository" = "https://github.com/xelixdev/django-memoized-prefetch"
3434

35+
[build-system]
36+
requires = ["hatchling"]
37+
build-backend = "hatchling.build"
38+
3539
[tool.ruff]
3640
line-length = 120
3741
target-version = "py39"

pytest.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@ DJANGO_SETTINGS_MODULE = tests.test_project.settings
33
addopts = --strict-markers --cov=django_memoized_prefetch --cov-report term-missing:skip-covered
44
pythonpath = .
55
django_find_project = false
6+
7+
filterwarnings =
8+
error::seal.exceptions.UnsealedAttributeAccess

tests/test_memoized_prefetch.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from django_memoized_prefetch import MemoizedPrefetch, MemoizedPrefetchConfig
88
from tests.test_project.test_app.factories import (
9+
SomeChildModelFactory,
910
SomeDifferentParentModelFactory,
1011
SomeModelFactory,
1112
SomeParentModelFactory,
@@ -115,6 +116,8 @@ def test_memoized_prefetch(
115116
with django_assert_num_queries(0): # all parents already fetched
116117
memoized_prefetch.process_chunk(objects)
117118

119+
assert objects
120+
118121
for obj in objects:
119122
# does not throw seal attribute -> fetched in process_chunk
120123
assert obj.some_parent_model is not None
@@ -151,6 +154,8 @@ def test_memoized_prefetch_many_to_many(
151154
with django_assert_num_queries(0): # already fetched
152155
memoized_prefetch.process_chunk(objects)
153156

157+
assert objects
158+
154159
for obj in objects:
155160
related_models = list(obj.some_related_models.all())
156161
assert len(related_models) == 2
@@ -178,22 +183,24 @@ def test_nullable_field(
178183
@pytest.fixture
179184
def child_models(self, objects_some_different_parent_a, objects_some_different_parent_b) -> list[SomeChildModel]:
180185
return [
181-
SomeChildModel(some_model=some_model)
186+
SomeChildModelFactory(some_model=some_model)
182187
for some_model in itertools.chain(objects_some_different_parent_a, objects_some_different_parent_b)
183188
]
184189

185190
def test_nested(self, child_models: list[SomeChildModel]):
186191
memoized_prefetch = MemoizedPrefetch(
187192
MemoizedPrefetchConfig(SomeParentModel, ["some_model__some_parent_model"], prefetch_all=True),
188193
MemoizedPrefetchConfig(
189-
SomeDifferentParentModel, ["some_model__some_other_different_parent"]
194+
SomeDifferentParentModel, ["some_model.some_other_different_parent"]
190195
), # support both . and __
191196
)
192197

193198
objects = list(SomeChildModel.objects.select_related("some_model").seal())
194199

195200
memoized_prefetch.process_chunk(objects)
196201

202+
assert objects
203+
197204
for obj in objects:
198205
# does not throw seal attribute -> fetched in process_chunk
199206
assert obj.some_model.some_parent_model is not None

tests/test_project/test_app/factories.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ def some_related_models(self, create: bool, extracted: list[SomeRelatedModel]) -
5353

5454
class Meta:
5555
model = SomeModel
56+
skip_postgeneration_save = True
5657

5758

5859
class SomeChildModelFactory(factory.django.DjangoModelFactory):

0 commit comments

Comments
 (0)