10
10
ValidationError ,
11
11
)
12
12
from mongoengine .base import _DocumentRegistry
13
- from tests .utils import MongoDBTestCase
13
+ from tests .utils import MongoDBTestCase , get_as_pymongo
14
14
15
15
16
16
class TestField (MongoDBTestCase ):
17
17
18
- def test_generic_reference (self ):
18
+ def test_generic_reference_field_basics (self ):
19
19
"""Ensure that a GenericReferenceField properly dereferences items."""
20
20
21
21
class Link (Document ):
@@ -42,14 +42,27 @@ class Bookmark(Document):
42
42
bm .save ()
43
43
44
44
bm = Bookmark .objects (bookmark_object = post_1 ).first ()
45
-
45
+ assert get_as_pymongo (bm ) == {
46
+ "_id" : bm .id ,
47
+ "bookmark_object" : {
48
+ "_cls" : "Post" ,
49
+ "_ref" : post_1 .to_dbref (),
50
+ },
51
+ }
46
52
assert bm .bookmark_object == post_1
47
53
assert isinstance (bm .bookmark_object , Post )
48
54
49
55
bm .bookmark_object = link_1
50
56
bm .save ()
51
57
52
58
bm = Bookmark .objects (bookmark_object = link_1 ).first ()
59
+ assert get_as_pymongo (bm ) == {
60
+ "_id" : bm .id ,
61
+ "bookmark_object" : {
62
+ "_cls" : "Link" ,
63
+ "_ref" : link_1 .to_dbref (),
64
+ },
65
+ }
53
66
54
67
assert bm .bookmark_object == link_1
55
68
assert isinstance (bm .bookmark_object , Link )
@@ -67,14 +80,6 @@ class OtherObj(Document):
67
80
s1 = SomeObj ().save ()
68
81
OtherObj (obj = s1 ).save ()
69
82
70
- assert OtherObj .objects .as_pymongo ().first () == {
71
- "_id" : OtherObj .objects .first ().id ,
72
- "obj" : {
73
- "_cls" : "SomeObj" ,
74
- "_ref" : s1 .to_dbref (),
75
- },
76
- }
77
-
78
83
# Query using to_dbref
79
84
assert OtherObj .objects (obj__in = [s1 .to_dbref ()]).count () == 1
80
85
0 commit comments