Skip to content

Commit a8a3b4e

Browse files
authored
Merge branch 'master' into PYTHON-5814
2 parents 1ac4967 + 18757e1 commit a8a3b4e

9 files changed

Lines changed: 64 additions & 104 deletions

File tree

test/asynchronous/test_encryption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2922,7 +2922,7 @@ async def encrypt_and_cast(i):
29222922
EncryptionError, "expected matching 'min' and value type. Got range option"
29232923
):
29242924
await self.client_encryption.encrypt(
2925-
6 if cast_func is int else float(6),
2925+
6 if cast_func is not int else float(6),
29262926
key_id=self.key1_id,
29272927
algorithm=Algorithm.RANGE,
29282928
contention_factor=0,

test/asynchronous/test_pooling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ async def test_no_wait_queue_timeout(self):
346346
async with pool.checkout() as s1:
347347
t = SocketGetter(self.c, pool)
348348
await t.start()
349-
while t.state != "get_socket": # noqa: ASYNC110
349+
while t.state != "get_socket": # noqa: ASYNC110, RUF100
350350
await asyncio.sleep(0.1)
351351

352352
await asyncio.sleep(1)
353353
self.assertEqual(t.state, "get_socket")
354354

355-
while t.state != "connection": # noqa: ASYNC110
355+
while t.state != "connection": # noqa: ASYNC110, RUF100
356356
await asyncio.sleep(0.1)
357357

358358
self.assertEqual(t.state, "connection")
@@ -521,7 +521,7 @@ async def test_pool_backpressure_preserves_existing_connections(self):
521521
await coll.insert_many([{"x": 1} for _ in range(10)])
522522
t = SocketGetter(self.c, pool)
523523
await t.start()
524-
while t.state != "connection": # noqa: ASYNC110
524+
while t.state != "connection": # noqa: ASYNC110, RUF100
525525
await asyncio.sleep(0.1)
526526

527527
assert not t.sock.conn_closed()

test/asynchronous/utils_spec_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ async def _create_tests(self):
160160
dirname = os.path.split(dirpath)[-1]
161161

162162
for filename in filenames:
163-
with open(os.path.join(dirpath, filename)) as scenario_stream: # noqa: ASYNC230
163+
with open(os.path.join(dirpath, filename)) as scenario_stream: # noqa: ASYNC230, RUF100
164164
# Use tz_aware=False to match how CodecOptions decodes
165165
# dates.
166166
opts = json_util.JSONOptions(tz_aware=False)

test/handshake/unified/opmsg-not-supported.json

Lines changed: 0 additions & 74 deletions
This file was deleted.

test/test_bson.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1275,6 +1275,18 @@ def test_binary_length_accounts_for_header(self):
12751275
with self.assertRaises(InvalidBSON):
12761276
decode(payload)
12771277

1278+
def test_large_list_encoding(self):
1279+
# gen_list_name yields pre-cached names for indices 0-999 then
1280+
# generates on the fly for 1000+. Encode a list that crosses that
1281+
# boundary and verify the round-trip is correct.
1282+
values = list(range(1002))
1283+
decoded = decode(encode({"a": values}))
1284+
self.assertEqual(decoded["a"], values)
1285+
# Spot-check elements on both sides of the cache/on-demand boundary.
1286+
self.assertEqual(decoded["a"][999], 999)
1287+
self.assertEqual(decoded["a"][1000], 1000)
1288+
self.assertEqual(decoded["a"][1001], 1001)
1289+
12781290

12791291
class TestCodecOptions(unittest.TestCase):
12801292
def test_document_class(self):
@@ -1746,6 +1758,40 @@ def test_array_of_documents_to_buffer(self):
17461758
with self.assertRaises(InvalidBSON):
17471759
_array_of_documents_to_buffer(buf)
17481760

1761+
def test_datetime_ms_hash(self):
1762+
# Equal values must have equal hashes.
1763+
self.assertEqual(hash(DatetimeMS(0)), hash(DatetimeMS(0)))
1764+
self.assertEqual(hash(DatetimeMS(-1)), hash(DatetimeMS(-1)))
1765+
self.assertEqual(hash(DatetimeMS(2**62)), hash(DatetimeMS(2**62)))
1766+
# Usable as a dict key.
1767+
d = {DatetimeMS(0): "epoch", DatetimeMS(1): "one"}
1768+
self.assertEqual(d[DatetimeMS(0)], "epoch")
1769+
self.assertEqual(d[DatetimeMS(1)], "one")
1770+
# Usable in a set.
1771+
s = {DatetimeMS(0), DatetimeMS(1), DatetimeMS(0)}
1772+
self.assertEqual(len(s), 2)
1773+
1774+
def test_datetime_ms_repr(self):
1775+
self.assertEqual(repr(DatetimeMS(0)), "DatetimeMS(0)")
1776+
self.assertEqual(repr(DatetimeMS(-1)), "DatetimeMS(-1)")
1777+
self.assertEqual(repr(DatetimeMS(2**62)), f"DatetimeMS({2**62})")
1778+
# repr round-trips through eval.
1779+
for value in (0, 1, -1, 2**32):
1780+
obj = DatetimeMS(value)
1781+
self.assertEqual(eval(repr(obj)), obj)
1782+
1783+
def test_datetime_ms_invalid_type(self):
1784+
# Non-int, non-datetime arguments must raise TypeError.
1785+
for bad in ("2024-01-01", 3.14, [], None, b"bytes"):
1786+
with self.assertRaises(TypeError):
1787+
DatetimeMS(bad) # type: ignore[arg-type]
1788+
# Out-of-range int must raise OverflowError directly, not only
1789+
# when encoding.
1790+
with self.assertRaises(OverflowError):
1791+
DatetimeMS(2**63)
1792+
with self.assertRaises(OverflowError):
1793+
DatetimeMS(-(2**63) - 1)
1794+
17491795

17501796
class TestLongLongToString(unittest.TestCase):
17511797
def test_long_long_to_string(self):

test/test_client_metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def test_doesnt_update_established_connections(self):
193193
)
194194

195195
# send initial metadata
196-
name, version, platform, _ = self.send_ping_and_get_metadata(client, True)
196+
name, version, platform, _metadata = self.send_ping_and_get_metadata(client, True)
197197
self.assertIsNotNone(name)
198198
self.assertIsNotNone(version)
199199
self.assertIsNotNone(platform)

test/test_pooling.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,13 @@ def test_no_wait_queue_timeout(self):
346346
with pool.checkout() as s1:
347347
t = SocketGetter(self.c, pool)
348348
t.start()
349-
while t.state != "get_socket":
349+
while t.state != "get_socket": # noqa: ASYNC110, RUF100
350350
time.sleep(0.1)
351351

352352
time.sleep(1)
353353
self.assertEqual(t.state, "get_socket")
354354

355-
while t.state != "connection":
355+
while t.state != "connection": # noqa: ASYNC110, RUF100
356356
time.sleep(0.1)
357357

358358
self.assertEqual(t.state, "connection")
@@ -519,7 +519,7 @@ def test_pool_backpressure_preserves_existing_connections(self):
519519
coll.insert_many([{"x": 1} for _ in range(10)])
520520
t = SocketGetter(self.c, pool)
521521
t.start()
522-
while t.state != "connection":
522+
while t.state != "connection": # noqa: ASYNC110, RUF100
523523
time.sleep(0.1)
524524

525525
assert not t.sock.conn_closed()

test/utils_spec_runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _create_tests(self):
160160
dirname = os.path.split(dirpath)[-1]
161161

162162
for filename in filenames:
163-
with open(os.path.join(dirpath, filename)) as scenario_stream: # noqa: RUF100
163+
with open(os.path.join(dirpath, filename)) as scenario_stream: # noqa: ASYNC230, RUF100
164164
# Use tz_aware=False to match how CodecOptions decodes
165165
# dates.
166166
opts = json_util.JSONOptions(tz_aware=False)

tools/synchro.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,22 +171,14 @@
171171
if not Path.exists(Path(_gridfs_dest_base)):
172172
Path.mkdir(Path(_gridfs_dest_base))
173173

174-
async_files = [
175-
_pymongo_base + str(f)
176-
for f in Path.iterdir(Path(_pymongo_base))
177-
if (Path(_pymongo_base) / f).is_file()
178-
]
174+
async_files = [_pymongo_base + f.name for f in Path(_pymongo_base).iterdir() if f.is_file()]
179175

180-
gridfs_files = [
181-
_gridfs_base + str(f)
182-
for f in Path.iterdir(Path(_gridfs_base))
183-
if (Path(_gridfs_base) / f).is_file()
184-
]
176+
gridfs_files = [_gridfs_base + f.name for f in Path(_gridfs_base).iterdir() if f.is_file()]
185177

186178

187179
def async_only_test(f: Path) -> bool:
188180
"""Return True for async tests that should not be converted to sync."""
189-
return str(f) in [
181+
return f.name in [
190182
"test_locks.py",
191183
"test_concurrency.py",
192184
"test_async_cancellation.py",
@@ -197,9 +189,9 @@ def async_only_test(f: Path) -> bool:
197189

198190

199191
test_files = [
200-
_test_base + str(f)
201-
for f in Path.iterdir(Path(_test_base))
202-
if (Path(_test_base) / f).is_file() and not async_only_test(f)
192+
_test_base + f.name
193+
for f in Path(_test_base).iterdir()
194+
if f.is_file() and not async_only_test(f)
203195
]
204196

205197
# Add each asynchronized test here as part of the converting PR
@@ -453,15 +445,11 @@ def main() -> None:
453445
unasync_directory(test_files, _test_base, _test_dest_base, replacements)
454446

455447
sync_files = [
456-
_pymongo_dest_base + str(f)
457-
for f in Path.iterdir(Path(_pymongo_dest_base))
458-
if (Path(_pymongo_dest_base) / f).is_file()
448+
_pymongo_dest_base + f.name for f in Path(_pymongo_dest_base).iterdir() if f.is_file()
459449
]
460450

461451
sync_gridfs_files = [
462-
_gridfs_dest_base + str(f)
463-
for f in Path.iterdir(Path(_gridfs_dest_base))
464-
if (Path(_gridfs_dest_base) / f).is_file()
452+
_gridfs_dest_base + f.name for f in Path(_gridfs_dest_base).iterdir() if f.is_file()
465453
]
466454
sync_test_files = [
467455
_test_dest_base + f for f in converted_tests if (Path(_test_dest_base) / f).is_file()

0 commit comments

Comments
 (0)