| 828 | self.assertRaises(InvalidOperation, a.sort, "x", ASCENDING) |
| 829 | |
| 830 | async def test_where(self): |
| 831 | db = self.db |
| 832 | await db.test.drop() |
| 833 | |
| 834 | a = db.test.find() |
| 835 | with self.assertRaises(TypeError): |
| 836 | a.where(5) # type: ignore[arg-type] |
| 837 | with self.assertRaises(TypeError): |
| 838 | a.where(None) # type: ignore[arg-type] |
| 839 | with self.assertRaises(TypeError): |
| 840 | a.where({}) # type: ignore[arg-type] |
| 841 | |
| 842 | await db.test.insert_many([{"x": i} for i in range(10)]) |
| 843 | |
| 844 | self.assertEqual(3, len(await db.test.find().where("this.x < 3").to_list())) |
| 845 | self.assertEqual(3, len(await db.test.find().where(Code("this.x < 3")).to_list())) |
| 846 | |
| 847 | code_with_scope = Code("this.x < i", {"i": 3}) |
| 848 | if async_client_context.version.at_least(4, 3, 3): |
| 849 | # MongoDB 4.4 removed support for Code with scope. |
| 850 | with self.assertRaises(OperationFailure): |
| 851 | await db.test.find().where(code_with_scope).to_list() |
| 852 | |
| 853 | code_with_empty_scope = Code("this.x < 3", {}) |
| 854 | with self.assertRaises(OperationFailure): |
| 855 | await db.test.find().where(code_with_empty_scope).to_list() |
| 856 | else: |
| 857 | self.assertEqual(3, len(await db.test.find().where(code_with_scope).to_list())) |
| 858 | |
| 859 | self.assertEqual(10, len(await db.test.find().to_list())) |
| 860 | self.assertEqual([0, 1, 2], [a["x"] async for a in db.test.find().where("this.x < 3")]) |
| 861 | self.assertEqual([], [a["x"] async for a in db.test.find({"x": 5}).where("this.x < 3")]) |
| 862 | self.assertEqual([5], [a["x"] async for a in db.test.find({"x": 5}).where("this.x > 3")]) |
| 863 | |
| 864 | cursor = db.test.find().where("this.x < 3").where("this.x > 7") |
| 865 | self.assertEqual([8, 9], [a["x"] async for a in cursor]) |
| 866 | |
| 867 | a = db.test.find() |
| 868 | _ = a.where("this.x > 3") |
| 869 | async for _ in a: |
| 870 | break |
| 871 | self.assertRaises(InvalidOperation, a.where, "this.x < 3") |
| 872 | |
| 873 | async def test_rewind(self): |
| 874 | await self.db.test.insert_many([{"x": i} for i in range(1, 4)]) |