|
| 1 | +# Copyright 2015-present MongoDB, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""Test the read_concern module.""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import sys |
| 19 | +import unittest |
| 20 | + |
| 21 | +sys.path[0:0] = [""] |
| 22 | + |
| 23 | +from test.asynchronous import AsyncIntegrationTest, async_client_context |
| 24 | +from test.utils import OvertCommandListener |
| 25 | + |
| 26 | +from bson.son import SON |
| 27 | +from pymongo.errors import OperationFailure |
| 28 | +from pymongo.read_concern import ReadConcern |
| 29 | + |
| 30 | +_IS_SYNC = False |
| 31 | + |
| 32 | + |
| 33 | +class TestReadConcern(AsyncIntegrationTest): |
| 34 | + listener: OvertCommandListener |
| 35 | + |
| 36 | + @async_client_context.require_connection |
| 37 | + async def asyncSetUp(self): |
| 38 | + await super().asyncSetUp() |
| 39 | + self.listener = OvertCommandListener() |
| 40 | + self.client = await self.async_rs_or_single_client(event_listeners=[self.listener]) |
| 41 | + self.db = self.client.pymongo_test |
| 42 | + await async_client_context.client.pymongo_test.create_collection("coll") |
| 43 | + |
| 44 | + async def asyncTearDown(self): |
| 45 | + await async_client_context.client.pymongo_test.drop_collection("coll") |
| 46 | + |
| 47 | + def test_read_concern(self): |
| 48 | + rc = ReadConcern() |
| 49 | + self.assertIsNone(rc.level) |
| 50 | + self.assertTrue(rc.ok_for_legacy) |
| 51 | + |
| 52 | + rc = ReadConcern("majority") |
| 53 | + self.assertEqual("majority", rc.level) |
| 54 | + self.assertFalse(rc.ok_for_legacy) |
| 55 | + |
| 56 | + rc = ReadConcern("local") |
| 57 | + self.assertEqual("local", rc.level) |
| 58 | + self.assertTrue(rc.ok_for_legacy) |
| 59 | + |
| 60 | + self.assertRaises(TypeError, ReadConcern, 42) |
| 61 | + |
| 62 | + async def test_read_concern_uri(self): |
| 63 | + uri = f"mongodb://{await async_client_context.pair}/?readConcernLevel=majority" |
| 64 | + client = await self.async_rs_or_single_client(uri, connect=False) |
| 65 | + self.assertEqual(ReadConcern("majority"), client.read_concern) |
| 66 | + |
| 67 | + async def test_invalid_read_concern(self): |
| 68 | + coll = self.db.get_collection("coll", read_concern=ReadConcern("unknown")) |
| 69 | + # We rely on the server to validate read concern. |
| 70 | + with self.assertRaises(OperationFailure): |
| 71 | + await coll.find_one() |
| 72 | + |
| 73 | + async def test_find_command(self): |
| 74 | + # readConcern not sent in command if not specified. |
| 75 | + coll = self.db.coll |
| 76 | + await coll.find({"field": "value"}).to_list() |
| 77 | + self.assertNotIn("readConcern", self.listener.started_events[0].command) |
| 78 | + |
| 79 | + self.listener.reset() |
| 80 | + |
| 81 | + # Explicitly set readConcern to 'local'. |
| 82 | + coll = self.db.get_collection("coll", read_concern=ReadConcern("local")) |
| 83 | + await coll.find({"field": "value"}).to_list() |
| 84 | + self.assertEqualCommand( |
| 85 | + SON( |
| 86 | + [ |
| 87 | + ("find", "coll"), |
| 88 | + ("filter", {"field": "value"}), |
| 89 | + ("readConcern", {"level": "local"}), |
| 90 | + ] |
| 91 | + ), |
| 92 | + self.listener.started_events[0].command, |
| 93 | + ) |
| 94 | + |
| 95 | + async def test_command_cursor(self): |
| 96 | + # readConcern not sent in command if not specified. |
| 97 | + coll = self.db.coll |
| 98 | + await (await coll.aggregate([{"$match": {"field": "value"}}])).to_list() |
| 99 | + self.assertNotIn("readConcern", self.listener.started_events[0].command) |
| 100 | + |
| 101 | + self.listener.reset() |
| 102 | + |
| 103 | + # Explicitly set readConcern to 'local'. |
| 104 | + coll = self.db.get_collection("coll", read_concern=ReadConcern("local")) |
| 105 | + await (await coll.aggregate([{"$match": {"field": "value"}}])).to_list() |
| 106 | + self.assertEqual({"level": "local"}, self.listener.started_events[0].command["readConcern"]) |
| 107 | + |
| 108 | + async def test_aggregate_out(self): |
| 109 | + coll = self.db.get_collection("coll", read_concern=ReadConcern("local")) |
| 110 | + await ( |
| 111 | + await coll.aggregate([{"$match": {"field": "value"}}, {"$out": "output_collection"}]) |
| 112 | + ).to_list() |
| 113 | + |
| 114 | + # Aggregate with $out supports readConcern MongoDB 4.2 onwards. |
| 115 | + if async_client_context.version >= (4, 1): |
| 116 | + self.assertIn("readConcern", self.listener.started_events[0].command) |
| 117 | + else: |
| 118 | + self.assertNotIn("readConcern", self.listener.started_events[0].command) |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + unittest.main() |
0 commit comments