File: test_indent.py

package info (click to toggle)
python-orjson 3.11.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,116 kB
  • sloc: ansic: 11,268; python: 6,796; sh: 105; makefile: 9
file content (126 lines) | stat: -rw-r--r-- 3,606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
# Copyright ijl (2020-2025)

import datetime
import json

import orjson

from .util import needs_data, read_fixture_obj


@needs_data
class TestIndentedOutput:
    def test_equivalent(self):
        """
        OPT_INDENT_2 is equivalent to indent=2
        """
        obj = {"a": "b", "c": {"d": True}, "e": [1, 2]}
        assert orjson.dumps(obj, option=orjson.OPT_INDENT_2) == json.dumps(
            obj,
            indent=2,
        ).encode("utf-8")

    def test_sort(self):
        obj = {"b": 1, "a": 2}
        assert (
            orjson.dumps(obj, option=orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS)
            == b'{\n  "a": 2,\n  "b": 1\n}'
        )

    def test_non_str(self):
        obj = {1: 1, "a": 2}
        assert (
            orjson.dumps(obj, option=orjson.OPT_INDENT_2 | orjson.OPT_NON_STR_KEYS)
            == b'{\n  "1": 1,\n  "a": 2\n}'
        )

    def test_options(self):
        obj = {
            1: 1,
            "b": True,
            "a": datetime.datetime(1970, 1, 1),
        }
        assert (
            orjson.dumps(
                obj,
                option=orjson.OPT_INDENT_2
                | orjson.OPT_SORT_KEYS
                | orjson.OPT_NON_STR_KEYS
                | orjson.OPT_NAIVE_UTC,
            )
            == b'{\n  "1": 1,\n  "a": "1970-01-01T00:00:00+00:00",\n  "b": true\n}'
        )

    def test_empty(self):
        obj = [{}, [[[]]], {"key": []}]
        ref = b'[\n  {},\n  [\n    [\n      []\n    ]\n  ],\n  {\n    "key": []\n  }\n]'
        assert orjson.dumps(obj, option=orjson.OPT_INDENT_2) == ref

    def test_list_max(self):
        fixture = b"".join(
            (b"".join(b"[" for _ in range(254)), b"".join(b"]" for _ in range(254))),
        )
        obj = orjson.loads(fixture)
        serialized = orjson.dumps(
            obj,
            option=orjson.OPT_INDENT_2,
        )
        assert orjson.loads(serialized) == obj

    def test_dict_max(self):
        fixture = {"key": None}
        target = fixture
        for _ in range(253):
            target["key"] = {"key": None}  # type:ignore
            target = target["key"]  # type: ignore

        serialized = orjson.dumps(
            fixture,
            option=orjson.OPT_INDENT_2,
        )
        assert orjson.loads(serialized) == fixture

    def test_twitter_pretty(self):
        """
        twitter.json pretty
        """
        obj = read_fixture_obj("twitter.json.xz")
        assert orjson.dumps(obj, option=orjson.OPT_INDENT_2) == json.dumps(
            obj,
            indent=2,
            ensure_ascii=False,
        ).encode("utf-8")

    def test_github_pretty(self):
        """
        github.json pretty
        """
        obj = read_fixture_obj("github.json.xz")
        assert orjson.dumps(obj, option=orjson.OPT_INDENT_2) == json.dumps(
            obj,
            indent=2,
            ensure_ascii=False,
        ).encode("utf-8")

    def test_canada_pretty(self):
        """
        canada.json pretty
        """
        obj = read_fixture_obj("canada.json.xz")
        assert orjson.dumps(obj, option=orjson.OPT_INDENT_2) == json.dumps(
            obj,
            indent=2,
            ensure_ascii=False,
        ).encode("utf-8")

    def test_citm_catalog_pretty(self):
        """
        citm_catalog.json pretty
        """
        obj = read_fixture_obj("citm_catalog.json.xz")
        assert orjson.dumps(obj, option=orjson.OPT_INDENT_2) == json.dumps(
            obj,
            indent=2,
            ensure_ascii=False,
        ).encode("utf-8")