Skip to content

Commit 85a6a9a

Browse files
committed
Add unsupported lookups test
1 parent 7f5c57f commit 85a6a9a

File tree

2 files changed

+22
-25
lines changed

2 files changed

+22
-25
lines changed

tests/model_fields_/models.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,13 @@ def __str__(self):
169169

170170
class RestorationRecord(EmbeddedModel):
171171
date = models.DateField()
172-
description = models.TextField()
173172
restored_by = models.CharField(max_length=255)
174173

175174

176175
class ArtifactDetail(EmbeddedModel):
177176
"""Details about a specific artifact."""
178177

179178
name = models.CharField(max_length=255)
180-
description = models.CharField(max_length=255)
181179
metadata = models.JSONField()
182180
restorations = EmbeddedModelArrayField(RestorationRecord, null=True)
183181
last_restoration = EmbeddedModelField(RestorationRecord, null=True)

tests/model_fields_/test_embedded_model_array.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from datetime import date
22

3-
from django.db import models
3+
from django.db import connection, models
44
from django.test import SimpleTestCase, TestCase
5-
from django.test.utils import isolate_apps
5+
from django.test.utils import CaptureQueriesContext, isolate_apps
66

77
from django_mongodb_backend.fields import EmbeddedModelArrayField
88
from django_mongodb_backend.models import EmbeddedModel
@@ -93,11 +93,8 @@ def setUpTestData(cls):
9393
artifacts=[
9494
ArtifactDetail(
9595
name="Ptolemaic Crown",
96-
description="Royal headpiece worn by Ptolemy kings.",
9796
metadata={
98-
"material": "gold",
9997
"origin": "Egypt",
100-
"era": "Ptolemaic Period",
10198
},
10299
)
103100
],
@@ -112,57 +109,50 @@ def setUpTestData(cls):
112109
artifacts=[
113110
ArtifactDetail(
114111
name="Statue of Zeus",
115-
description="One of the Seven Wonders, created by Phidias.",
116112
metadata={"location": "Olympia", "height_m": 12},
117113
),
118114
ArtifactDetail(
119115
name="Hanging Gardens",
120-
description="Legendary gardens of Babylon.",
121-
metadata={"debated_existence": True},
122116
),
123117
],
124118
),
119+
],
120+
)
121+
cls.new_descoveries = MuseumExhibit.objects.create(
122+
exhibit_name="New Discoveries",
123+
sections=[
125124
ExhibitSection(
126-
section_number=2,
125+
section_number=1,
127126
artifacts=[
128127
ArtifactDetail(
129128
name="Lighthouse of Alexandria",
130-
description="Guided sailors safely to port.",
131129
metadata={"height_m": 100, "built": "3rd century BC"},
132130
)
133131
],
134-
),
132+
)
135133
],
136134
)
137-
cls.new_descoveries = MuseumExhibit.objects.create(
138-
exhibit_name="New Discoveries",
139-
sections=[ExhibitSection(section_number=1, artifacts=[])],
140-
)
141135
cls.lost_empires = MuseumExhibit.objects.create(
142136
exhibit_name="Lost Empires",
143137
main_section=ExhibitSection(
144138
section_number=3,
145139
artifacts=[
146140
ArtifactDetail(
147141
name="Bronze Statue",
148-
description="Statue from the Hellenistic period.",
149-
metadata={"origin": "Pergamon", "material": "bronze"},
142+
metadata={"origin": "Pergamon"},
150143
restorations=[
151144
RestorationRecord(
152145
date=date(1998, 4, 15),
153-
description="Removed oxidized layer.",
154-
restored_by="Restoration Lab A",
146+
restored_by="Zacarias",
155147
),
156148
RestorationRecord(
157149
date=date(2010, 7, 22),
158-
description="Reinforced the base structure.",
159-
restored_by="Dr. Liu Cheng",
150+
restored_by="Vicente",
160151
),
161152
],
162153
last_restoration=RestorationRecord(
163154
date=date(2010, 7, 22),
164-
description="Reinforced the base structure.",
165-
restored_by="Dr. Liu Cheng",
155+
restored_by="Monzon",
166156
),
167157
)
168158
],
@@ -188,6 +178,15 @@ def test_filter_with_embeddedfield_array_path(self):
188178
[self.lost_empires],
189179
)
190180

181+
def test_filter_unsupported_lookups(self):
182+
# handle the unsupported lookups as key in a keytransform
183+
184+
for lookup in ["contained_by", "contains", "contains", "range"]:
185+
kwargs = {f"main_section__artifacts__metadata__origin__{lookup}": ["Pergamon", "Egypt"]}
186+
with CaptureQueriesContext(connection) as captured_queries:
187+
self.assertCountEqual(MuseumExhibit.objects.filter(**kwargs), [])
188+
self.assertIn(f"'field': '{lookup}'", captured_queries[0]["sql"])
189+
191190
def test_len(self):
192191
self.assertCountEqual(MuseumExhibit.objects.filter(sections__len=10), [])
193192
self.assertCountEqual(

0 commit comments

Comments
 (0)