test_ujson.py


Overview

`test_ujson.py` is a comprehensive test suite designed to verify the functionality, correctness, and robustness of JSON serialization and deserialization using the `orjson` library. The tests focus on ensuring that `orjson` can correctly encode various Python data types (including integers, floats, strings, lists, dictionaries, booleans, and `None`) into JSON, and decode JSON back into Python objects without loss of fidelity.

This file aims to:

The file uses `pytest` as the testing framework and heavily relies on `orjson.dumps` and `orjson.loads` for JSON operations.


Classes and Methods

Class: TestUltraJSON

This class encapsulates all test cases for JSON serialization/deserialization using `orjson`. Each method tests a specific scenario or data type.


Test Methods

1. test_doubleLongIssue(self)

2. test_doubleLongDecimalIssue(self)

3. test_encodeDecodeLongDecimal(self)

4. test_decimalDecodeTest(self)

5. test_encodeDictWithUnicodeKeys(self)

6. test_encodeArrayOfNestedArrays(self)

7. test_encodeArrayOfDoubles(self)

8. test_encodeStringConversion2(self)

9. test_decodeUnicodeConversion(self)

10. test_encodeUnicodeConversion1(self)

11. test_encodeControlEscaping(self)

12-16. Various test_encodeUnicode... methods

17. testEncodeUnicodeBMP(self)

18. testEncodeSymbols(self)

19-26. Various tests for arrays, integers (positive and negative), long integers, lists, dictionaries, None, booleans.

27. test_encodeToUTF8(self)

28. test_decodeFromUnicode(self)

29-35. Various tests for error conditions in decoding:

36-38. Leak tests for broken dict keys and lists.

39-41. Additional malformed JSON tests raising exceptions.

42-43. Numeric decoding tests for positive and negative integers.

44-51. Null character encoding/decoding tests.

52-54. Escape sequence decoding tests.


Important Implementation Details


Interaction with Other Parts of the System


Usage

To run the tests in this file, ensure that `pytest` and `orjson` are installed:

pip install pytest orjson
pytest test_ujson.py

Visual Diagram

classDiagram
    class TestUltraJSON {
        +test_doubleLongIssue()
        +test_doubleLongDecimalIssue()
        +test_encodeDecodeLongDecimal()
        +test_decimalDecodeTest()
        +test_encodeDictWithUnicodeKeys()
        +test_encodeArrayOfNestedArrays()
        +test_encodeArrayOfDoubles()
        +test_encodeStringConversion2()
        +test_decodeUnicodeConversion()
        +test_encodeUnicodeConversion1()
        +test_encodeControlEscaping()
        +test_encodeUnicodeConversion2()
        +test_encodeUnicodeSurrogatePair()
        +test_encodeUnicode4BytesUTF8()
        +test_encodeUnicode4BytesUTF8Highest()
        +testEncodeUnicodeBMP()
        +testEncodeSymbols()
        +test_encodeArrayInArray()
        +test_encodeIntConversion()
        +test_encodeIntNegConversion()
        +test_encodeLongNegConversion()
        +test_encodeListConversion()
        +test_encodeDictConversion()
        +test_encodeNoneConversion()
        +test_encodeTrueConversion()
        +test_encodeFalseConversion()
        +test_encodeToUTF8()
        +test_decodeFromUnicode()
        +test_decodeJibberish()
        +test_decodeBrokenArrayStart()
        +test_decodeBrokenObjectStart()
        +test_decodeBrokenArrayEnd()
        +test_decodeBrokenObjectEnd()
        +test_decodeObjectDepthTooBig()
        +test_decodeStringUnterminated()
        +test_decodeStringUntermEscapeSequence()
        +test_decodeStringBadEscape()
        +test_decodeTrueBroken()
        +test_decodeFalseBroken()
        +test_decodeNullBroken()
        +test_decodeBrokenDictKeyTypeLeakTest()
        +test_decodeBrokenDictLeakTest()
        +test_decodeBrokenListLeakTest()
        +test_decodeDictWithNoKey()
        +test_decodeDictWithNoColonOrValue()
        +test_decodeDictWithNoValue()
        +test_decodeNumericIntPos()
        +test_decodeNumericIntNeg()
        +test_encodeNullCharacter()
        +test_decodeNullCharacter()
        +test_decodeEscape()
        +test_decodeBigEscape()
    }

Summary

`test_ujson.py` is a detailed and thorough test suite validating the correctness and robustness of JSON serialization and parsing via the `orjson` library. It covers common use cases, edge cases, Unicode intricacies, and error handling, ensuring that `orjson` behaves as expected in a wide range of scenarios. The file is critical for maintaining the integrity of JSON handling in any application relying on `orjson` for performance and accuracy.