Skip to content

Commit 5e3d5d3

Browse files
committed
Add more unit test
1 parent f8d4a13 commit 5e3d5d3

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
@@ -192,12 +192,20 @@ def __str__(self):
192192
return self.title
193193

194194

195+
class RestorationRecord(EmbeddedModel):
196+
date = models.DateField()
197+
description = models.TextField()
198+
restored_by = models.CharField(max_length=255)
199+
200+
195201
class ArtifactDetail(EmbeddedModel):
196202
"""Details about a specific artifact."""
197203

198204
name = models.CharField(max_length=255)
199205
description = models.CharField(max_length=255)
200206
metadata = models.JSONField()
207+
restorations = EmbeddedModelArrayField(RestorationRecord, null=True)
208+
last_restoration = EmbeddedModelField(RestorationRecord, null=True)
201209

202210

203211
class ExhibitSection(EmbeddedModel):
@@ -207,11 +215,17 @@ class ExhibitSection(EmbeddedModel):
207215
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
208216

209217

218+
class ExhibitMeta(EmbeddedModel):
219+
curator_name = models.CharField(max_length=255)
220+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
221+
222+
210223
class MuseumExhibit(models.Model):
211224
"""An exhibit in the museum, composed of multiple sections."""
212225

213226
exhibit_name = models.CharField(max_length=255)
214227
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
228+
meta = EmbeddedModelField(ExhibitMeta, null=True)
215229

216230
def __str__(self):
217231
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
@@ -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)