Skip to content

Commit b15263d

Browse files
committed
Add more unit test
1 parent 1d6917e commit b15263d

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
@@ -188,12 +188,20 @@ def __str__(self):
188188
return self.title
189189

190190

191+
class RestorationRecord(EmbeddedModel):
192+
date = models.DateField()
193+
description = models.TextField()
194+
restored_by = models.CharField(max_length=255)
195+
196+
191197
class ArtifactDetail(EmbeddedModel):
192198
"""Details about a specific artifact."""
193199

194200
name = models.CharField(max_length=255)
195201
description = models.CharField(max_length=255)
196202
metadata = models.JSONField()
203+
restorations = EmbeddedModelArrayField(RestorationRecord, null=True)
204+
last_restoration = EmbeddedModelField(RestorationRecord, null=True)
197205

198206

199207
class ExhibitSection(EmbeddedModel):
@@ -203,11 +211,17 @@ class ExhibitSection(EmbeddedModel):
203211
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
204212

205213

214+
class ExhibitMeta(EmbeddedModel):
215+
curator_name = models.CharField(max_length=255)
216+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
217+
218+
206219
class MuseumExhibit(models.Model):
207220
"""An exhibit in the museum, composed of multiple sections."""
208221

209222
exhibit_name = models.CharField(max_length=255)
210223
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
224+
meta = EmbeddedModelField(ExhibitMeta, null=True)
211225

212226
def __str__(self):
213227
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 connection, models
@@ -24,12 +24,14 @@
2424
Author,
2525
Book,
2626
Data,
27+
ExhibitMeta,
2728
ExhibitSection,
2829
Holder,
2930
Library,
3031
Movie,
3132
MuseumExhibit,
3233
NestedData,
34+
RestorationRecord,
3335
Review,
3436
)
3537
from .utils import truncate_ms
@@ -199,6 +201,36 @@ def setUpTestData(cls):
199201
exhibit_name="New Discoveries",
200202
sections=[ExhibitSection(section_number=1, artifacts=[])],
201203
)
204+
cls.lost_empires = MuseumExhibit.objects.create(
205+
exhibit_name="Lost Empires",
206+
meta=ExhibitMeta(
207+
curator_name="Dr. Amina Hale",
208+
artifacts=[
209+
ArtifactDetail(
210+
name="Bronze Statue",
211+
description="Statue from the Hellenistic period.",
212+
metadata={"origin": "Pergamon", "material": "bronze"},
213+
restorations=[
214+
RestorationRecord(
215+
date=date(1998, 4, 15),
216+
description="Removed oxidized layer.",
217+
restored_by="Restoration Lab A",
218+
),
219+
RestorationRecord(
220+
date=date(2010, 7, 22),
221+
description="Reinforced the base structure.",
222+
restored_by="Dr. Liu Cheng",
223+
),
224+
],
225+
last_restoration=RestorationRecord(
226+
date=date(2010, 7, 22),
227+
description="Reinforced the base structure.",
228+
restored_by="Dr. Liu Cheng",
229+
),
230+
)
231+
],
232+
),
233+
)
202234

203235
def test_filter_with_field(self):
204236
self.assertCountEqual(
@@ -217,6 +249,14 @@ def test_filter_with_embeddedfield_path(self):
217249
[self.egypt, self.wonders, self.new_descoveries],
218250
)
219251

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

221261
class QueryingTests(TestCase):
222262
@classmethod

0 commit comments

Comments
 (0)