fixed embedded null item_frequencies
This commit is contained in:
parent
f7fbb3d2f6
commit
a9cacd2e06
@ -1526,7 +1526,12 @@ class QuerySet(object):
|
||||
function() {
|
||||
path = '{{~%(field)s}}'.split('.');
|
||||
field = this;
|
||||
for (p in path) { field = field[path[p]]; }
|
||||
for (p in path) {
|
||||
if (field)
|
||||
field = field[path[p]];
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (field && field.constructor == Array) {
|
||||
field.forEach(function(item) {
|
||||
emit(item, 1);
|
||||
@ -1572,7 +1577,12 @@ class QuerySet(object):
|
||||
var total = 0.0;
|
||||
db[collection].find(query).forEach(function(doc) {
|
||||
field = doc;
|
||||
for (p in path) { field = field[path[p]]; }
|
||||
for (p in path) {
|
||||
if (field)
|
||||
field = field[path[p]];
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (field && field.constructor == Array) {
|
||||
total += field.length;
|
||||
} else {
|
||||
@ -1588,7 +1598,12 @@ class QuerySet(object):
|
||||
}
|
||||
db[collection].find(query).forEach(function(doc) {
|
||||
field = doc;
|
||||
for (p in path) { field = field[path[p]]; }
|
||||
for (p in path) {
|
||||
if (field)
|
||||
field = field[path[p]];
|
||||
else
|
||||
break;
|
||||
}
|
||||
if (field && field.constructor == Array) {
|
||||
field.forEach(function(item) {
|
||||
frequencies[item] = inc + (isNaN(frequencies[item]) ? 0: frequencies[item]);
|
||||
|
@ -1840,6 +1840,36 @@ class QuerySetTest(unittest.TestCase):
|
||||
freq = Person.objects.item_frequencies('city', normalize=True, map_reduce=True)
|
||||
self.assertEquals(freq, {'CRB': 0.5, None: 0.5})
|
||||
|
||||
|
||||
def test_item_frequencies_with_null_embedded(self):
|
||||
class Data(EmbeddedDocument):
|
||||
name = StringField()
|
||||
|
||||
class Extra(EmbeddedDocument):
|
||||
tag = StringField()
|
||||
|
||||
class Person(Document):
|
||||
data = EmbeddedDocumentField(Data, required=True)
|
||||
extra = EmbeddedDocumentField(Extra)
|
||||
|
||||
|
||||
Person.drop_collection()
|
||||
|
||||
p = Person()
|
||||
p.data = Data(name="Wilson Jr")
|
||||
p.save()
|
||||
|
||||
p = Person()
|
||||
p.data = Data(name="Wesley")
|
||||
p.extra = Extra(tag="friend")
|
||||
p.save()
|
||||
|
||||
ot = Person.objects.item_frequencies('extra.tag', map_reduce=False)
|
||||
self.assertEquals(ot, {None: 1.0, u'friend': 1.0})
|
||||
|
||||
ot = Person.objects.item_frequencies('extra.tag', map_reduce=True)
|
||||
self.assertEquals(ot, {None: 1.0, u'friend': 1.0})
|
||||
|
||||
def test_average(self):
|
||||
"""Ensure that field can be averaged correctly.
|
||||
"""
|
||||
|
Loading…
x
Reference in New Issue
Block a user