Skip to content

Commit

Permalink
rewrite simple map/reduce test
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdennewitz committed Mar 17, 2010
1 parent f156da4 commit f4d0938
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions tests/queryset.py
Expand Up @@ -637,23 +637,20 @@ def test_order_by(self):
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);
}); });
} }
""" """
Expand All @@ -668,19 +665,18 @@ class Song(Document):
} }
""" """


# 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) film = filter(lambda r: r.key == "film", results)[0]
self.assertEqual(results[0].key, "Patti Smith") self.assertEqual(film.value, 3)
self.assertEqual(results[0].value, 3.0)


Song.drop_collection() 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"
Expand Down

0 comments on commit f4d0938

Please sign in to comment.