Skip to content

Commit f735db9

Browse files
committed
Add unit test
1 parent ff62737 commit f735db9

File tree

2 files changed

+87
-57
lines changed

2 files changed

+87
-57
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):
@@ -192,3 +190,28 @@ class Movie(models.Model):
192190

193191
def __str__(self):
194192
return self.title
193+
194+
195+
class ArtifactDetail(EmbeddedModel):
196+
"""Details about a specific artifact."""
197+
198+
name = models.CharField(max_length=255)
199+
description = models.CharField(max_length=255)
200+
metadata = models.JSONField()
201+
202+
203+
class ExhibitSection(EmbeddedModel):
204+
"""A section within an exhibit, containing multiple artifacts."""
205+
206+
section_number = models.IntegerField()
207+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
208+
209+
210+
class MuseumExhibit(models.Model):
211+
"""An exhibit in the museum, composed of multiple sections."""
212+
213+
exhibit_name = models.CharField(max_length=255)
214+
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
215+
216+
def __str__(self):
217+
return self.exhibit_name

tests/model_fields_/test_embedded_model.py

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,16 @@
1919

2020

2121
from .models import (
22-
A,
2322
Address,
23+
ArtifactDetail,
2424
Author,
25-
B,
2625
Book,
27-
C,
28-
D,
2926
Data,
30-
E,
27+
ExhibitSection,
3128
Holder,
3229
Library,
3330
Movie,
31+
MuseumExhibit,
3432
NestedData,
3533
Review,
3634
)
@@ -148,6 +146,59 @@ def setUpTestData(cls):
148146
Review(title="Classic", rating=7),
149147
]
150148
cls.bears = Movie.objects.create(title="Bears", reviews=reviews)
149+
cls.egypt = MuseumExhibit.objects.create(
150+
exhibit_name="Ancient Egypt",
151+
sections=[
152+
ExhibitSection(
153+
section_number=1,
154+
artifacts=[
155+
ArtifactDetail(
156+
name="Ptolemaic Crown",
157+
description="Royal headpiece worn by Ptolemy kings.",
158+
metadata={
159+
"material": "gold",
160+
"origin": "Egypt",
161+
"era": "Ptolemaic Period",
162+
},
163+
)
164+
],
165+
)
166+
],
167+
)
168+
cls.wonders = MuseumExhibit.objects.create(
169+
exhibit_name="Wonders of the Ancient World",
170+
sections=[
171+
ExhibitSection(
172+
section_number=1,
173+
artifacts=[
174+
ArtifactDetail(
175+
name="Statue of Zeus",
176+
description="One of the Seven Wonders, created by Phidias.",
177+
metadata={"location": "Olympia", "height_m": 12},
178+
),
179+
ArtifactDetail(
180+
name="Hanging Gardens",
181+
description="Legendary gardens of Babylon.",
182+
metadata={"debated_existence": True},
183+
),
184+
],
185+
),
186+
ExhibitSection(
187+
section_number=2,
188+
artifacts=[
189+
ArtifactDetail(
190+
name="Lighthouse of Alexandria",
191+
description="Guided sailors safely to port.",
192+
metadata={"height_m": 100, "built": "3rd century BC"},
193+
)
194+
],
195+
),
196+
],
197+
)
198+
cls.new_descoveries = MuseumExhibit.objects.create(
199+
exhibit_name="New Discoveries",
200+
sections=[ExhibitSection(section_number=1, artifacts=[])],
201+
)
151202

152203
def test_filter_with_field(self):
153204
self.assertCountEqual(
@@ -160,6 +211,12 @@ def test_filter_with_model(self):
160211
[self.clouds, self.frozen],
161212
)
162213

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

164221
class QueryingTests(TestCase):
165222
@classmethod
@@ -279,56 +336,6 @@ def test_nested(self):
279336
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])
280337

281338

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

0 commit comments

Comments
 (0)