Skip to content

Commit 4f91ba9

Browse files
committed
Add unit test
1 parent 51de1cc commit 4f91ba9

File tree

2 files changed

+102
-53
lines changed

2 files changed

+102
-53
lines changed

tests/model_fields_/models.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,12 @@ class Address(EmbeddedModel):
115115
city = models.CharField(max_length=20)
116116
state = models.CharField(max_length=2)
117117
zip_code = models.IntegerField(db_index=True)
118-
tags = ArrayField(models.CharField(max_length=100), null=True, blank=True)
119118

120119

121120
class Author(EmbeddedModel):
122121
name = models.CharField(max_length=10)
123122
age = models.IntegerField()
124123
address = EmbeddedModelField(Address)
125-
skills = ArrayField(models.CharField(max_length=100), null=True, blank=True)
126124

127125

128126
class Book(models.Model):
@@ -182,3 +180,28 @@ class Movie(models.Model):
182180

183181
def __str__(self):
184182
return self.title
183+
184+
185+
class ArtifactDetail(EmbeddedModel):
186+
"""Details about a specific artifact."""
187+
188+
name = models.CharField(max_length=255)
189+
description = models.CharField(max_length=255)
190+
metadata = models.JSONField()
191+
192+
193+
class ExhibitSection(EmbeddedModel):
194+
"""A section within an exhibit, containing multiple artifacts."""
195+
196+
section_number = models.IntegerField()
197+
artifacts = EmbeddedModelArrayField(ArtifactDetail, null=True)
198+
199+
200+
class MuseumExhibit(models.Model):
201+
"""An exhibit in the museum, composed of multiple sections."""
202+
203+
exhibit_name = models.CharField(max_length=255)
204+
sections = EmbeddedModelArrayField(ExhibitSection, null=True)
205+
206+
def __str__(self):
207+
return self.exhibit_name

tests/model_fields_/test_embedded_model.py

Lines changed: 77 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,24 @@
1717
from django_mongodb_backend.fields import EmbeddedModelField
1818
from django_mongodb_backend.models import EmbeddedModel
1919

20-
from .models import A, Address, Author, B, Book, C, D, Data, E, Holder, Library, Movie, Review
20+
from .models import (
21+
A,
22+
Address,
23+
ArtifactDetail,
24+
Author,
25+
B,
26+
Book,
27+
C,
28+
D,
29+
Data,
30+
E,
31+
ExhibitSection,
32+
Holder,
33+
Library,
34+
Movie,
35+
MuseumExhibit,
36+
Review,
37+
)
2138
from .utils import truncate_ms
2239

2340

@@ -127,6 +144,59 @@ def setUpTestData(cls):
127144
Review(title="Classic", rating=7),
128145
]
129146
cls.bears = Movie.objects.create(title="Bears", reviews=reviews)
147+
cls.egypt = MuseumExhibit.objects.create(
148+
exhibit_name="Ancient Egypt",
149+
sections=[
150+
ExhibitSection(
151+
section_number=1,
152+
artifacts=[
153+
ArtifactDetail(
154+
name="Ptolemaic Crown",
155+
description="Royal headpiece worn by Ptolemy kings.",
156+
metadata={
157+
"material": "gold",
158+
"origin": "Egypt",
159+
"era": "Ptolemaic Period",
160+
},
161+
)
162+
],
163+
)
164+
],
165+
)
166+
cls.wonders = MuseumExhibit.objects.create(
167+
exhibit_name="Wonders of the Ancient World",
168+
sections=[
169+
ExhibitSection(
170+
section_number=1,
171+
artifacts=[
172+
ArtifactDetail(
173+
name="Statue of Zeus",
174+
description="One of the Seven Wonders, created by Phidias.",
175+
metadata={"location": "Olympia", "height_m": 12},
176+
),
177+
ArtifactDetail(
178+
name="Hanging Gardens",
179+
description="Legendary gardens of Babylon.",
180+
metadata={"debated_existence": True},
181+
),
182+
],
183+
),
184+
ExhibitSection(
185+
section_number=2,
186+
artifacts=[
187+
ArtifactDetail(
188+
name="Lighthouse of Alexandria",
189+
description="Guided sailors safely to port.",
190+
metadata={"height_m": 100, "built": "3rd century BC"},
191+
)
192+
],
193+
),
194+
],
195+
)
196+
cls.new_descoveries = MuseumExhibit.objects.create(
197+
exhibit_name="New Discoveries",
198+
sections=[ExhibitSection(section_number=1, artifacts=[])],
199+
)
130200

131201
def test_filter_with_field(self):
132202
self.assertCountEqual(
@@ -139,6 +209,12 @@ def test_filter_with_model(self):
139209
[self.clouds, self.frozen],
140210
)
141211

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

143219
class QueryingTests(TestCase):
144220
@classmethod
@@ -296,56 +372,6 @@ def test_nested(self):
296372
self.assertCountEqual(Book.objects.filter(author__address__city="NYC"), [obj])
297373

298374

299-
class ArrayFieldTests(TestCase):
300-
@classmethod
301-
def setUpTestData(cls):
302-
cls.book = Book.objects.create(
303-
author=Author(
304-
name="Shakespeare",
305-
age=55,
306-
skills=["writing", "editing"],
307-
address=Address(city="NYC", state="NY", tags=["home", "shipping"]),
308-
),
309-
)
310-
311-
def test_contains(self):
312-
self.assertCountEqual(Book.objects.filter(author__skills__contains=["nonexistent"]), [])
313-
self.assertCountEqual(
314-
Book.objects.filter(author__skills__contains=["writing"]), [self.book]
315-
)
316-
# Nested
317-
self.assertCountEqual(
318-
Book.objects.filter(author__address__tags__contains=["nonexistent"]), []
319-
)
320-
self.assertCountEqual(
321-
Book.objects.filter(author__address__tags__contains=["home"]), [self.book]
322-
)
323-
324-
def test_contained_by(self):
325-
self.assertCountEqual(
326-
Book.objects.filter(author__skills__contained_by=["writing", "publishing"]), []
327-
)
328-
self.assertCountEqual(
329-
Book.objects.filter(author__skills__contained_by=["writing", "editing", "publishing"]),
330-
[self.book],
331-
)
332-
# Nested
333-
self.assertCountEqual(
334-
Book.objects.filter(author__address__tags__contained_by=["home", "work"]), []
335-
)
336-
self.assertCountEqual(
337-
Book.objects.filter(author__address__tags__contained_by=["home", "work", "shipping"]),
338-
[self.book],
339-
)
340-
341-
def test_len(self):
342-
self.assertCountEqual(Book.objects.filter(author__skills__len=1), [])
343-
self.assertCountEqual(Book.objects.filter(author__skills__len=2), [self.book])
344-
# Nested
345-
self.assertCountEqual(Book.objects.filter(author__address__tags__len=1), [])
346-
self.assertCountEqual(Book.objects.filter(author__address__tags__len=2), [self.book])
347-
348-
349375
class InvalidLookupTests(SimpleTestCase):
350376
def test_invalid_field(self):
351377
msg = "Author has no field named 'first_name'"

0 commit comments

Comments
 (0)