rewrite simple map/reduce test
This commit is contained in:
parent
f156da4ec2
commit
f4d0938e3d
@ -637,23 +637,20 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
def test_map_reduce(self):
|
def test_map_reduce(self):
|
||||||
"""Ensure map/reduce is both mapping and reducing.
|
"""Ensure map/reduce is both mapping and reducing.
|
||||||
"""
|
"""
|
||||||
class Song(Document):
|
class BlogPost(Document):
|
||||||
artists = ListField(StringField())
|
|
||||||
title = StringField()
|
title = StringField()
|
||||||
is_cover = BooleanField()
|
tags = ListField(StringField())
|
||||||
|
|
||||||
Song.drop_collection()
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
Song(title="Gloria", is_cover=True, artists=['Patti Smith']).save()
|
BlogPost(title="Post #1", tags=['music', 'film', 'print']).save()
|
||||||
Song(title="Redondo beach", is_cover=False,
|
BlogPost(title="Post #2", tags=['music', 'film']).save()
|
||||||
artists=['Patti Smith']).save()
|
BlogPost(title="Post #3", tags=['film', 'photography']).save()
|
||||||
Song(title="My Generation", is_cover=True,
|
|
||||||
artists=['Patti Smith', 'John Cale']).save()
|
|
||||||
|
|
||||||
map_f = """
|
map_f = """
|
||||||
function() {
|
function() {
|
||||||
this.artists.forEach(function(artist) {
|
this.tags.forEach(function(tag) {
|
||||||
emit(artist, 1);
|
emit(tag, 1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
@ -668,19 +665,18 @@ class QuerySetTest(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# ensure both artists are found
|
# run a map/reduce operation spanning all posts
|
||||||
results = Song.objects.map_reduce(map_f, reduce_f)
|
results = BlogPost.objects.map_reduce(map_f, reduce_f)
|
||||||
results = list(results)
|
results = list(results)
|
||||||
self.assertEqual(len(results), 2)
|
self.assertEqual(len(results), 4)
|
||||||
|
|
||||||
# query for a count of Songs per artist, ordered by -count.
|
music = filter(lambda r: r.key == "music", results)[0]
|
||||||
# Patti Smith has 3 song credits, and should therefore be first.
|
self.assertEqual(music.value, 2)
|
||||||
results = Song.objects.order_by("-value").map_reduce(map_f, reduce_f)
|
|
||||||
results = list(results)
|
|
||||||
self.assertEqual(results[0].key, "Patti Smith")
|
|
||||||
self.assertEqual(results[0].value, 3.0)
|
|
||||||
|
|
||||||
Song.drop_collection()
|
film = filter(lambda r: r.key == "film", results)[0]
|
||||||
|
self.assertEqual(film.value, 3)
|
||||||
|
|
||||||
|
BlogPost.drop_collection()
|
||||||
|
|
||||||
def test_map_reduce_finalize(self):
|
def test_map_reduce_finalize(self):
|
||||||
"""Ensure that map, reduce, and finalize run and introduce "scope"
|
"""Ensure that map, reduce, and finalize run and introduce "scope"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user