Fixed item_frequency methods to handle null values

[fixes #216]
This commit is contained in:
Ross Lawley 2011-07-11 16:50:31 +01:00
parent 031c507fde
commit 1452d3fac5
2 changed files with 30 additions and 5 deletions

View File

@ -1435,7 +1435,7 @@ class QuerySet(object):
path = '{{~%(field)s}}'.split('.'); path = '{{~%(field)s}}'.split('.');
field = this; field = this;
for (p in path) { field = field[path[p]]; } for (p in path) { field = field[path[p]]; }
if (field.constructor == Array) { if (field && field.constructor == Array) {
field.forEach(function(item) { field.forEach(function(item) {
emit(item, 1); emit(item, 1);
}); });
@ -1481,7 +1481,7 @@ class QuerySet(object):
db[collection].find(query).forEach(function(doc) { db[collection].find(query).forEach(function(doc) {
field = doc; field = doc;
for (p in path) { field = field[path[p]]; } for (p in path) { field = field[path[p]]; }
if (field.constructor == Array) { if (field && field.constructor == Array) {
total += field.length; total += field.length;
} else { } else {
total++; total++;
@ -1497,7 +1497,7 @@ class QuerySet(object):
db[collection].find(query).forEach(function(doc) { db[collection].find(query).forEach(function(doc) {
field = doc; field = doc;
for (p in path) { field = field[path[p]]; } for (p in path) { field = field[path[p]]; }
if (field.constructor == Array) { if (field && field.constructor == Array) {
field.forEach(function(item) { field.forEach(function(item) {
frequencies[item] = inc + (isNaN(frequencies[item]) ? 0: frequencies[item]); frequencies[item] = inc + (isNaN(frequencies[item]) ? 0: frequencies[item]);
}); });
@ -1509,8 +1509,11 @@ class QuerySet(object):
return frequencies; return frequencies;
} }
""" """
data = self.exec_js(freq_func, field, normalize=normalize)
return self.exec_js(freq_func, field, normalize=normalize) if 'undefined' in data:
data[None] = data['undefined']
del(data['undefined'])
return data
def __repr__(self): def __repr__(self):
limit = REPR_OUTPUT_SIZE + 1 limit = REPR_OUTPUT_SIZE + 1

View File

@ -1781,6 +1781,28 @@ class QuerySetTest(unittest.TestCase):
test_assertions(exec_js) test_assertions(exec_js)
test_assertions(map_reduce) 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): def test_average(self):
"""Ensure that field can be averaged correctly. """Ensure that field can be averaged correctly.
""" """