Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/asynchronous/test_json_util_integration.py
Original file line number Diff line number Diff line change
@@ -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)
23 changes: 2 additions & 21 deletions test/test_json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
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 (
Expand Down Expand Up @@ -636,24 +636,5 @@ class MyBinary(Binary):
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()
28 changes: 28 additions & 0 deletions test/test_json_util_integration.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions tools/synchro.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ def async_only_test(f: str) -> bool:
"test_encryption.py",
"test_index_management.py",
"test_grid_file.py",
"test_json_util_integration.py",
"test_gridfs_spec.py",
"test_logger.py",
"test_monitoring.py",
Expand Down
Loading