Skip to content

Commit 5323eba

Browse files
committed
Add more unit test
1 parent c963470 commit 5323eba

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

189189

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

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

197205

198206
class ExhibitSection(EmbeddedModel):
@@ -202,11 +210,17 @@ class ExhibitSection(EmbeddedModel):
202210
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
203211

204212

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

208221
exhibit_name = models.CharField(max_length=255)
209222
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
223+
meta = EmbeddedModelField(ExhibitMeta, null=True)
210224

211225
def __str__(self):
212226
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)