test_issue331.py
Overview
`test_issue331.py` is a test module primarily focused on verifying the correct serialization and deserialization behavior of JSON data using the `orjson` library. The file contains a complex JSON-like Python dictionary fixture (`FIXTURE_ISSUE_335`) and multiple test functions that load JSON data from fixture files, serialize it with various options, and validate that the output matches the original input after round-trip conversion.
The tests are designed to stress test `orjson`’s ability to correctly handle both compact and pretty-printed JSON representations, including complex nested structures with datetime objects, nested dictionaries and lists, and various data types. The tests run multiple iterations (1000 times) to ensure stability and consistency.
This file appears to be part of a test suite for a larger application or library that uses `orjson` for JSON processing, possibly related to fixing or preventing regressions associated with GitHub issues #331 and #335.
Detailed Explanation
Constants
FIXTURE_ISSUE_335
Type:
dictDescription: A large, deeply nested dictionary containing various data types including strings, integers, floating-point numbers,
None, lists, nested dictionaries, datetime.datetime and datetime.date objects, and boolean values.Purpose: Used as a test fixture to validate the serialization capabilities of
orjsonon complex data structures, including support for datetime serialization.
Decorators
@needs_data
Imported from
.utilPurpose: Likely a test decorator used to mark tests that require external data files (fixtures) before execution.
Usage: Applied to test functions that load JSON data from compressed fixture files.
Functions
Each function is a test case using the `pytest` framework style, aimed at validating the serialization and deserialization of JSON data using `orjson`.
test_issue331_1_pretty()
Purpose: Tests round-trip serialization/deserialization of JSON data from the fixture file issue331_1.json.xz using pretty-printed JSON output (2-space indentation).
Parameters: None
Returns: None (asserts inside the loop)
Implementation Details:
Reads the JSON fixture bytes using
read_fixture_bytes.Loads JSON into Python object using
orjson.loads.Serializes the object back to JSON string with pretty-print option
orjson.OPT_INDENT_2.Deserializes back and asserts equality with the original object.
Runs 1000 times to ensure consistency and performance.
Example Usage: This function is intended to be run by a test runner; no external invocation needed.
test_issue331_1_compact()
Same as
test_issue331_1_pretty()but tests compact (no indentation) JSON serialization.
test_issue331_2_pretty()
Same as
test_issue331_1_pretty()but uses a different data fixture fileissue331_2.json.xz.
test_issue331_2_compact()
Same as
test_issue331_2_pretty()but tests compact JSON serialization.
test_issue335_compact()
Purpose: Tests compact JSON serialization of the
FIXTURE_ISSUE_335dictionary.Parameters: None
Returns: None (asserts inside the loop)
Implementation Details:
Runs
orjson.dumpson the fixture 1000 times.Does not deserialize back; the test implicitly checks that serialization completes without error.
Note: This test likely checks for serialization correctness and performance.
test_issue335_pretty()
Same as
test_issue335_compact()but includes pretty-print optionorjson.OPT_INDENT_2.
Important Implementation Details
Use of
orjson: This module leveragesorjson, a fast JSON library in Rust with a Python binding, known for high performance and support for advanced types likedatetime.Fixture Loading: Test functions use
read_fixture_bytesto load compressed fixture files (*.json.xz), ensuring tests run on consistent data.Repeated Execution: Each test runs 1000 iterations to check for stability and performance in repeated serialization/deserialization cycles.
Handling of Dates: The fixture contains many datetime.datetime objects with timezone info (
datetime.timezone.utc), testingorjson's datetime serialization support.Assertion Strategy: Round-trip tests assert equality between the original Python object and the object obtained after serialization and deserialization to catch any data loss or transformation errors.
Interaction with Other System Components
Imports from
.util:needs_data decorator likely integrates with the test framework to manage test data dependencies.
read_fixture_byteshandles reading compressed test fixture files, abstracting file I/O and decompression.
Fixture Files:
The tests rely on external fixture files like issue331_1.json.xz and
issue331_2.json.xz, which are expected to be part of the test data repository.
JSON Processing Library:
orjsonis the core serialization/deserialization library being tested.
Test Suite:
These tests are designed to be part of an automated test suite, probably run via
pytestor a similar framework.
Usage Examples
Since this file is a test module, usage involves running the tests, for example:
pytest test_issue331.py
This command will execute all test functions, validating the JSON serialization/deserialization behavior for the provided fixtures and the complex dictionary.
Mermaid Class Diagram
The file contains only functions (no classes), so a flowchart illustrating the relationships between the main functions and their data dependencies is appropriate:
flowchart TD
A[read_fixture_bytes] --> B[test_issue331_1_pretty]
A --> C[test_issue331_1_compact]
A --> D[test_issue331_2_pretty]
A --> E[test_issue331_2_compact]
B --> F[orjson.loads]
C --> F
D --> F
E --> F
F --> G[orjson.dumps]
G --> H[assert equality]
FIXTURE_ISSUE_335 --> I[test_issue335_compact]
FIXTURE_ISSUE_335 --> J[test_issue335_pretty]
I --> G
J --> G
Summary
`test_issue331.py` is a test module validating the correctness and robustness of JSON serialization and deserialization via `orjson`. It uses both external fixture files and an internally defined complex fixture to ensure the library handles a wide range of JSON structures, including nested objects and datetime types, in both compact and pretty-printed formats. The tests emphasize repeated execution for reliability and performance assurance. The module integrates with test infrastructure for fixture management and is part of a broader testing strategy to prevent regressions in JSON processing functionality.