Skip to content

Commit bf4cec1

Browse files
committed
Add more unit test
1 parent 4fee413 commit bf4cec1

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
@@ -182,12 +182,20 @@ def __str__(self):
182182
return self.title
183183

184184

185+
class RestorationRecord(EmbeddedModel):
186+
date = models.DateField()
187+
description = models.TextField()
188+
restored_by = models.CharField(max_length=255)
189+
190+
185191
class ArtifactDetail(EmbeddedModel):
186192
"""Details about a specific artifact."""
187193

188194
name = models.CharField(max_length=255)
189195
description = models.CharField(max_length=255)
190196
metadata = models.JSONField()
197+
restorations = EmbeddedModelArrayField(RestorationRecord, null=True)
198+
last_restoration = EmbeddedModelField(RestorationRecord, null=True)
191199

192200

193201
class ExhibitSection(EmbeddedModel):
@@ -197,11 +205,17 @@ class ExhibitSection(EmbeddedModel):
197205
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
198206

199207

208+
class ExhibitMeta(EmbeddedModel):
209+
curator_name = models.CharField(max_length=255)
210+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
211+
212+
200213
class MuseumExhibit(models.Model):
201214
"""An exhibit in the museum, composed of multiple sections."""
202215

203216
exhibit_name = models.CharField(max_length=255)
204217
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
218+
meta = EmbeddedModelField(ExhibitMeta, null=True)
205219

206220
def __str__(self):
207221
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
@@ -28,11 +28,13 @@
2828
D,
2929
Data,
3030
E,
31+
ExhibitMeta,
3132
ExhibitSection,
3233
Holder,
3334
Library,
3435
Movie,
3536
MuseumExhibit,
37+
RestorationRecord,
3638
Review,
3739
)
3840
from .utils import truncate_ms
@@ -197,6 +199,36 @@ def setUpTestData(cls):
197199
exhibit_name="New Discoveries",
198200
sections=[ExhibitSection(section_number=1, artifacts=[])],
199201
)
202+
cls.lost_empires = MuseumExhibit.objects.create(
203+
exhibit_name="Lost Empires",
204+
meta=ExhibitMeta(
205+
curator_name="Dr. Amina Hale",
206+
artifacts=[
207+
ArtifactDetail(
208+
name="Bronze Statue",
209+
description="Statue from the Hellenistic period.",
210+
metadata={"origin": "Pergamon", "material": "bronze"},
211+
restorations=[
212+
RestorationRecord(
213+
date=date(1998, 4, 15),
214+
description="Removed oxidized layer.",
215+
restored_by="Restoration Lab A",
216+
),
217+
RestorationRecord(
218+
date=date(2010, 7, 22),
219+
description="Reinforced the base structure.",
220+
restored_by="Dr. Liu Cheng",
221+
),
222+
],
223+
last_restoration=RestorationRecord(
224+
date=date(2010, 7, 22),
225+
description="Reinforced the base structure.",
226+
restored_by="Dr. Liu Cheng",
227+
),
228+
)
229+
],
230+
),
231+
)
200232

201233
def test_filter_with_field(self):
202234
self.assertCountEqual(
@@ -215,6 +247,14 @@ def test_filter_with_embeddedfield_path(self):
215247
[self.egypt, self.wonders, self.new_descoveries],
216248
)
217249

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

219259
class QueryingTests(TestCase):
220260
@classmethod

0 commit comments

Comments
 (0)