test_api.py


Overview

`test_api.py` is a comprehensive test suite designed to verify the correctness, robustness, and expected behavior of the `orjson` library's core JSON serialization and deserialization functions: `orjson.dumps` and `orjson.loads`. This file uses the `pytest` framework to define multiple test cases that cover a wide range of scenarios including:

The tests ensure that `orjson` maintains compatibility with Python's built-in `json` module (for simple types), enforces recursion limits to prevent stack overflows, and validates option parameters rigorously.


Detailed Explanation of Classes and Methods

Class: TestApi

This class encapsulates all the test cases for the `orjson` library API.


Test Methods

Each method name starts with `test_` and uses `pytest` assertions or exception checks to validate behavior.

1. test_loads_trailing(self)


2. test_loads_trailing_invalid(self)


3. test_simple_json(self)


4. test_simple_round_trip(self)


5. test_loads_type(self)


6. Recursion Limit Tests:

`orjson` enforces a recursion limit (`LOADS_RECURSION_LIMIT = 1024`) to prevent stack overflow when parsing deeply nested JSON. Several tests validate this behavior.

All these test cases construct JSON strings or bytes that exceed or reach the recursion limit with arrays or objects or combinations and verify that a `JSONDecodeError` is raised.


7. Pretty-Printed Recursion Limit Tests

These tests are similar to the recursion limit tests but include newlines and spaces (pretty formatting) in the JSON input to ensure whitespace does not affect recursion limit detection.


8. test_version(self)


9. test_valueerror(self)


10. Option and Default Argument Tests

These tests validate that the `option` and `default` parameters of `orjson.dumps()` behave correctly, including error handling for invalid inputs:

**Example of combining options and default:**

class Custom:
    def __str__(self):
        return "zxc"

orjson.dumps(
    [Custom(), datetime.datetime(2000, 1, 1, 2, 3, 4)],
    default,
    option=orjson.OPT_NAIVE_UTC,
)
# Output: b'["zxc","2000-01-01T02:03:04+00:00"]'

11. Signature and Module Metadata Tests


12. Buffer and Null-Termination Tests


Important Implementation Details


Interaction with Other Parts of the System


Usage Example

Running this test suite requires `pytest` and the `orjson` package installed in the environment. The tests can be executed with:

pytest test_api.py

Visual Diagram

The following Mermaid class diagram illustrates the structure of `test_api.py`. Since the file contains a single test class with multiple test methods (no properties), the diagram focuses on this class and its methods.

classDiagram
    class TestApi {
        +test_loads_trailing()
        +test_loads_trailing_invalid()
        +test_simple_json()
        +test_simple_round_trip()
        +test_loads_type()
        +test_loads_recursion_partial()
        +test_loads_recursion_valid_limit_array()
        +test_loads_recursion_valid_limit_object()
        +test_loads_recursion_valid_limit_mixed()
        +test_loads_recursion_valid_excessive_array()
        +test_loads_recursion_valid_limit_array_pretty()
        +test_loads_recursion_valid_limit_object_pretty()
        +test_loads_recursion_valid_limit_mixed_pretty()
        +test_loads_recursion_valid_excessive_array_pretty()
        +test_version()
        +test_valueerror()
        +test_optional_none()
        +test_option_not_int()
        +test_option_invalid_int()
        +test_option_range_low()
        +test_option_range_high()
        +test_opts_multiple()
        +test_default_positional()
        +test_default_unknown_kwarg()
        +test_default_empty_kwarg()
        +test_default_twice()
        +test_option_twice()
        +test_option_mixed()
        +test_dumps_signature()
        +test_loads_signature()
        +test_dumps_module_str()
        +test_loads_module_str()
        +test_bytes_buffer()
        +test_bytes_null_terminated()
    }

Summary

`test_api.py` is a critical quality assurance component for the `orjson` library, containing an extensive set of tests that cover serialization, deserialization, error handling, recursion limits, option validation, and metadata checks. This ensures that `orjson` behaves consistently, safely, and as expected across a broad spectrum of use cases, contributing to the reliability of applications that depend on it for JSON processing.