Skip to content

Commit 8cc88f1

Browse files
committed
Add unit test
1 parent 6183c7e commit 8cc88f1

File tree

2 files changed

+87
-52
lines changed

2 files changed

+87
-52
lines changed

tests/model_fields_/models.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,12 @@ class Address(EmbeddedModel):
126126
city = models.CharField(max_length=20)
127127
state = models.CharField(max_length=2)
128128
zip_code = models.IntegerField(db_index=True)
129-
tags = ArrayField(models.CharField(max_length=100), null=True, blank=True)
130129

131130

132131
class Author(EmbeddedModel):
133132
name = models.CharField(max_length=10)
134133
age = models.IntegerField()
135134
address = EmbeddedModelField(Address)
136-
skills = ArrayField(models.CharField(max_length=100), null=True, blank=True)
137135

138136

139137
class Book(models.Model):
@@ -165,3 +163,28 @@ class Movie(models.Model):
165163

166164
def __str__(self):
167165
return self.title
166+
167+
168+
class ArtifactDetail(EmbeddedModel):
169+
"""Details about a specific artifact."""
170+
171+
name = models.CharField(max_length=255)
172+
description = models.CharField(max_length=255)
173+
metadata = models.JSONField()
174+
175+
176+
class ExhibitSection(EmbeddedModel):
177+
"""A section within an exhibit, containing multiple artifacts."""
178+
179+
section_number = models.IntegerField()
180+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
181+
182+
183+
class MuseumExhibit(models.Model):
184+
"""An exhibit in the museum, composed of multiple sections."""
185+
186+
exhibit_name = models.CharField(max_length=255)
187+
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
188+
189+
def __str__(self):
190+
return self.exhibit_name

tests/model_fields_/test_embedded_model.py

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
from .models import (
2121
Address,
22+
ArtifactDetail,
2223
Author,
2324
Book,
2425
Data,
26+
ExhibitSection,
2527
Holder,
2628
Library,
2729
Movie,
30+
MuseumExhibit,
2831
NestedData,
2932
Review,
3033
)
@@ -142,6 +145,59 @@ def setUpTestData(cls):
142145
Review(title="Classic", rating=7),
143146
]
144147
cls.bears = Movie.objects.create(title="Bears", reviews=reviews)
148+
cls.egypt = MuseumExhibit.objects.create(
149+
exhibit_name="Ancient Egypt",
150+
sections=[
151+
ExhibitSection(
152+
section_number=1,
153+
artifacts=[
154+
ArtifactDetail(
155+
name="Ptolemaic Crown",
156+
description="Royal headpiece worn by Ptolemy kings.",
157+
metadata={
158+
"material": "gold",
159+
"origin": "Egypt",
160+
"era": "Ptolemaic Period",
161+
},
162+
)
163+
],
164+
)
165+
],
166+
)
167+
cls.wonders = MuseumExhibit.objects.create(
168+
exhibit_name="Wonders of the Ancient World",
169+
sections=[
170+
ExhibitSection(
171+
section_number=1,
172+
artifacts=[
173+
ArtifactDetail(
174+
name="Statue of Zeus",
175+
description="One of the Seven Wonders, created by Phidias.",
176+
metadata={"location": "Olympia", "height_m": 12},
177+
),
178+
ArtifactDetail(
179+
name="Hanging Gardens",
180+
description="Legendary gardens of Babylon.",
181+
metadata={"debated_existence": True},
182+
),
183+
],
184+
),
185+
ExhibitSection(
186+
section_number=2,
187+
artifacts=[
188+
ArtifactDetail(
189+
name="Lighthouse of Alexandria",
190+
description="Guided sailors safely to port.",
191+
metadata={"height_m": 100, "built": "3rd century BC"},
192+
)
193+
],
194+
),
195+
],
196+
)
197+
cls.new_descoveries = MuseumExhibit.objects.create(
198+
exhibit_name="New Discoveries",
199+
sections=[ExhibitSection(section_number=1, artifacts=[])],
200+
)
145201

146202
def test_filter_with_field(self):
147203
self.assertCountEqual(
@@ -154,6 +210,12 @@ def test_filter_with_model(self):
154210
[self.clouds, self.frozen],
155211
)
156212

213+
def test_filter_with_embeddedfield_path(self):
214+
self.assertCountEqual(
215+
MuseumExhibit.objects.filter(sections__0__section_number=1),
216+
[self.egypt, self.wonders, self.new_descoveries],
217+
)
218+
157219

158220
class QueryingTests(TestCase):
159221
@classmethod
@@ -273,56 +335,6 @@ def test_nested(self):
273335
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])
274336

275337

276-
class ArrayFieldTests(TestCase):
277-
@classmethod
278-
def setUpTestData(cls):
279-
cls.book = Book.objects.create(
280-
author=Author(
281-
name="Shakespeare",
282-
age=55,
283-
skills=["writing", "editing"],
284-
address=Address(city="NYC", state="NY", tags=["home", "shipping"]),
285-
),
286-
)
287-
288-
def test_contains(self):
289-
self.assertCountEqual(Book.objects.filter(author__skills__contains=["nonexistent"]), [])
290-
self.assertCountEqual(
291-
Book.objects.filter(author__skills__contains=["writing"]), [self.book]
292-
)
293-
# Nested
294-
self.assertCountEqual(
295-
Book.objects.filter(author__address__tags__contains=["nonexistent"]), []
296-
)
297-
self.assertCountEqual(
298-
Book.objects.filter(author__address__tags__contains=["home"]), [self.book]
299-
)
300-
301-
def test_contained_by(self):
302-
self.assertCountEqual(
303-
Book.objects.filter(author__skills__contained_by=["writing", "publishing"]), []
304-
)
305-
self.assertCountEqual(
306-
Book.objects.filter(author__skills__contained_by=["writing", "editing", "publishing"]),
307-
[self.book],
308-
)
309-
# Nested
310-
self.assertCountEqual(
311-
Book.objects.filter(author__address__tags__contained_by=["home", "work"]), []
312-
)
313-
self.assertCountEqual(
314-
Book.objects.filter(author__address__tags__contained_by=["home", "work", "shipping"]),
315-
[self.book],
316-
)
317-
318-
def test_len(self):
319-
self.assertCountEqual(Book.objects.filter(author__skills__len=1), [])
320-
self.assertCountEqual(Book.objects.filter(author__skills__len=2), [self.book])
321-
# Nested
322-
self.assertCountEqual(Book.objects.filter(author__address__tags__len=1), [])
323-
self.assertCountEqual(Book.objects.filter(author__address__tags__len=2), [self.book])
324-
325-
326338
class InvalidLookupTests(SimpleTestCase):
327339
def test_invalid_field(self):
328340
msg = "Author has no field named 'first_name'"

0 commit comments

Comments
 (0)