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

Method test_reference_miss

tests/fields/test_fields.py:1356–1390  ·  view source on GitHub ↗

Ensure an exception is raised when dereferencing an unknown document.

(self)

Source from the content-addressed store, hash-verified

1354 assert content == User.objects.first().groups[0].content
1355
1356 def test_reference_miss(self):
1357 """Ensure an exception is raised when dereferencing an unknown
1358 document.
1359 """
1360
1361 class Foo(Document):
1362 pass
1363
1364 class Bar(Document):
1365 ref = ReferenceField(Foo)
1366 generic_ref = GenericReferenceField()
1367
1368 Foo.drop_collection()
1369 Bar.drop_collection()
1370
1371 foo = Foo().save()
1372 bar = Bar(ref=foo, generic_ref=foo).save()
1373
1374 # Reference is no longer valid
1375 foo.delete()
1376 bar = Bar.objects.get()
1377
1378 with pytest.raises(DoesNotExist):
1379 bar.ref
1380
1381 with pytest.raises(DoesNotExist):
1382 bar.generic_ref
1383
1384 # When auto_dereference is disabled, there is no trouble returning DBRef
1385 bar = Bar.objects.get()
1386 expected = foo.to_dbref()
1387 bar._fields["ref"].set_auto_dereferencing(False)
1388 assert bar.ref == expected
1389 bar._fields["generic_ref"].set_auto_dereferencing(False)
1390 assert bar.generic_ref == {"_ref": expected, "_cls": "Foo"}
1391
1392 def test_list_item_dereference(self):
1393 """Ensure that DBRef items in ListFields are dereferenced."""

Callers

nothing calls this directly

Calls 8

drop_collectionMethod · 0.80
to_dbrefMethod · 0.80
FooClass · 0.70
BarClass · 0.70
saveMethod · 0.45
deleteMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected