19
19
20
20
21
21
from .models import (
22
- A ,
23
22
Address ,
23
+ ArtifactDetail ,
24
24
Author ,
25
- B ,
26
25
Book ,
27
- C ,
28
- D ,
29
26
Data ,
30
- E ,
27
+ ExhibitSection ,
31
28
Holder ,
32
29
Library ,
33
30
Movie ,
31
+ MuseumExhibit ,
34
32
NestedData ,
35
33
Review ,
36
34
)
@@ -148,6 +146,59 @@ def setUpTestData(cls):
148
146
Review (title = "Classic" , rating = 7 ),
149
147
]
150
148
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
+ )
151
202
152
203
def test_filter_with_field (self ):
153
204
self .assertCountEqual (
@@ -160,6 +211,12 @@ def test_filter_with_model(self):
160
211
[self .clouds , self .frozen ],
161
212
)
162
213
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
+
163
220
164
221
class QueryingTests (TestCase ):
165
222
@classmethod
@@ -339,56 +396,6 @@ def test_nested(self):
339
396
self .assertCountEqual (Book .objects .filter (author__address__city = "NYC" ), [obj ])
340
397
341
398
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
-
392
399
class InvalidLookupTests (SimpleTestCase ):
393
400
def test_invalid_field (self ):
394
401
msg = "Author has no field named 'first_name'"
0 commit comments