Skip to content

Commit 189ced9

Browse files
committed
test
1 parent 66754d6 commit 189ced9

9 files changed

Lines changed: 1104 additions & 3 deletions

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
dist: xenial
22
language: python
33
python:
4-
- "3.5"
54
- "3.6"
65
- "3.7"
76
install:
8-
- pip install --upgrade pip wheel pyo3-pack pytest
7+
- pip install --upgrade pip wheel pyo3-pack pytest hypothesis
98
- curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly -y
109
- PATH=$HOME/.cargo/bin:$PATH cargo update
1110
script:

test/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-

test/test_api.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,55 @@
11
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

33
import unittest
4+
import json
45

56
import orjson
67

78

9+
SIMPLE_TYPES = (1, 1.0, -1, None, "str", True, False)
10+
11+
812
class ApiTests(unittest.TestCase):
913

14+
def test_loads_trailing(self):
15+
"""
16+
loads() handles trailing whitespace
17+
"""
18+
self.assertEqual(orjson.loads('{}\n\t '), {})
19+
20+
def test_loads_trailing_invalid(self):
21+
"""
22+
loads() handles trailing invalid
23+
"""
24+
self.assertRaises(orjson.JSONDecodeError, orjson.loads, '{}\n\t a')
25+
26+
def test_simple_json(self):
27+
"""
28+
dumps() equivalent to json on simple types
29+
"""
30+
for obj in SIMPLE_TYPES:
31+
self.assertEqual(orjson.dumps(obj), json.dumps(obj).encode('utf-8'))
32+
33+
def test_simple_round_trip(self):
34+
"""
35+
dumps(), loads() round trip on simple types
36+
"""
37+
for obj in SIMPLE_TYPES:
38+
self.assertEqual(orjson.loads(orjson.dumps(obj)), obj)
39+
40+
def test_loads_type(self):
41+
"""
42+
loads() invalid type
43+
"""
44+
for val in (1, 3.14, [], {}, None):
45+
self.assertRaises(TypeError, orjson.loads, val)
46+
47+
def test_loads_recursion(self):
48+
"""
49+
loads() recursion limit
50+
"""
51+
self.assertRaises(orjson.JSONDecodeError, orjson.loads, '[' * (1024 * 1024))
52+
1053
def test_version(self):
1154
"""
1255
__version__

test/test_fixture.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
2+
3+
import json
4+
import lzma
5+
import os
6+
import unittest
7+
8+
import orjson
9+
10+
11+
dirname = os.path.dirname(__file__)
12+
13+
14+
def fixture_fileh(filename):
15+
return lzma.open(os.path.join(dirname, '../data', filename), 'r')
16+
17+
18+
def read_fixture(filename):
19+
with fixture_fileh(filename) as fileh:
20+
return fileh.read().decode('utf-8')
21+
22+
23+
class FixtureTests(unittest.TestCase):
24+
25+
def test_twitter(self):
26+
"""
27+
loads(),dumps() twitter.json
28+
"""
29+
val = read_fixture('twitter.json.xz')
30+
read = json.loads(val)
31+
orjson.dumps(read)
32+
33+
def test_canada(self):
34+
"""
35+
loads(), dumps() canada.json
36+
"""
37+
val = read_fixture('canada.json.xz')
38+
read = orjson.loads(val)
39+
orjson.dumps(read)
40+
41+
def test_citm_catalog(self):
42+
"""
43+
loads(), dumps() citm_catalog.json
44+
"""
45+
val = read_fixture('citm_catalog.json.xz')
46+
read = orjson.loads(val)
47+
orjson.dumps(read)
48+
49+
def test_blns(self):
50+
"""
51+
loads() blns.json JSONDecodeError
52+
53+
https://github.com/minimaxir/big-list-of-naughty-strings
54+
"""
55+
val = read_fixture('blns.json.xz')
56+
with self.assertRaises(json.decoder.JSONDecodeError):
57+
_ = orjson.loads(val)

test/test_hypothesis.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
2+
3+
import pytest
4+
from hypothesis import given, strategies as st
5+
6+
import orjson
7+
8+
9+
# min, max: RFC 7159
10+
st_int = st.integers(min_value=-(2**53)+1, max_value=(2**53)-1)
11+
st_floats = st.floats(min_value=-(2**53)+1, max_value=(2**53)-1)
12+
13+
# st.floats would be nice, but then we need pytest.approx, which doesn't work with eg. text
14+
st_json = st.recursive(st.booleans() | st.none() | st_int # | st_floats # st.text() |
15+
, lambda children: st.lists(children) | st.dictionaries(st.text(), children))
16+
17+
18+
@given(st_floats)
19+
def test_floats(xs):
20+
assert orjson.loads(orjson.dumps(xs)) == pytest.approx(
21+
xs) # fails when abs=0.05
22+
23+
24+
@given(st.text())
25+
def test_text(xs):
26+
assert orjson.loads(orjson.dumps(xs)) == xs
27+
28+
29+
@given(st.booleans())
30+
def test_bool(xs):
31+
assert orjson.loads(orjson.dumps(xs)) == xs
32+
33+
34+
@given(st.none())
35+
def test_none(xs):
36+
assert orjson.loads(orjson.dumps(xs)) == xs
37+
38+
39+
@given(st.lists(st_int))
40+
def test_list_integers(lst):
41+
assert orjson.loads(orjson.dumps(lst)) == lst
42+
43+
44+
@given(st.lists(st.floats(min_value=-(2**53)+1, max_value=(2**53)-1)))
45+
def test_list_floats(lst):
46+
assert orjson.loads(orjson.dumps(lst)) == pytest.approx(lst)
47+
48+
49+
@given(st.lists(st.text()))
50+
def test_list_text(lst):
51+
assert orjson.loads(orjson.dumps(lst)) == lst
52+
53+
54+
@given(st.lists(st.one_of(st_int, st_floats)))
55+
def test_list_mixed(lst):
56+
assert orjson.loads(orjson.dumps(lst)) == pytest.approx(lst)
57+
58+
59+
@given(st_json)
60+
def test_json_obj(j_obj):
61+
assert orjson.loads(orjson.dumps(j_obj)) == j_obj

0 commit comments

Comments
 (0)