test_sort_keys.py


Overview

`test_sort_keys.py` is a test suite designed to verify the behavior of JSON serialization with sorted keys using the [orjson](/projects/287/67747) library. The primary focus is to ensure that when JSON objects are serialized with the [OPT_SORT_KEYS](/projects/287/67684) option, their dictionary keys are correctly sorted in the serialized output.

This file contains a single test class, `TestDictSortKeys`, which includes multiple test methods. Each test method loads a JSON fixture (or creates a JSON object), confirms the original keys are unsorted, serializes the object with key sorting enabled, then deserializes it and asserts that the keys are sorted.

The tests cover various JSON inputs, including large fixture files ([twitter.json.xz](/projects/287/67742), [canada.json.xz](/projects/287/67742), [github.json.xz](/projects/287/67742)) and a small hardcoded dictionary with UTF-8 keys. This validates both typical and edge cases for key sorting behavior in JSON serialization.


Classes and Methods

Class: TestDictSortKeys

This test class is decorated with [@needs_data](/projects/287/67677), suggesting it depends on pre-existing fixture data for the tests to run successfully.

Methods:


test_twitter_sorted(self)


test_canada_sorted(self)


test_github_sorted(self)


test_utf8_sorted(self)


Implementation Details and Algorithms


Interactions with Other Parts of the System


Usage in the System


Visual Diagram

The following Mermaid class diagram summarizes the structure of the `TestDictSortKeys` class and its methods:

classDiagram
    class TestDictSortKeys {
        +test_twitter_sorted()
        +test_canada_sorted()
        +test_github_sorted()
        +test_utf8_sorted()
    }

Summary

`test_sort_keys.py` is a focused test module verifying that JSON dictionary keys are sorted when serialized with the [orjson](/projects/287/67747) library's [OPT_SORT_KEYS](/projects/287/67684) option. Through a series of tests on both fixture files and a small UTF-8 dictionary, it confirms that the serialization and deserialization process preserves the desired sorted order of keys. It leverages utility functions to load compressed JSON fixtures and is intended to run within a Python testing framework as part of a larger system ensuring JSON serialization correctness and consistency.