(self)
| 1572 | self.assertIsInstance(cursor, CommandCursor) |
| 1573 | |
| 1574 | def test_aggregation_cursor(self): |
| 1575 | db = self.db |
| 1576 | if client_context.has_secondaries: |
| 1577 | # Test that getMore messages are sent to the right server. |
| 1578 | db = self.client.get_database( |
| 1579 | db.name, |
| 1580 | read_preference=ReadPreference.SECONDARY, |
| 1581 | write_concern=WriteConcern(w=self.w), |
| 1582 | ) |
| 1583 | |
| 1584 | for collection_size in (10, 1000): |
| 1585 | db.drop_collection("test") |
| 1586 | db.test.insert_many([{"_id": i} for i in range(collection_size)]) |
| 1587 | expected_sum = sum(range(collection_size)) |
| 1588 | # Use batchSize to ensure multiple getMore messages |
| 1589 | cursor = db.test.aggregate([{"$project": {"_id": "$_id"}}], batchSize=5) |
| 1590 | |
| 1591 | self.assertEqual(expected_sum, sum(doc["_id"] for doc in cursor.to_list())) |
| 1592 | |
| 1593 | # Test that batchSize is handled properly. |
| 1594 | cursor = db.test.aggregate([], batchSize=5) |
| 1595 | self.assertEqual(5, len(cursor._data)) |
| 1596 | # Force a getMore |
| 1597 | cursor._data.clear() |
| 1598 | next(cursor) |
| 1599 | # batchSize - 1 |
| 1600 | self.assertEqual(4, len(cursor._data)) |
| 1601 | # Exhaust the cursor. There shouldn't be any errors. |
| 1602 | for _doc in cursor: |
| 1603 | pass |
| 1604 | |
| 1605 | def test_aggregation_cursor_alive(self): |
| 1606 | self.db.test.delete_many({}) |
nothing calls this directly
no test coverage detected