forked from ijl/orjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
209 lines (176 loc) · 5.75 KB
/
Copy pathtest_api.py
File metadata and controls
209 lines (176 loc) · 5.75 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
import datetime
import inspect
import json
import unittest
import orjson
SIMPLE_TYPES = (1, 1.0, -1, None, "str", True, False)
def default(obj):
return str(obj)
class ApiTests(unittest.TestCase):
def test_loads_trailing(self):
"""
loads() handles trailing whitespace
"""
self.assertEqual(orjson.loads("{}\n\t "), {})
def test_loads_trailing_invalid(self):
"""
loads() handles trailing invalid
"""
self.assertRaises(orjson.JSONDecodeError, orjson.loads, "{}\n\t a")
def test_simple_json(self):
"""
dumps() equivalent to json on simple types
"""
for obj in SIMPLE_TYPES:
self.assertEqual(orjson.dumps(obj), json.dumps(obj).encode("utf-8"))
def test_simple_round_trip(self):
"""
dumps(), loads() round trip on simple types
"""
for obj in SIMPLE_TYPES:
self.assertEqual(orjson.loads(orjson.dumps(obj)), obj)
def test_loads_type(self):
"""
loads() invalid type
"""
for val in (1, 3.14, [], {}, None):
self.assertRaises(orjson.JSONDecodeError, orjson.loads, val)
def test_loads_recursion(self):
"""
loads() recursion limit
"""
self.assertRaises(orjson.JSONDecodeError, orjson.loads, "[" * (1024 * 1024))
def test_version(self):
"""
__version__
"""
self.assertRegex(orjson.__version__, r"^\d+\.\d+(\.\d+)?$")
def test_valueerror(self):
"""
orjson.JSONDecodeError is a subclass of ValueError
"""
self.assertRaises(orjson.JSONDecodeError, orjson.loads, "{")
self.assertRaises(ValueError, orjson.loads, "{")
def test_option_not_int(self):
"""
dumps() option not int or None
"""
with self.assertRaises(orjson.JSONEncodeError):
orjson.dumps(True, option=True)
def test_option_invalid_int(self):
"""
dumps() option invalid 64-bit number
"""
with self.assertRaises(orjson.JSONEncodeError):
orjson.dumps(True, option=9223372036854775809)
def test_option_range_low(self):
"""
dumps() option out of range low
"""
with self.assertRaises(orjson.JSONEncodeError):
orjson.dumps(True, option=-1)
def test_option_range_high(self):
"""
dumps() option out of range high
"""
with self.assertRaises(orjson.JSONEncodeError):
orjson.dumps(True, option=1 << 12)
def test_opts_multiple(self):
"""
dumps() multiple option
"""
self.assertEqual(
orjson.dumps(
[1, datetime.datetime(2000, 1, 1, 2, 3, 4)],
option=orjson.OPT_STRICT_INTEGER | orjson.OPT_NAIVE_UTC,
),
b'[1,"2000-01-01T02:03:04+00:00"]',
)
def test_default_positional(self):
"""
dumps() positional arg
"""
with self.assertRaises(TypeError):
orjson.dumps(__obj={})
with self.assertRaises(TypeError):
orjson.dumps(zxc={})
def test_default_unknown_kwarg(self):
"""
dumps() unknown kwarg
"""
with self.assertRaises(TypeError):
orjson.dumps({}, zxc=default)
def test_default_empty_kwarg(self):
"""
dumps() empty kwarg
"""
self.assertEqual(orjson.dumps(None, **{}), b"null")
def test_default_twice(self):
"""
dumps() default twice
"""
with self.assertRaises(TypeError):
orjson.dumps({}, default, default=default)
def test_option_twice(self):
"""
dumps() option twice
"""
with self.assertRaises(TypeError):
orjson.dumps({}, None, orjson.OPT_NAIVE_UTC, option=orjson.OPT_NAIVE_UTC)
def test_option_mixed(self):
"""
dumps() option one arg, one kwarg
"""
class Custom:
def __str__(self):
return "zxc"
self.assertEqual(
orjson.dumps(
[Custom(), datetime.datetime(2000, 1, 1, 2, 3, 4)],
default,
option=orjson.OPT_NAIVE_UTC,
),
b'["zxc","2000-01-01T02:03:04+00:00"]',
)
def test_dumps_signature(self):
"""
dumps() valid __text_signature__
"""
self.assertEqual(
str(inspect.signature(orjson.dumps)), "(obj, /, default=None, option=None)"
)
inspect.signature(orjson.dumps).bind("str")
inspect.signature(orjson.dumps).bind("str", default=default, option=1)
def test_loads_signature(self):
"""
loads() valid __text_signature__
"""
self.assertEqual(str(inspect.signature(orjson.loads)), "(obj, /)")
inspect.signature(orjson.loads).bind("[]")
def test_dumps_module_str(self):
"""
orjson.dumps.__module__ is a str
"""
self.assertEqual(orjson.dumps.__module__, "orjson")
def test_loads_module_str(self):
"""
orjson.loads.__module__ is a str
"""
self.assertEqual(orjson.loads.__module__, "orjson")
def test_bytes_buffer(self):
"""
dumps() trigger buffer growing where length is greater than growth
"""
a = "a" * 900
b = "b" * 4096
c = "c" * 4096 * 4096
self.assertEqual(
orjson.dumps([a, b, c]), f'["{a}","{b}","{c}"]'.encode("utf-8")
)
def test_bytes_null_terminated(self):
"""
dumps() PyBytesObject buffer is null-terminated
"""
# would raise ValueError: invalid literal for int() with base 10: b'1596728892'
int(orjson.dumps(1596728892))