test_circular.py


Overview

`test_circular.py` is a test module designed to verify the behavior of the `orjson` JSON serialization library when handling circular references within Python data structures. Circular references occur when an object directly or indirectly references itself, which can cause infinite loops or errors during serialization.

This file contains a suite of unit tests that ensure `orjson.dumps()` raises the appropriate `orjson.JSONEncodeError` exception when attempting to serialize circular references. The tests cover various scenarios, including circular dictionaries, lists, nested structures, and the use of different serialization options like `OPT_SORT_KEYS` and `OPT_NON_STR_KEYS`.


Classes and Methods

Class: TestCircular

This class groups together tests related to the serialization of circular references using the `orjson` library.


Method: test_circular_dict(self)

**Purpose:** Tests that serializing a dictionary containing a direct circular reference raises `orjson.JSONEncodeError`.

**Parameters:**

**Behavior:**

**Usage Example:**

test = TestCircular()
test.test_circular_dict()

Method: test_circular_dict_sort_keys(self)

**Purpose:** Tests serialization of a circular dictionary with the `OPT_SORT_KEYS` option enabled.

**Parameters:**

**Behavior:**


Method: test_circular_dict_non_str_keys(self)

**Purpose:** Tests serialization of a circular dictionary with the `OPT_NON_STR_KEYS` option enabled.

**Parameters:**

**Behavior:**


Method: test_circular_list(self)

**Purpose:** Tests serialization of a list containing a circular reference to itself.

**Parameters:**

**Behavior:**


Method: test_circular_nested(self)

**Purpose:** Tests serialization of nested data structures where a dictionary contains a list which in turn references the dictionary, forming a circular reference.

**Parameters:**

**Behavior:**


Method: test_circular_nested_sort_keys(self)

**Purpose:** Tests nested circular reference serialization with the `OPT_SORT_KEYS` option.

**Parameters:**

**Behavior:**


Method: test_circular_nested_non_str_keys(self)

**Purpose:** Tests nested circular reference serialization with the `OPT_NON_STR_KEYS` option.

**Parameters:**

**Behavior:**


Important Implementation Details


Interaction with Other Parts of the System


Visual Diagram

classDiagram
    class TestCircular {
        +test_circular_dict()
        +test_circular_dict_sort_keys()
        +test_circular_dict_non_str_keys()
        +test_circular_list()
        +test_circular_nested()
        +test_circular_nested_sort_keys()
        +test_circular_nested_non_str_keys()
    }
    TestCircular ..> orjson : uses
    TestCircular ..> pytest : uses

Summary

`test_circular.py` is a focused test suite ensuring that the `orjson` library correctly detects and rejects circular references in various data structures during JSON serialization, whether or not certain serialization options are enabled. It helps maintain the reliability and correctness of JSON encoding by preventing infinite loops or corrupted outputs caused by circular data.