Skip to content

Commit 33910a0

Browse files
committed
PYTHON-4839 - Convert test.test_collation to async
1 parent 2895e84 commit 33910a0

File tree

3 files changed

+296
-4
lines changed

3 files changed

+296
-4
lines changed

test/asynchronous/test_collation.py

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
# Copyright 2016-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 collation module."""
16+
from __future__ import annotations
17+
18+
import functools
19+
import warnings
20+
from test.asynchronous import AsyncIntegrationTest, async_client_context, unittest
21+
from test.utils import EventListener
22+
from typing import Any
23+
24+
from pymongo.collation import (
25+
Collation,
26+
CollationAlternate,
27+
CollationCaseFirst,
28+
CollationMaxVariable,
29+
CollationStrength,
30+
)
31+
from pymongo.errors import ConfigurationError
32+
from pymongo.operations import (
33+
DeleteMany,
34+
DeleteOne,
35+
IndexModel,
36+
ReplaceOne,
37+
UpdateMany,
38+
UpdateOne,
39+
)
40+
from pymongo.write_concern import WriteConcern
41+
42+
_IS_SYNC = False
43+
44+
45+
class TestCollationObject(unittest.TestCase):
46+
def test_constructor(self):
47+
self.assertRaises(TypeError, Collation, locale=42)
48+
# Fill in a locale to test the other options.
49+
_Collation = functools.partial(Collation, "en_US")
50+
# No error.
51+
_Collation(caseFirst=CollationCaseFirst.UPPER)
52+
self.assertRaises(TypeError, _Collation, caseLevel="true")
53+
self.assertRaises(ValueError, _Collation, strength="six")
54+
self.assertRaises(TypeError, _Collation, numericOrdering="true")
55+
self.assertRaises(TypeError, _Collation, alternate=5)
56+
self.assertRaises(TypeError, _Collation, maxVariable=2)
57+
self.assertRaises(TypeError, _Collation, normalization="false")
58+
self.assertRaises(TypeError, _Collation, backwards="true")
59+
60+
# No errors.
61+
Collation("en_US", future_option="bar", another_option=42)
62+
collation = Collation(
63+
"en_US",
64+
caseLevel=True,
65+
caseFirst=CollationCaseFirst.UPPER,
66+
strength=CollationStrength.QUATERNARY,
67+
numericOrdering=True,
68+
alternate=CollationAlternate.SHIFTED,
69+
maxVariable=CollationMaxVariable.SPACE,
70+
normalization=True,
71+
backwards=True,
72+
)
73+
74+
self.assertEqual(
75+
{
76+
"locale": "en_US",
77+
"caseLevel": True,
78+
"caseFirst": "upper",
79+
"strength": 4,
80+
"numericOrdering": True,
81+
"alternate": "shifted",
82+
"maxVariable": "space",
83+
"normalization": True,
84+
"backwards": True,
85+
},
86+
collation.document,
87+
)
88+
89+
self.assertEqual(
90+
{"locale": "en_US", "backwards": True}, Collation("en_US", backwards=True).document
91+
)
92+
93+
94+
class TestCollation(AsyncIntegrationTest):
95+
listener: EventListener
96+
warn_context: Any
97+
collation: Collation
98+
99+
@classmethod
100+
@async_client_context.require_connection
101+
async def _setup_class(cls):
102+
await super()._setup_class()
103+
cls.listener = EventListener()
104+
cls.client = await cls.unmanaged_async_rs_or_single_client(event_listeners=[cls.listener])
105+
cls.db = cls.client.pymongo_test
106+
cls.collation = Collation("en_US")
107+
cls.warn_context = warnings.catch_warnings()
108+
cls.warn_context.__enter__()
109+
warnings.simplefilter("ignore", DeprecationWarning)
110+
111+
@classmethod
112+
async def _tearDown_class(cls):
113+
cls.warn_context.__exit__()
114+
cls.warn_context = None
115+
await cls.client.close()
116+
await super()._tearDown_class()
117+
118+
def tearDown(self):
119+
self.listener.reset()
120+
super().tearDown()
121+
122+
def last_command_started(self):
123+
return self.listener.started_events[-1].command
124+
125+
def assertCollationInLastCommand(self):
126+
self.assertEqual(self.collation.document, self.last_command_started()["collation"])
127+
128+
async def test_create_collection(self):
129+
await self.db.test.drop()
130+
await self.db.create_collection("test", collation=self.collation)
131+
self.assertCollationInLastCommand()
132+
133+
# Test passing collation as a dict as well.
134+
await self.db.test.drop()
135+
self.listener.reset()
136+
await self.db.create_collection("test", collation=self.collation.document)
137+
self.assertCollationInLastCommand()
138+
139+
def test_index_model(self):
140+
model = IndexModel([("a", 1), ("b", -1)], collation=self.collation)
141+
self.assertEqual(self.collation.document, model.document["collation"])
142+
143+
async def test_create_index(self):
144+
await self.db.test.create_index("foo", collation=self.collation)
145+
ci_cmd = self.listener.started_events[0].command
146+
self.assertEqual(self.collation.document, ci_cmd["indexes"][0]["collation"])
147+
148+
async def test_aggregate(self):
149+
await self.db.test.aggregate([{"$group": {"_id": 42}}], collation=self.collation)
150+
self.assertCollationInLastCommand()
151+
152+
async def test_count_documents(self):
153+
await self.db.test.count_documents({}, collation=self.collation)
154+
self.assertCollationInLastCommand()
155+
156+
async def test_distinct(self):
157+
await self.db.test.distinct("foo", collation=self.collation)
158+
self.assertCollationInLastCommand()
159+
160+
self.listener.reset()
161+
await self.db.test.find(collation=self.collation).distinct("foo")
162+
self.assertCollationInLastCommand()
163+
164+
async def test_find_command(self):
165+
await self.db.test.insert_one({"is this thing on?": True})
166+
self.listener.reset()
167+
await anext(self.db.test.find(collation=self.collation))
168+
self.assertCollationInLastCommand()
169+
170+
async def test_explain_command(self):
171+
self.listener.reset()
172+
await self.db.test.find(collation=self.collation).explain()
173+
# The collation should be part of the explained command.
174+
self.assertEqual(
175+
self.collation.document, self.last_command_started()["explain"]["collation"]
176+
)
177+
178+
async def test_delete(self):
179+
await self.db.test.delete_one({"foo": 42}, collation=self.collation)
180+
command = self.listener.started_events[0].command
181+
self.assertEqual(self.collation.document, command["deletes"][0]["collation"])
182+
183+
self.listener.reset()
184+
await self.db.test.delete_many({"foo": 42}, collation=self.collation)
185+
command = self.listener.started_events[0].command
186+
self.assertEqual(self.collation.document, command["deletes"][0]["collation"])
187+
188+
async def test_update(self):
189+
await self.db.test.replace_one({"foo": 42}, {"foo": 43}, collation=self.collation)
190+
command = self.listener.started_events[0].command
191+
self.assertEqual(self.collation.document, command["updates"][0]["collation"])
192+
193+
self.listener.reset()
194+
await self.db.test.update_one({"foo": 42}, {"$set": {"foo": 43}}, collation=self.collation)
195+
command = self.listener.started_events[0].command
196+
self.assertEqual(self.collation.document, command["updates"][0]["collation"])
197+
198+
self.listener.reset()
199+
await self.db.test.update_many({"foo": 42}, {"$set": {"foo": 43}}, collation=self.collation)
200+
command = self.listener.started_events[0].command
201+
self.assertEqual(self.collation.document, command["updates"][0]["collation"])
202+
203+
async def test_find_and(self):
204+
await self.db.test.find_one_and_delete({"foo": 42}, collation=self.collation)
205+
self.assertCollationInLastCommand()
206+
207+
self.listener.reset()
208+
await self.db.test.find_one_and_update(
209+
{"foo": 42}, {"$set": {"foo": 43}}, collation=self.collation
210+
)
211+
self.assertCollationInLastCommand()
212+
213+
self.listener.reset()
214+
await self.db.test.find_one_and_replace({"foo": 42}, {"foo": 43}, collation=self.collation)
215+
self.assertCollationInLastCommand()
216+
217+
async def test_bulk_write(self):
218+
await self.db.test.collection.bulk_write(
219+
[
220+
DeleteOne({"noCollation": 42}),
221+
DeleteMany({"noCollation": 42}),
222+
DeleteOne({"foo": 42}, collation=self.collation),
223+
DeleteMany({"foo": 42}, collation=self.collation),
224+
ReplaceOne({"noCollation": 24}, {"bar": 42}),
225+
UpdateOne({"noCollation": 84}, {"$set": {"bar": 10}}, upsert=True),
226+
UpdateMany({"noCollation": 45}, {"$set": {"bar": 42}}),
227+
ReplaceOne({"foo": 24}, {"foo": 42}, collation=self.collation),
228+
UpdateOne(
229+
{"foo": 84}, {"$set": {"foo": 10}}, upsert=True, collation=self.collation
230+
),
231+
UpdateMany({"foo": 45}, {"$set": {"foo": 42}}, collation=self.collation),
232+
]
233+
)
234+
235+
delete_cmd = self.listener.started_events[0].command
236+
update_cmd = self.listener.started_events[1].command
237+
238+
def check_ops(ops):
239+
for op in ops:
240+
if "noCollation" in op["q"]:
241+
self.assertNotIn("collation", op)
242+
else:
243+
self.assertEqual(self.collation.document, op["collation"])
244+
245+
check_ops(delete_cmd["deletes"])
246+
check_ops(update_cmd["updates"])
247+
248+
async def test_indexes_same_keys_different_collations(self):
249+
await self.db.test.drop()
250+
usa_collation = Collation("en_US")
251+
ja_collation = Collation("ja")
252+
await self.db.test.create_indexes(
253+
[
254+
IndexModel("fieldname", collation=usa_collation),
255+
IndexModel("fieldname", name="japanese_version", collation=ja_collation),
256+
IndexModel("fieldname", name="simple"),
257+
]
258+
)
259+
indexes = await self.db.test.index_information()
260+
self.assertEqual(
261+
usa_collation.document["locale"], indexes["fieldname_1"]["collation"]["locale"]
262+
)
263+
self.assertEqual(
264+
ja_collation.document["locale"], indexes["japanese_version"]["collation"]["locale"]
265+
)
266+
self.assertNotIn("collation", indexes["simple"])
267+
await self.db.test.drop_index("fieldname_1")
268+
indexes = await self.db.test.index_information()
269+
self.assertIn("japanese_version", indexes)
270+
self.assertIn("simple", indexes)
271+
self.assertNotIn("fieldname", indexes)
272+
273+
async def test_unacknowledged_write(self):
274+
unacknowledged = WriteConcern(w=0)
275+
collection = self.db.get_collection("test", write_concern=unacknowledged)
276+
with self.assertRaises(ConfigurationError):
277+
await collection.update_one(
278+
{"hello": "world"}, {"$set": {"hello": "moon"}}, collation=self.collation
279+
)
280+
update_one = UpdateOne(
281+
{"hello": "world"}, {"$set": {"hello": "moon"}}, collation=self.collation
282+
)
283+
with self.assertRaises(ConfigurationError):
284+
await collection.bulk_write([update_one])
285+
286+
async def test_cursor_collation(self):
287+
await self.db.test.insert_one({"hello": "world"})
288+
await anext(self.db.test.find().collation(self.collation))
289+
self.assertCollationInLastCommand()

test/test_collation.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
)
4040
from pymongo.write_concern import WriteConcern
4141

42+
_IS_SYNC = True
43+
4244

4345
class TestCollationObject(unittest.TestCase):
4446
def test_constructor(self):
@@ -96,8 +98,8 @@ class TestCollation(IntegrationTest):
9698

9799
@classmethod
98100
@client_context.require_connection
99-
def setUpClass(cls):
100-
super().setUpClass()
101+
def _setup_class(cls):
102+
super()._setup_class()
101103
cls.listener = EventListener()
102104
cls.client = cls.unmanaged_rs_or_single_client(event_listeners=[cls.listener])
103105
cls.db = cls.client.pymongo_test
@@ -107,11 +109,11 @@ def setUpClass(cls):
107109
warnings.simplefilter("ignore", DeprecationWarning)
108110

109111
@classmethod
110-
def tearDownClass(cls):
112+
def _tearDown_class(cls):
111113
cls.warn_context.__exit__()
112114
cls.warn_context = None
113115
cls.client.close()
114-
super().tearDownClass()
116+
super()._tearDown_class()
115117

116118
def tearDown(self):
117119
self.listener.reset()

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ def async_only_test(f: str) -> bool:
188188
"test_client.py",
189189
"test_client_bulk_write.py",
190190
"test_client_context.py",
191+
"test_collation.py",
191192
"test_collection.py",
192193
"test_connections_survive_primary_stepdown_spec.py",
193194
"test_cursor.py",

0 commit comments

Comments
 (0)