Skip to content

Commit 7dffe56

Browse files
committed
added unit test to check that @type is correctly set when JSONDumper.dumps() is called with a pydantic instance
1 parent 2772b8c commit 7dffe56

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

tests/test_loaders_dumpers/test_dumpers_pydantic.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ def test_json_dumper(self):
5454
"creator" not in data["books"][i].keys()
5555
self.assertEqual(data, remove_empty_items(self.bookseries.model_dump()))
5656

57+
# test @type is added to the top level and is assigned correct type when inject_type is True
58+
bookseries_with_type = json.loads(json_dumper.dumps(self.bookseries, inject_type=True))
59+
self.assertIn("@type", bookseries_with_type)
60+
self.assertEqual("BookSeries", bookseries_with_type.get("@type"))
61+
62+
# test @type is not added to the top level when inject_type is False
63+
bookseries_without_type = json.loads(json_dumper.dumps(self.bookseries, inject_type=False))
64+
self.assertNotIn("@type", bookseries_without_type)
65+
self.assertEqual(None, bookseries_without_type.get("@type"))
66+
5767

5868
class PydanticDumpersDateTestCase(LoaderDumperTestCase):
5969
@classmethod

0 commit comments

Comments
 (0)