Skip to content

Commit 19fdf7c

Browse files
authored
PYTHON-5093 - Convert test.test_read_concern to async (#2109)
1 parent c8d3afd commit 19fdf7c

File tree

3 files changed

+130
-5
lines changed

3 files changed

+130
-5
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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()

test/test_read_concern.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from pymongo.errors import OperationFailure
2828
from pymongo.read_concern import ReadConcern
2929

30+
_IS_SYNC = True
31+
3032

3133
class TestReadConcern(IntegrationTest):
3234
listener: OvertCommandListener
@@ -71,14 +73,14 @@ def test_invalid_read_concern(self):
7173
def test_find_command(self):
7274
# readConcern not sent in command if not specified.
7375
coll = self.db.coll
74-
tuple(coll.find({"field": "value"}))
76+
coll.find({"field": "value"}).to_list()
7577
self.assertNotIn("readConcern", self.listener.started_events[0].command)
7678

7779
self.listener.reset()
7880

7981
# Explicitly set readConcern to 'local'.
8082
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
81-
tuple(coll.find({"field": "value"}))
83+
coll.find({"field": "value"}).to_list()
8284
self.assertEqualCommand(
8385
SON(
8486
[
@@ -93,19 +95,19 @@ def test_find_command(self):
9395
def test_command_cursor(self):
9496
# readConcern not sent in command if not specified.
9597
coll = self.db.coll
96-
tuple(coll.aggregate([{"$match": {"field": "value"}}]))
98+
(coll.aggregate([{"$match": {"field": "value"}}])).to_list()
9799
self.assertNotIn("readConcern", self.listener.started_events[0].command)
98100

99101
self.listener.reset()
100102

101103
# Explicitly set readConcern to 'local'.
102104
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
103-
tuple(coll.aggregate([{"$match": {"field": "value"}}]))
105+
(coll.aggregate([{"$match": {"field": "value"}}])).to_list()
104106
self.assertEqual({"level": "local"}, self.listener.started_events[0].command["readConcern"])
105107

106108
def test_aggregate_out(self):
107109
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
108-
tuple(coll.aggregate([{"$match": {"field": "value"}}, {"$out": "output_collection"}]))
110+
(coll.aggregate([{"$match": {"field": "value"}}, {"$out": "output_collection"}])).to_list()
109111

110112
# Aggregate with $out supports readConcern MongoDB 4.2 onwards.
111113
if client_context.version >= (4, 1):

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ def async_only_test(f: str) -> bool:
216216
"test_logger.py",
217217
"test_monitoring.py",
218218
"test_raw_bson.py",
219+
"test_read_concern.py",
219220
"test_retryable_reads.py",
220221
"test_retryable_writes.py",
221222
"test_session.py",

0 commit comments

Comments
 (0)