From 1452d3fac5f2500cda0439a45294b9382c8c2d42 Mon Sep 17 00:00:00 2001 From: Ross Lawley Date: Mon, 11 Jul 2011 16:50:31 +0100 Subject: [PATCH] Fixed item_frequency methods to handle null values [fixes #216] --- mongoengine/queryset.py | 13 ++++++++----- tests/queryset.py | 22 ++++++++++++++++++++++ 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/mongoengine/queryset.py b/mongoengine/queryset.py index 6b110ff0..d533736b 100644 --- a/mongoengine/queryset.py +++ b/mongoengine/queryset.py @@ -1435,7 +1435,7 @@ class QuerySet(object): path = '{{~%(field)s}}'.split('.'); field = this; for (p in path) { field = field[path[p]]; } - if (field.constructor == Array) { + if (field && field.constructor == Array) { field.forEach(function(item) { emit(item, 1); }); @@ -1481,7 +1481,7 @@ class QuerySet(object): db[collection].find(query).forEach(function(doc) { field = doc; for (p in path) { field = field[path[p]]; } - if (field.constructor == Array) { + if (field && field.constructor == Array) { total += field.length; } else { total++; @@ -1497,7 +1497,7 @@ class QuerySet(object): db[collection].find(query).forEach(function(doc) { field = doc; for (p in path) { field = field[path[p]]; } - if (field.constructor == Array) { + if (field && field.constructor == Array) { field.forEach(function(item) { frequencies[item] = inc + (isNaN(frequencies[item]) ? 0: frequencies[item]); }); @@ -1509,8 +1509,11 @@ class QuerySet(object): return frequencies; } """ - - return self.exec_js(freq_func, field, normalize=normalize) + data = self.exec_js(freq_func, field, normalize=normalize) + if 'undefined' in data: + data[None] = data['undefined'] + del(data['undefined']) + return data def __repr__(self): limit = REPR_OUTPUT_SIZE + 1 diff --git a/tests/queryset.py b/tests/queryset.py index c0860b5c..e21db0fa 100644 --- a/tests/queryset.py +++ b/tests/queryset.py @@ -1781,6 +1781,28 @@ class QuerySetTest(unittest.TestCase): test_assertions(exec_js) test_assertions(map_reduce) + def test_item_frequencies_null_values(self): + + class Person(Document): + name = StringField() + city = StringField() + + Person.drop_collection() + + Person(name="Wilson Snr", city="CRB").save() + Person(name="Wilson Jr").save() + + freq = Person.objects.item_frequencies('city') + self.assertEquals(freq, {'CRB': 1.0, None: 1.0}) + freq = Person.objects.item_frequencies('city', normalize=True) + self.assertEquals(freq, {'CRB': 0.5, None: 0.5}) + + + freq = Person.objects.item_frequencies('city', map_reduce=True) + self.assertEquals(freq, {'CRB': 1.0, None: 1.0}) + freq = Person.objects.item_frequencies('city', normalize=True, map_reduce=True) + self.assertEquals(freq, {'CRB': 0.5, None: 0.5}) + def test_average(self): """Ensure that field can be averaged correctly. """