Skip to content

Commit b74fe12

Browse files
committed
Add unit test
1 parent b6bedcb commit b74fe12

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
@@ -121,14 +121,12 @@ class Address(EmbeddedModel):
121121
city = models.CharField(max_length=20)
122122
state = models.CharField(max_length=2)
123123
zip_code = models.IntegerField(db_index=True)
124-
tags = ArrayField(models.CharField(max_length=100), null=True, blank=True)
125124

126125

127126
class Author(EmbeddedModel):
128127
name = models.CharField(max_length=10)
129128
age = models.IntegerField()
130129
address = EmbeddedModelField(Address)
131-
skills = ArrayField(models.CharField(max_length=100), null=True, blank=True)
132130

133131

134132
class Book(models.Model):
@@ -188,3 +186,28 @@ class Movie(models.Model):
188186

189187
def __str__(self):
190188
return self.title
189+
190+
191+
class ArtifactDetail(EmbeddedModel):
192+
"""Details about a specific artifact."""
193+
194+
name = models.CharField(max_length=255)
195+
description = models.CharField(max_length=255)
196+
metadata = models.JSONField()
197+
198+
199+
class ExhibitSection(EmbeddedModel):
200+
"""A section within an exhibit, containing multiple artifacts."""
201+
202+
section_number = models.IntegerField()
203+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
204+
205+
206+
class MuseumExhibit(models.Model):
207+
"""An exhibit in the museum, composed of multiple sections."""
208+
209+
exhibit_name = models.CharField(max_length=255)
210+
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
211+
212+
def __str__(self):
213+
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
@@ -339,56 +396,6 @@ def test_nested(self):
339396
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])
340397

341398

342-
class ArrayFieldTests(TestCase):
343-
@classmethod
344-
def setUpTestData(cls):
345-
cls.book = Book.objects.create(
346-
author=Author(
347-
name="Shakespeare",
348-
age=55,
349-
skills=["writing", "editing"],
350-
address=Address(city="NYC", state="NY", tags=["home", "shipping"]),
351-
),
352-
)
353-
354-
def test_contains(self):
355-
self.assertCountEqual(Book.objects.filter(author__skills__contains=["nonexistent"]), [])
356-
self.assertCountEqual(
357-
Book.objects.filter(author__skills__contains=["writing"]), [self.book]
358-
)
359-
# Nested
360-
self.assertCountEqual(
361-
Book.objects.filter(author__address__tags__contains=["nonexistent"]), []
362-
)
363-
self.assertCountEqual(
364-
Book.objects.filter(author__address__tags__contains=["home"]), [self.book]
365-
)
366-
367-
def test_contained_by(self):
368-
self.assertCountEqual(
369-
Book.objects.filter(author__skills__contained_by=["writing", "publishing"]), []
370-
)
371-
self.assertCountEqual(
372-
Book.objects.filter(author__skills__contained_by=["writing", "editing", "publishing"]),
373-
[self.book],
374-
)
375-
# Nested
376-
self.assertCountEqual(
377-
Book.objects.filter(author__address__tags__contained_by=["home", "work"]), []
378-
)
379-
self.assertCountEqual(
380-
Book.objects.filter(author__address__tags__contained_by=["home", "work", "shipping"]),
381-
[self.book],
382-
)
383-
384-
def test_len(self):
385-
self.assertCountEqual(Book.objects.filter(author__skills__len=1), [])
386-
self.assertCountEqual(Book.objects.filter(author__skills__len=2), [self.book])
387-
# Nested
388-
self.assertCountEqual(Book.objects.filter(author__address__tags__len=1), [])
389-
self.assertCountEqual(Book.objects.filter(author__address__tags__len=2), [self.book])
390-
391-
392399
class InvalidLookupTests(SimpleTestCase):
393400
def test_invalid_field(self):
394401
msg = "Author has no field named 'first_name'"

0 commit comments

Comments
 (0)