Skip to content

Commit c8d3afd

Browse files
authored
PYTHON-5086 - Convert test.json_util integration test to async (#2102)
1 parent 0a1471d commit c8d3afd

File tree

4 files changed

+59
-21
lines changed

4 files changed

+59
-21
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
from test.asynchronous import AsyncIntegrationTest
4+
from typing import Any, List, MutableMapping
5+
6+
from bson import Binary, Code, DBRef, ObjectId, json_util
7+
from bson.binary import USER_DEFINED_SUBTYPE
8+
9+
_IS_SYNC = False
10+
11+
12+
class TestJsonUtilRoundtrip(AsyncIntegrationTest):
13+
async def test_cursor(self):
14+
db = self.db
15+
16+
await db.drop_collection("test")
17+
docs: List[MutableMapping[str, Any]] = [
18+
{"foo": [1, 2]},
19+
{"bar": {"hello": "world"}},
20+
{"code": Code("function x() { return 1; }")},
21+
{"bin": Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
22+
{"dbref": {"_ref": DBRef("simple", ObjectId("509b8db456c02c5ab7e63c34"))}},
23+
]
24+
25+
await db.test.insert_many(docs)
26+
reloaded_docs = json_util.loads(json_util.dumps(await (db.test.find()).to_list()))
27+
for doc in docs:
28+
self.assertTrue(doc in reloaded_docs)

test/test_json_util.py

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
import sys
2222
import uuid
2323
from collections import OrderedDict
24-
from typing import Any, List, MutableMapping, Tuple, Type
24+
from typing import Any, Tuple, Type
2525

2626
from bson.codec_options import CodecOptions, DatetimeConversion
2727

2828
sys.path[0:0] = [""]
2929

30-
from test import IntegrationTest, unittest
30+
from test import unittest
3131

3232
from bson import EPOCH_AWARE, EPOCH_NAIVE, SON, DatetimeMS, json_util
3333
from bson.binary import (
@@ -636,24 +636,5 @@ class MyBinary(Binary):
636636
self.assertEqual(json_util.dumps(MyBinary(b"bin", USER_DEFINED_SUBTYPE)), expected_json)
637637

638638

639-
class TestJsonUtilRoundtrip(IntegrationTest):
640-
def test_cursor(self):
641-
db = self.db
642-
643-
db.drop_collection("test")
644-
docs: List[MutableMapping[str, Any]] = [
645-
{"foo": [1, 2]},
646-
{"bar": {"hello": "world"}},
647-
{"code": Code("function x() { return 1; }")},
648-
{"bin": Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
649-
{"dbref": {"_ref": DBRef("simple", ObjectId("509b8db456c02c5ab7e63c34"))}},
650-
]
651-
652-
db.test.insert_many(docs)
653-
reloaded_docs = json_util.loads(json_util.dumps(db.test.find()))
654-
for doc in docs:
655-
self.assertTrue(doc in reloaded_docs)
656-
657-
658639
if __name__ == "__main__":
659640
unittest.main()

test/test_json_util_integration.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from __future__ import annotations
2+
3+
from test import IntegrationTest
4+
from typing import Any, List, MutableMapping
5+
6+
from bson import Binary, Code, DBRef, ObjectId, json_util
7+
from bson.binary import USER_DEFINED_SUBTYPE
8+
9+
_IS_SYNC = True
10+
11+
12+
class TestJsonUtilRoundtrip(IntegrationTest):
13+
def test_cursor(self):
14+
db = self.db
15+
16+
db.drop_collection("test")
17+
docs: List[MutableMapping[str, Any]] = [
18+
{"foo": [1, 2]},
19+
{"bar": {"hello": "world"}},
20+
{"code": Code("function x() { return 1; }")},
21+
{"bin": Binary(b"\x00\x01\x02\x03\x04", USER_DEFINED_SUBTYPE)},
22+
{"dbref": {"_ref": DBRef("simple", ObjectId("509b8db456c02c5ab7e63c34"))}},
23+
]
24+
25+
db.test.insert_many(docs)
26+
reloaded_docs = json_util.loads(json_util.dumps((db.test.find()).to_list()))
27+
for doc in docs:
28+
self.assertTrue(doc in reloaded_docs)

tools/synchro.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ def async_only_test(f: str) -> bool:
211211
"test_heartbeat_monitoring.py",
212212
"test_index_management.py",
213213
"test_grid_file.py",
214+
"test_json_util_integration.py",
214215
"test_gridfs_spec.py",
215216
"test_logger.py",
216217
"test_monitoring.py",

0 commit comments

Comments
 (0)