Skip to content

PYTHON-4179 Verify document_class type in json_util.loads test #1509

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 1 commit into from
Feb 6, 2024
Merged
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
17 changes: 8 additions & 9 deletions test/test_json_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import re
import sys
import uuid
from collections import OrderedDict
from typing import Any, List, MutableMapping, Tuple, Type

from bson.codec_options import CodecOptions, DatetimeConversion
Expand Down Expand Up @@ -557,15 +558,13 @@ def test_numberlong(self):
)

def test_loads_document_class(self):
# document_class dict should always work
self.assertEqual(
{"foo": "bar"},
json_util.loads('{"foo": "bar"}', json_options=JSONOptions(document_class=dict)),
)
self.assertEqual(
SON([("foo", "bar"), ("b", 1)]),
json_util.loads('{"foo": "bar", "b": 1}', json_options=JSONOptions(document_class=SON)),
)
json_doc = '{"foo": "bar", "b": 1, "d": {"a": 1}}'
expected_doc = {"foo": "bar", "b": 1, "d": {"a": 1}}
for cls in (dict, SON, OrderedDict):
doc = json_util.loads(json_doc, json_options=JSONOptions(document_class=cls))
self.assertEqual(doc, expected_doc)
self.assertIsInstance(doc, cls)
self.assertIsInstance(doc["d"], cls)

def test_encode_subclass(self):
cases: list[Tuple[Type, Any]] = [
Expand Down