Ejemplo n.º 1
0
 def test_map_reduce(self):
     store_event("withdraw", {'user': '******', 'amount': 10})
     store_event("withdraw", {'user': '******', 'amount': 5})
     store_event("deposit", {'user': '******', 'amount': 20})
     mapper_code = Code("""
            function () {
                 signed = this.payload.amount;
                 if(this.event == 'withdraw'){
                     signed = signed * -1;
                 }
                 emit(this.payload.user,signed);
            }
      """)
     reduce_code = Code("""
             function (key, values) {
               var balance = 0;
               for (var i = 0; i < values.length; i++) {
                 balance += values[i];
               }
               return balance;
             }
      """)
     result = self.collection.map_reduce(mapper_code, reduce_code, "balance_results")
     for k in result.find():
         print k
Ejemplo n.º 2
0
 def test_store_event(self):
     store_event("test", {'test': 'value'})
     self.assertTrue(self.collection.find({u'event': u'test'}))
     self.assertEquals(1, self.collection.find({'event': 'test'}).count())