19
19
20
20
from .models import (
21
21
Address ,
22
+ ArtifactDetail ,
22
23
Author ,
23
24
Book ,
24
25
Data ,
26
+ ExhibitSection ,
25
27
Holder ,
26
28
Library ,
27
29
Movie ,
30
+ MuseumExhibit ,
28
31
NestedData ,
29
32
Review ,
30
33
)
@@ -142,6 +145,59 @@ def setUpTestData(cls):
142
145
Review (title = "Classic" , rating = 7 ),
143
146
]
144
147
cls .bears = Movie .objects .create (title = "Bears" , reviews = reviews )
148
+ cls .egypt = MuseumExhibit .objects .create (
149
+ exhibit_name = "Ancient Egypt" ,
150
+ sections = [
151
+ ExhibitSection (
152
+ section_number = 1 ,
153
+ artifacts = [
154
+ ArtifactDetail (
155
+ name = "Ptolemaic Crown" ,
156
+ description = "Royal headpiece worn by Ptolemy kings." ,
157
+ metadata = {
158
+ "material" : "gold" ,
159
+ "origin" : "Egypt" ,
160
+ "era" : "Ptolemaic Period" ,
161
+ },
162
+ )
163
+ ],
164
+ )
165
+ ],
166
+ )
167
+ cls .wonders = MuseumExhibit .objects .create (
168
+ exhibit_name = "Wonders of the Ancient World" ,
169
+ sections = [
170
+ ExhibitSection (
171
+ section_number = 1 ,
172
+ artifacts = [
173
+ ArtifactDetail (
174
+ name = "Statue of Zeus" ,
175
+ description = "One of the Seven Wonders, created by Phidias." ,
176
+ metadata = {"location" : "Olympia" , "height_m" : 12 },
177
+ ),
178
+ ArtifactDetail (
179
+ name = "Hanging Gardens" ,
180
+ description = "Legendary gardens of Babylon." ,
181
+ metadata = {"debated_existence" : True },
182
+ ),
183
+ ],
184
+ ),
185
+ ExhibitSection (
186
+ section_number = 2 ,
187
+ artifacts = [
188
+ ArtifactDetail (
189
+ name = "Lighthouse of Alexandria" ,
190
+ description = "Guided sailors safely to port." ,
191
+ metadata = {"height_m" : 100 , "built" : "3rd century BC" },
192
+ )
193
+ ],
194
+ ),
195
+ ],
196
+ )
197
+ cls .new_descoveries = MuseumExhibit .objects .create (
198
+ exhibit_name = "New Discoveries" ,
199
+ sections = [ExhibitSection (section_number = 1 , artifacts = [])],
200
+ )
145
201
146
202
def test_filter_with_field (self ):
147
203
self .assertCountEqual (
@@ -154,6 +210,12 @@ def test_filter_with_model(self):
154
210
[self .clouds , self .frozen ],
155
211
)
156
212
213
+ def test_filter_with_embeddedfield_path (self ):
214
+ self .assertCountEqual (
215
+ MuseumExhibit .objects .filter (sections__0__section_number = 1 ),
216
+ [self .egypt , self .wonders , self .new_descoveries ],
217
+ )
218
+
157
219
158
220
class QueryingTests (TestCase ):
159
221
@classmethod
@@ -273,56 +335,6 @@ def test_nested(self):
273
335
self .assertCountEqual (Book .objects .filter (author__address__city = "NYC" ), [obj ])
274
336
275
337
276
- class ArrayFieldTests (TestCase ):
277
- @classmethod
278
- def setUpTestData (cls ):
279
- cls .book = Book .objects .create (
280
- author = Author (
281
- name = "Shakespeare" ,
282
- age = 55 ,
283
- skills = ["writing" , "editing" ],
284
- address = Address (city = "NYC" , state = "NY" , tags = ["home" , "shipping" ]),
285
- ),
286
- )
287
-
288
- def test_contains (self ):
289
- self .assertCountEqual (Book .objects .filter (author__skills__contains = ["nonexistent" ]), [])
290
- self .assertCountEqual (
291
- Book .objects .filter (author__skills__contains = ["writing" ]), [self .book ]
292
- )
293
- # Nested
294
- self .assertCountEqual (
295
- Book .objects .filter (author__address__tags__contains = ["nonexistent" ]), []
296
- )
297
- self .assertCountEqual (
298
- Book .objects .filter (author__address__tags__contains = ["home" ]), [self .book ]
299
- )
300
-
301
- def test_contained_by (self ):
302
- self .assertCountEqual (
303
- Book .objects .filter (author__skills__contained_by = ["writing" , "publishing" ]), []
304
- )
305
- self .assertCountEqual (
306
- Book .objects .filter (author__skills__contained_by = ["writing" , "editing" , "publishing" ]),
307
- [self .book ],
308
- )
309
- # Nested
310
- self .assertCountEqual (
311
- Book .objects .filter (author__address__tags__contained_by = ["home" , "work" ]), []
312
- )
313
- self .assertCountEqual (
314
- Book .objects .filter (author__address__tags__contained_by = ["home" , "work" , "shipping" ]),
315
- [self .book ],
316
- )
317
-
318
- def test_len (self ):
319
- self .assertCountEqual (Book .objects .filter (author__skills__len = 1 ), [])
320
- self .assertCountEqual (Book .objects .filter (author__skills__len = 2 ), [self .book ])
321
- # Nested
322
- self .assertCountEqual (Book .objects .filter (author__address__tags__len = 1 ), [])
323
- self .assertCountEqual (Book .objects .filter (author__address__tags__len = 2 ), [self .book ])
324
-
325
-
326
338
class InvalidLookupTests (SimpleTestCase ):
327
339
def test_invalid_field (self ):
328
340
msg = "Author has no field named 'first_name'"
0 commit comments