Merge pull request #287 from wpjunior/fixitem_frequencies
Fix item_frequencies
This commit is contained in:
commit
1af54f93f5
@ -1526,7 +1526,12 @@ class QuerySet(object):
|
|||||||
function() {
|
function() {
|
||||||
path = '{{~%(field)s}}'.split('.');
|
path = '{{~%(field)s}}'.split('.');
|
||||||
field = this;
|
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) {
|
if (field && field.constructor == Array) {
|
||||||
field.forEach(function(item) {
|
field.forEach(function(item) {
|
||||||
emit(item, 1);
|
emit(item, 1);
|
||||||
@ -1572,7 +1577,12 @@ class QuerySet(object):
|
|||||||
var total = 0.0;
|
var total = 0.0;
|
||||||
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) {
|
||||||
|
if (field)
|
||||||
|
field = field[path[p]];
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (field && field.constructor == Array) {
|
if (field && field.constructor == Array) {
|
||||||
total += field.length;
|
total += field.length;
|
||||||
} else {
|
} else {
|
||||||
@ -1588,7 +1598,12 @@ 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) {
|
||||||
|
if (field)
|
||||||
|
field = field[path[p]];
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
if (field && 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]);
|
||||||
|
@ -1840,6 +1840,36 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
freq = Person.objects.item_frequencies('city', normalize=True, map_reduce=True)
|
freq = Person.objects.item_frequencies('city', normalize=True, map_reduce=True)
|
||||||
self.assertEquals(freq, {'CRB': 0.5, None: 0.5})
|
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):
|
def test_average(self):
|
||||||
"""Ensure that field can be averaged correctly.
|
"""Ensure that field can be averaged correctly.
|
||||||
"""
|
"""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user