Skip to content

Commit c357052

Browse files
committed
Add more unit test
1 parent 8079b1b commit c357052

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

tests/model_fields_/models.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,12 +165,20 @@ def __str__(self):
165165
return self.title
166166

167167

168+
class RestorationRecord(EmbeddedModel):
169+
date = models.DateField()
170+
description = models.TextField()
171+
restored_by = models.CharField(max_length=255)
172+
173+
168174
class ArtifactDetail(EmbeddedModel):
169175
"""Details about a specific artifact."""
170176

171177
name = models.CharField(max_length=255)
172178
description = models.CharField(max_length=255)
173179
metadata = models.JSONField()
180+
restorations = EmbeddedModelArrayField(RestorationRecord, null=True)
181+
last_restoration = EmbeddedModelField(RestorationRecord, null=True)
174182

175183

176184
class ExhibitSection(EmbeddedModel):
@@ -180,11 +188,17 @@ class ExhibitSection(EmbeddedModel):
180188
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
181189

182190

191+
class ExhibitMeta(EmbeddedModel):
192+
curator_name = models.CharField(max_length=255)
193+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
194+
195+
183196
class MuseumExhibit(models.Model):
184197
"""An exhibit in the museum, composed of multiple sections."""
185198

186199
exhibit_name = models.CharField(max_length=255)
187200
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
201+
meta = EmbeddedModelField(ExhibitMeta, null=True)
188202

189203
def __str__(self):
190204
return self.exhibit_name

tests/model_fields_/test_embedded_model.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import operator
2-
from datetime import timedelta
2+
from datetime import date, timedelta
33

44
from django.core.exceptions import FieldDoesNotExist, ValidationError
55
from django.db import models
@@ -23,12 +23,14 @@
2323
Author,
2424
Book,
2525
Data,
26+
ExhibitMeta,
2627
ExhibitSection,
2728
Holder,
2829
Library,
2930
Movie,
3031
MuseumExhibit,
3132
NestedData,
33+
RestorationRecord,
3234
Review,
3335
)
3436
from .utils import truncate_ms
@@ -198,6 +200,36 @@ def setUpTestData(cls):
198200
exhibit_name="New Discoveries",
199201
sections=[ExhibitSection(section_number=1, artifacts=[])],
200202
)
203+
cls.lost_empires = MuseumExhibit.objects.create(
204+
exhibit_name="Lost Empires",
205+
meta=ExhibitMeta(
206+
curator_name="Dr. Amina Hale",
207+
artifacts=[
208+
ArtifactDetail(
209+
name="Bronze Statue",
210+
description="Statue from the Hellenistic period.",
211+
metadata={"origin": "Pergamon", "material": "bronze"},
212+
restorations=[
213+
RestorationRecord(
214+
date=date(1998, 4, 15),
215+
description="Removed oxidized layer.",
216+
restored_by="Restoration Lab A",
217+
),
218+
RestorationRecord(
219+
date=date(2010, 7, 22),
220+
description="Reinforced the base structure.",
221+
restored_by="Dr. Liu Cheng",
222+
),
223+
],
224+
last_restoration=RestorationRecord(
225+
date=date(2010, 7, 22),
226+
description="Reinforced the base structure.",
227+
restored_by="Dr. Liu Cheng",
228+
),
229+
)
230+
],
231+
),
232+
)
201233

202234
def test_filter_with_field(self):
203235
self.assertCountEqual(
@@ -216,6 +248,14 @@ def test_filter_with_embeddedfield_path(self):
216248
[self.egypt, self.wonders, self.new_descoveries],
217249
)
218250

251+
def test_filter_with_embeddedfield_array_path(self):
252+
self.assertCountEqual(
253+
MuseumExhibit.objects.filter(
254+
meta__artifacts__restorations__0__restored_by="Restoration Lab A"
255+
),
256+
[self.lost_empires],
257+
)
258+
219259

220260
class QueryingTests(TestCase):
221261
@classmethod

0 commit comments

Comments
 (0)