MCPcopy Index your code
hub / github.com/MongoEngine/mongoengine / test_recursive_reference

Method test_recursive_reference

tests/test_dereference.py:274–338  ·  view source on GitHub ↗

Ensure that ReferenceFields can reference their own documents.

(self)

Source from the content-addressed store, hash-verified

272 assert isinstance(raw_data["members"][0], ObjectId)
273
274 def test_recursive_reference(self):
275 """Ensure that ReferenceFields can reference their own documents."""
276
277 class Employee(Document):
278 name = StringField()
279 boss = ReferenceField("self")
280 friends = ListField(ReferenceField("self"))
281
282 Employee.drop_collection()
283
284 bill = Employee(name="Bill Lumbergh")
285 bill.save()
286
287 michael = Employee(name="Michael Bolton")
288 michael.save()
289
290 samir = Employee(name="Samir Nagheenanajar")
291 samir.save()
292
293 friends = [michael, samir]
294 peter = Employee(name="Peter Gibbons", boss=bill, friends=friends)
295 peter.save()
296
297 Employee(name="Funky Gibbon", boss=bill, friends=friends).save()
298 Employee(name="Funky Gibbon", boss=bill, friends=friends).save()
299 Employee(name="Funky Gibbon", boss=bill, friends=friends).save()
300
301 with query_counter() as q:
302 assert q == 0
303
304 peter = Employee.objects.with_id(peter.id)
305 assert q == 1
306
307 peter.boss
308 assert q == 2
309
310 peter.friends
311 assert q == 3
312
313 # Document select_related
314 with query_counter() as q:
315 assert q == 0
316
317 peter = Employee.objects.with_id(peter.id).select_related()
318 assert q == 2
319
320 assert peter.boss == bill
321 assert q == 2
322
323 assert peter.friends == friends
324 assert q == 2
325
326 # Queryset select_related
327 with query_counter() as q:
328 assert q == 0
329
330 employees = Employee.objects(boss=bill).select_related()
331 assert q == 2

Callers

nothing calls this directly

Calls 7

query_counterClass · 0.90
drop_collectionMethod · 0.80
with_idMethod · 0.80
EmployeeClass · 0.70
saveMethod · 0.45
select_relatedMethod · 0.45
objectsMethod · 0.45

Tested by

no test coverage detected