diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 5e7f34b58..84993278b 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -3608,6 +3608,32 @@ def test_delete(self): # deleted from the database self.assertEqual(number, 2) + def test_only_populates_missing_listfield(self): + """ + Tests that getting a query result filtered by only() still populates + missing list fields with an empty list. + """ + + class A(Document): + my_list = ListField(IntField()) + + app = A(my_list=[3]).save() + app.my_list = [] + app.save() + # This creates a document without a my_list key in the a collection. + + + app2 = A.objects(id=app.id).get() + self.assertEqual(app2.my_list, []) + + app3 = A.objects(id=app.id).only('my_list').get() + self.assertEqual(app3.my_list, []) + + # This creates a document with a my_list key mapped to an empty list. + app4 = A(my_list=[]).save() + app5 = A.objects(id=app4.id).only('my_list').get() + self.assertEqual(app5.my_list, []) + def test_filtered_delete(self): """ Tests the delete method of a List of Embedded Documents