PYTHON-5086 - Convert test.json_util integration test to async (#2102)

This commit is contained in:
Noah Stapp 2025-01-30 16:30:04 -05:00 committed by GitHub
parent 0a1471d8f9
commit c8d3afdefd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 59 additions and 21 deletions

View File

@ -0,0 +1,28 @@
from __future__ import annotations
from test.asynchronous import AsyncIntegrationTest
from typing import Any, List, MutableMapping
from bson import Binary, Code, DBRef, ObjectId, json_util
from bson.binary import USER_DEFINED_SUBTYPE
_IS_SYNC = False
class TestJsonUtilRoundtrip(AsyncIntegrationTest):
async def test_cursor(self):
db = self.db
await db.drop_collection("test")
docs: List[MutableMapping[str, Any]] = [
{"foo": [1, 2]},
{"bar": {"hello": "world"}},
{"code": Code("function x() { return 1; }")},
{"bin": Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
{"dbref": {"_ref": DBRef("simple", ObjectId("509b8db456c02c5ab7e63c34"))}},
]
await db.test.insert_many(docs)
reloaded_docs = json_util.loads(json_util.dumps(await (db.test.find()).to_list()))
for doc in docs:
self.assertTrue(doc in reloaded_docs)

View File

@ -21,13 +21,13 @@ import re
import sys
import uuid
from collections import OrderedDict
from typing import Any, List, MutableMapping, Tuple, Type
from typing import Any, Tuple, Type
from bson.codec_options import CodecOptions, DatetimeConversion
sys.path[0:0] = [""]
from test import IntegrationTest, unittest
from test import unittest
from bson import EPOCH_AWARE, EPOCH_NAIVE, SON, DatetimeMS, json_util
from bson.binary import (
@ -636,24 +636,5 @@ class TestJsonUtil(unittest.TestCase):
self.assertEqual(json_util.dumps(MyBinary(b"bin", USER_DEFINED_SUBTYPE)), expected_json)
class TestJsonUtilRoundtrip(IntegrationTest):
def test_cursor(self):
db = self.db
db.drop_collection("test")
docs: List[MutableMapping[str, Any]] = [
{"foo": [1, 2]},
{"bar": {"hello": "world"}},
{"code": Code("function x() { return 1; }")},
{"bin": Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
{"dbref": {"_ref": DBRef("simple", ObjectId("509b8db456c02c5ab7e63c34"))}},
]
db.test.insert_many(docs)
reloaded_docs = json_util.loads(json_util.dumps(db.test.find()))
for doc in docs:
self.assertTrue(doc in reloaded_docs)
if __name__ == "__main__":
unittest.main()

View File

@ -0,0 +1,28 @@
from __future__ import annotations
from test import IntegrationTest
from typing import Any, List, MutableMapping
from bson import Binary, Code, DBRef, ObjectId, json_util
from bson.binary import USER_DEFINED_SUBTYPE
_IS_SYNC = True
class TestJsonUtilRoundtrip(IntegrationTest):
def test_cursor(self):
db = self.db
db.drop_collection("test")
docs: List[MutableMapping[str, Any]] = [
{"foo": [1, 2]},
{"bar": {"hello": "world"}},
{"code": Code("function x() { return 1; }")},
{"bin": Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
{"dbref": {"_ref": DBRef("simple", ObjectId("509b8db456c02c5ab7e63c34"))}},
]
db.test.insert_many(docs)
reloaded_docs = json_util.loads(json_util.dumps((db.test.find()).to_list()))
for doc in docs:
self.assertTrue(doc in reloaded_docs)

View File

@ -211,6 +211,7 @@ converted_tests = [
"test_heartbeat_monitoring.py",
"test_index_management.py",
"test_grid_file.py",
"test_json_util_integration.py",
"test_gridfs_spec.py",
"test_logger.py",
"test_monitoring.py",