17
17
from django_mongodb_backend .fields import EmbeddedModelField
18
18
from django_mongodb_backend .models import EmbeddedModel
19
19
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
+ )
21
38
from .utils import truncate_ms
22
39
23
40
@@ -127,6 +144,59 @@ def setUpTestData(cls):
127
144
Review (title = "Classic" , rating = 7 ),
128
145
]
129
146
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
+ )
130
200
131
201
def test_filter_with_field (self ):
132
202
self .assertCountEqual (
@@ -139,6 +209,12 @@ def test_filter_with_model(self):
139
209
[self .clouds , self .frozen ],
140
210
)
141
211
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
+
142
218
143
219
class QueryingTests (TestCase ):
144
220
@classmethod
@@ -296,56 +372,6 @@ def test_nested(self):
296
372
self .assertCountEqual (Book .objects .filter (author__address__city = "NYC" ), [obj ])
297
373
298
374
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
-
349
375
class InvalidLookupTests (SimpleTestCase ):
350
376
def test_invalid_field (self ):
351
377
msg = "Author has no field named 'first_name'"
0 commit comments