Skip to content

Commit 295bd96

Browse files
committed
PYTHON-1237 - Update aggregate test for MongoDB 3.5+
1 parent 0308797 commit 295bd96

3 files changed

Lines changed: 36 additions & 25 deletions

File tree

test/test_collection.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,12 +1460,9 @@ def test_aggregate(self):
14601460

14611461
pipeline = {"$project": {"_id": False, "foo": True}}
14621462
for result in [
1463-
db.test.aggregate(pipeline),
1464-
db.test.aggregate([pipeline]),
1465-
db.test.aggregate((pipeline,))]:
1466-
1467-
self.assertEqual(1.0, result['ok'])
1468-
self.assertEqual([{'foo': [1, 2]}], result['result'])
1463+
(db.test.aggregate(pl, cursor={})).next()
1464+
for pl in (pipeline, [pipeline], (pipeline,))]:
1465+
self.assertEqual({'foo': [1, 2]}, result)
14691466

14701467
def test_aggregate_with_compile_re(self):
14711468
# See SERVER-6470.
@@ -1478,10 +1475,10 @@ def test_aggregate_with_compile_re(self):
14781475
db.test.drop()
14791476
db.test.insert({'r': re.compile('.*')})
14801477

1481-
result = db.test.aggregate([])
1482-
self.assertTrue(isinstance(result['result'][0]['r'], RE_TYPE))
1483-
result = db.test.aggregate([], compile_re=False)
1484-
self.assertTrue(isinstance(result['result'][0]['r'], Regex))
1478+
result = db.test.aggregate([], cursor={}).next()
1479+
self.assertTrue(isinstance(result['r'], RE_TYPE))
1480+
result = db.test.aggregate([], cursor={}, compile_re=False).next()
1481+
self.assertTrue(isinstance(result['r'], Regex))
14851482

14861483
def test_aggregation_cursor_validation(self):
14871484
# Regardless of version, should return CommandCursor when given
@@ -1491,10 +1488,11 @@ def test_aggregation_cursor_validation(self):
14911488
cursor = db.test.aggregate(projection, cursor={})
14921489
self.assertTrue(isinstance(cursor, CommandCursor))
14931490

1494-
db = self.db
1495-
projection = {'$project': {'_id': '$_id'}}
1496-
cursor = db.test.aggregate(projection)
1497-
self.assertFalse(isinstance(cursor, CommandCursor))
1491+
if not version.at_least(self.db.connection, (3, 5, 1)):
1492+
db = self.db
1493+
projection = {'$project': {'_id': '$_id'}}
1494+
cursor = db.test.aggregate(projection)
1495+
self.assertFalse(isinstance(cursor, CommandCursor))
14981496

14991497
def test_aggregation_cursor(self):
15001498
db = self.db

test/test_database.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,8 @@ def test_command_with_compile_re(self):
345345
raise SkipTest(
346346
"Retrieving a regex with aggregation requires "
347347
"MongoDB >= 2.3.2")
348+
if version.at_least(self.client, (3, 5, 1)):
349+
raise SkipTest("SERVER-24623")
348350

349351
db = self.client.pymongo_test
350352
db.test.drop()
@@ -736,12 +738,18 @@ def test_command_max_time_ms(self):
736738
'count', 'test', maxTimeMS=1)
737739
pipeline = [{'$project': {'name': 1, 'count': 1}}]
738740
# Database command helper.
739-
db.command('aggregate', 'test', pipeline=pipeline)
740-
self.assertRaises(ExecutionTimeout, db.command,
741-
'aggregate', 'test',
742-
pipeline=pipeline, maxTimeMS=1)
741+
if not version.at_least(self.client, (3, 5, 1)):
742+
db.command('aggregate', 'test', pipeline=pipeline)
743+
self.assertRaises(ExecutionTimeout, db.command,
744+
'aggregate', 'test',
745+
pipeline=pipeline, maxTimeMS=1)
746+
else:
747+
db.command('aggregate', 'test', pipeline=pipeline, cursor={})
748+
self.assertRaises(ExecutionTimeout, db.command,
749+
'aggregate', 'test',
750+
pipeline=pipeline, cursor={}, maxTimeMS=1)
743751
# Collection helper.
744-
db.test.aggregate(pipeline=pipeline)
752+
db.test.aggregate(pipeline=pipeline, cursor={})
745753
self.assertRaises(ExecutionTimeout,
746754
db.test.aggregate, pipeline, maxTimeMS=1)
747755
finally:

test/test_read_preferences.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ def test_command(self):
523523
('geoSearch', 'test'), ('near', [33, 33]), ('maxDistance', 6),
524524
('search', {'type': 'restaurant'}), ('limit', 30)])))
525525

526-
if version.at_least(self.c, (2, 1, 0)):
526+
if (version.at_least(self.c, (2, 1, 0)) and
527+
not version.at_least(self.c, (3, 5, 1))):
527528
self._test_fn(
528529
True, lambda: self.c.pymongo_test.command(SON([
529530
('aggregate', 'test'),
@@ -568,8 +569,8 @@ def test_map_reduce_command(self):
568569
])))
569570

570571
def test_aggregate_command_with_out(self):
571-
if not version.at_least(self.c, (2, 5, 2)):
572-
raise SkipTest("Aggregation with $out requires MongoDB >= 2.5.2")
572+
if not version.at_least(self.c, (2, 6, 0)):
573+
raise SkipTest("Aggregation with $out requires MongoDB >= 2.6.0")
573574

574575
# Tests aggregate command when pipeline contains $out.
575576
self.c.pymongo_test.test.insert({"x": 1, "y": 1}, w=self.w)
@@ -584,12 +585,14 @@ def test_aggregate_command_with_out(self):
584585
warnings.simplefilter("ignore", UserWarning)
585586
self._test_fn(False, lambda: self.c.pymongo_test.command(
586587
"aggregate", "test",
587-
pipeline=[{"$match": {"x": 1}}, {"$out": "agg_out"}]
588+
pipeline=[{"$match": {"x": 1}}, {"$out": "agg_out"}],
589+
cursor={}
588590
))
589591

590592
# Test aggregate when sent through the collection aggregate function.
591593
self._test_fn(False, lambda: self.c.pymongo_test.test.aggregate(
592-
[{"$match": {"x": 2}}, {"$out": "agg_out"}]
594+
[{"$match": {"x": 2}}, {"$out": "agg_out"}],
595+
cursor={}
593596
))
594597
finally:
595598
ctx.exit()
@@ -662,7 +665,9 @@ def test_distinct(self):
662665

663666
def test_aggregate(self):
664667
if version.at_least(self.c, (2, 1, 0)):
665-
self._test_fn(True, lambda: self.c.pymongo_test.test.aggregate([]))
668+
self._test_fn(
669+
True,
670+
lambda: self.c.pymongo_test.test.aggregate([], cursor={}))
666671

667672

668673
class TestMovingAverage(unittest.TestCase):

0 commit comments

Comments
 (0)