From 5756e8e203e22590c1f2e82e2c7b53a6f2d862a3 Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Fri, 10 Apr 2015 15:27:35 +0200 Subject: [PATCH 1/5] Added a test demonstrating the behavior in issue #938 --- tests/fields/fields.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 5e7f34b58..6161d7b92 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -3608,6 +3608,27 @@ def test_delete(self): # deleted from the database self.assertEqual(number, 2) + def test_remove_empty_collection_issue(self): + 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.assertIsNone(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 From 2f0b3719cb075ecdc8b9d3748cc8a8d05895bb64 Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Fri, 10 Apr 2015 15:33:10 +0200 Subject: [PATCH 2/5] Better name for test --- tests/fields/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 6161d7b92..2cc54b382 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -3608,7 +3608,7 @@ def test_delete(self): # deleted from the database self.assertEqual(number, 2) - def test_remove_empty_collection_issue(self): + def test_only_populates_missing_listfield(self): class A(Document): my_list = ListField(IntField()) From 4ae1d8f002b3ba666facd0f4f640a61234960d2f Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Fri, 10 Apr 2015 15:52:35 +0200 Subject: [PATCH 3/5] Added docstring --- tests/fields/fields.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 2cc54b382..5e4458c57 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -3609,6 +3609,11 @@ def test_delete(self): self.assertEqual(number, 2) def test_only_populates_missing_listfield(self): + """ + Tests that getting a query result filtered by only() doesn't populate + missing list fields with an empty list. + """ + class A(Document): my_list = ListField(IntField()) From 8a26e91aeb19445542d6e89916b95020e38e517c Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Fri, 10 Apr 2015 21:09:52 +0200 Subject: [PATCH 4/5] Python 2.6 doesn't have assertIsNone --- tests/fields/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index 5e4458c57..e103599a9 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -3627,7 +3627,7 @@ class A(Document): self.assertEqual(app2.my_list, []) app3 = A.objects(id=app.id).only('my_list').get() - self.assertIsNone(app3.my_list) + self.assertTrue(app3.my_list is None) # This creates a document with a my_list key mapped to an empty list. app4 = A(my_list=[]).save() From 7f90cf96a6505166b50d7e7fa4ebe7ed0c036f42 Mon Sep 17 00:00:00 2001 From: Eli Boyarski Date: Sat, 25 Apr 2015 12:49:45 +0300 Subject: [PATCH 5/5] Failing the test instead of demonstrating the current behavior --- tests/fields/fields.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/fields/fields.py b/tests/fields/fields.py index e103599a9..84993278b 100644 --- a/tests/fields/fields.py +++ b/tests/fields/fields.py @@ -3610,7 +3610,7 @@ def test_delete(self): def test_only_populates_missing_listfield(self): """ - Tests that getting a query result filtered by only() doesn't populate + Tests that getting a query result filtered by only() still populates missing list fields with an empty list. """ @@ -3627,7 +3627,7 @@ class A(Document): self.assertEqual(app2.my_list, []) app3 = A.objects(id=app.id).only('my_list').get() - self.assertTrue(app3.my_list is None) + self.assertEqual(app3.my_list, []) # This creates a document with a my_list key mapped to an empty list. app4 = A(my_list=[]).save()