Skip to content

Commit 1e56b1f

Browse files
committed
Add test case for BaseList/EmbeddedDocListField weakref bugfix
1 parent a9a9271 commit 1e56b1f

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

tests/fields/test_embedded_document_field.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,35 @@ class MyFailingDoc(Document):
6363
class MyFailingdoc2(Document):
6464
emb = EmbeddedDocumentField("MyDoc")
6565

66-
def test_embedded_document_field_has_a_weakref__instance_reference(self):
67-
class Wallet(EmbeddedDocument):
68-
money = IntField()
69-
70-
class WalletOwner(Document):
71-
name = StringField()
72-
wallet = EmbeddedDocumentField(Wallet)
73-
74-
WalletOwner.drop_collection()
66+
def test_embedded_document_list_field__has__instance_weakref(self):
67+
class Comment(EmbeddedDocument):
68+
content = StringField()
7569

76-
wallet = Wallet(money=100)
77-
owner = WalletOwner(name="John", wallet=wallet)
78-
assert wallet._instance is owner
79-
assert isinstance(wallet._instance, weakref.ProxyTypes)
70+
class Post(Document):
71+
title = StringField()
72+
comment = EmbeddedDocumentField(Comment)
73+
comments = EmbeddedDocumentListField(Comment)
74+
comments2 = ListField(EmbeddedDocumentField(Comment))
75+
76+
Post.drop_collection()
77+
78+
for i in range(5):
79+
Post(
80+
title=f"{i}",
81+
comment=Comment(content=f"{i}"),
82+
comments=[Comment(content=f"{i}")],
83+
comments2=[Comment(content=f"{i}")],
84+
).save()
85+
86+
posts = list(Post.objects)
87+
for post in posts:
88+
assert isinstance(post.comments._instance, weakref.ProxyTypes)
89+
assert isinstance(post.comments2._instance, weakref.ProxyTypes)
90+
assert isinstance(post.comment._instance, weakref.ProxyTypes)
91+
for comment in post.comments:
92+
assert isinstance(comment._instance, weakref.ProxyTypes)
93+
for comment2 in post.comments2:
94+
assert isinstance(comment2._instance, weakref.ProxyTypes)
8095

8196
def test_embedded_document_field_validate_subclass(self):
8297
class BaseItem(EmbeddedDocument):

0 commit comments

Comments
 (0)