-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_test.py
44 lines (36 loc) · 1.45 KB
/
query_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import unittest
from pymongo import MongoClient
from pymongo import DESCENDING
from bson.code import Code
class QueryTest(unittest.TestCase):
def setUp(self):
self.mongo = MongoClient('localhost', 27017)
self.db = self.mongo.players
self.collection = self.db.players_collection
def test_goal_scorers(self):
# Map reduce to get a list of goal scorers
mapper = Code("""
function () {
emit(this.player_surname, {goals: this.goals});
}
""")
reducer = Code("""
function (key, values) {
var sum = 0;
values.forEach(function(doc) {
sum += doc.goals;
});
return {goals: sum};
}
""")
# Only include results where goals are greater than zero
self.collection.map_reduce(mapper, reducer, out="scorers",
query={"goals": {"$gt": 0}})
# Sort in descending order
result = list(self.db.scorers.find().sort(u'value', DESCENDING))
self.assertEqual(len(result), 261)
# Test top scorer
self.assertEqual(result[0][u'_id'], u'van Persie')
self.assertEqual(result[0][u'value'], {u'goals': 30.00})
if __name__ == '__main__':
unittest.main()