forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_indent.py
More file actions
92 lines (80 loc) · 2.6 KB
/
Copy pathtest_indent.py
File metadata and controls
92 lines (80 loc) · 2.6 KB
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
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import datetime
import json
import unittest
import orjson
from .util import read_fixture_obj
class IndentedOutputTests(unittest.TestCase):
def test_equivalent(self):
"""
OPT_INDENT_2 is equivalent to indent=2
"""
obj = {"a": "b", "c": {"d": True}, "e": [1, 2]}
self.assertEqual(
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}
self.assertEqual(
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}
self.assertEqual(
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),
}
self.assertEqual(
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_twitter_pretty(self):
"""
twitter.json pretty
"""
obj = read_fixture_obj("twitter.json.xz")
self.assertEqual(
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")
self.assertEqual(
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")
self.assertEqual(
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")
self.assertEqual(
orjson.dumps(obj, option=orjson.OPT_INDENT_2),
json.dumps(obj, indent=2, ensure_ascii=False).encode("utf-8"),
)