def test_sort(self): p = Aggregation(match({"group.status":"active"}), project({"_id": 0, "group.name": 1}), sort("group.name")) #print(p.aggregation_string) cursor = self._collection.aggregate(p) groups = list(cursor) self.assertEqual(groups[-1], {'group': {'name': 'mugh - MongoDB User Group in Hamburg'}}) sorted = Aggregation(match({"group.status":"active"}), project({"_id": 0, "group.name": 1}), sort(("group.name", pymongo.DESCENDING))) unsorted = Aggregation(match({"group.status":"active"}), project({"_id": 0, "group.name": 1})) unsorted_list = list(self._collection.aggregate(unsorted)) sorted_list = list(self._collection.aggregate(sorted)) self.assertNotEqual(unsorted_list, sorted_list) unsorted_list.sort(reverse=True, key=lambda x: x["group"]["name"]) self.assertEqual(unsorted_list, sorted_list) self.assertEqual(sorted_list[-1], {'group': {'name': 'Atlanta MongoDB User Group'}}) self.assertEqual(116, len(groups))
def test_op(self): m = match({"a": "b"}) l = lookup({"c": "d"}) g = group({"one": "two"}) x = Aggregation(m, l, g ) self.assertEqual(list(x), [m, l, g]) self.assertEqual(str(x), str([m, l, g]))
def test_append(self): m = match({"a": "b"}) l = lookup({"c": "d"}) g = group({"one": "two"}) x = Aggregation(m, l) self.assertEqual(list(x), [m, l]) x.append(g) self.assertEqual(list(x), [m, l, g]) self.assertEqual(list(x), [m, l, g])
def testFormatter(self): self._agg.append(match({"member.name": "Joe Drumgoole0"})) # print( "agg: %s" % self._agg ) self._agg.append(project({"member.name": 1, "member.id": 1, "_id": 0, "member.ts": 1 })) filename = "JoeDrumgoole" ext = "json" self._formatter = CursorFormatter(self._col.aggregate(self._agg), filename=filename, formatter=ext) self._formatter.output(fieldNames=["member.name", "member.id", "member.ts"], datemap=["member.ts"]) self.assertTrue(os.path.isfile(filename)) os.unlink(filename)
def test_aggregation(self): y = Aggregation(match()) self.assertEqual(str([match()]), str(y))