Skip to content

Store empty list instead of unset #2517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add other tests related with item_frequencies
  • Loading branch information
bagerard committed May 17, 2021
commit fa6cd9dc688d45e5d6e276058a2751d6907a5ead
32 changes: 32 additions & 0 deletions tests/queryset/test_field_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ class A(Document):
a = A.objects(id=app.id).only("my_list").get()
assert a.my_list == []

def test_item_frequencies_with_empty_list_edge_cases(self):
class TestDocument(Document):
fruit = ListField(StringField())

TestDocument.drop_collection()

doc1 = TestDocument(fruit=["a", "a", "b"]).save()
doc2 = TestDocument(fruit=["b", "c"]).save()

assert TestDocument.objects.item_frequencies("fruit") == {
"a": 2,
"b": 2,
"c": 1,
}

doc2.delete()
assert TestDocument.objects.item_frequencies("fruit") == {"a": 2, "b": 1}

doc1.fruit = []
doc1.save()
assert TestDocument.objects.item_frequencies("fruit") == {}

# delete the fruit field from db
# this creates weird item_frequencies result
# but somehow it is consistent
del doc1.fruit
doc1.save()
assert get_as_pymongo(doc1) == {
"_id": doc1.id,
}
assert TestDocument.objects.item_frequencies("fruit") == {None: 1}


class TestOnlyExcludeAll(unittest.TestCase):
def setUp(self):
Expand Down