Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -207,6 +207,7 @@ def async_only_test(f: str) -> bool:
"test_data_lake.py",
"test_encryption.py",
"test_grid_file.py",
"test_json_util_integration.py",
"test_logger.py",
"test_monitoring.py",
"test_raw_bson.py",
Expand Down
Loading