##// END OF EJS Templates
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
super-admin -
r5012:2b0b3872 default
parent child Browse files
Show More
@@ -3,8 +3,9 b' import decimal'
3 3 import functools
4 4 # we keep simplejson for having dump functionality still
5 5 # orjson doesn't support it
6 import simplejson as sjson
7
6 8 import orjson
7 import simplejson
8 9 import orjson as json
9 10
10 11
@@ -26,7 +27,6 b' def _obj_dump(obj):'
26 27
27 28 :param obj:
28 29 """
29
30 30 # See "Date Time String Format" in the ECMA-262 specification.
31 31 # some code borrowed from django 1.4
32 32 if isinstance(obj, set):
@@ -62,8 +62,11 b' def _obj_dump(obj):'
62 62 raise TypeError(repr(obj) + " is not JSON serializable")
63 63
64 64
65 sjson.dumps = functools.partial(sjson.dumps, default=_obj_dump)
66 sjson.dump = functools.partial(sjson.dump, default=_obj_dump)
67
65 68 json.dumps = functools.partial(json.dumps, default=_obj_dump, option=orjson.OPT_NON_STR_KEYS)
66 json.dump = functools.partial(simplejson.dump, default=_obj_dump)
69 json.dump = functools.partial(sjson.dump, default=_obj_dump)
67 70
68 71
69 72 def formatted_json(*args, **kwargs):
@@ -18,15 +18,14 b''
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 import io
21 22 import datetime
22 23 import decimal
23 import io
24 24 import textwrap
25 25
26 26 import pytest
27 27
28 from rhodecode.lib.ext_json import json
29 from rhodecode.lib.ext_json import formatted_json
28 from rhodecode.lib import ext_json
30 29 from rhodecode.translation import _, _pluralize
31 30
32 31
@@ -38,87 +37,89 b' class Timezone(datetime.tzinfo):'
38 37 return datetime.timedelta(hours=self.hours)
39 38
40 39
40 class SerializableObject(object):
41 def __json__(self):
42 return 'foo'
43
44
41 45 def test_dumps_set():
42 result = json.dumps(set((1, 2, 3)))
46 result = ext_json.json.dumps(set((1, 2, 3)))
43 47 # We cannot infer what the order of result is going to be
44 result = json.loads(result)
48 result = ext_json.json.loads(result)
45 49 assert isinstance(result, list)
46 50 assert [1, 2, 3] == sorted(result)
47 51
48 52
49 53 def test_dumps_decimal():
50 assert '"1.5"' == json.dumps(decimal.Decimal('1.5'))
54 assert b'"1.5"' == ext_json.json.dumps(decimal.Decimal('1.5'))
51 55
52 56
53 57 def test_dumps_complex():
54 assert "[0.0, 1.0]" == json.dumps(1j)
55 assert "[1.0, 0.0]" == json.dumps(1 + 0j)
56 assert "[1.1, 1.2]" == json.dumps(1.1 + 1.2j)
58 assert b"[0.0,1.0]" == ext_json.json.dumps(1j)
59 assert b"[1.0,0.0]" == ext_json.json.dumps(1 + 0j)
60 assert b"[1.1,1.2]" == ext_json.json.dumps(1.1 + 1.2j)
57 61
58 62
59 63 def test_dumps_object_with_json_method():
60 class SerializableObject(object):
61 def __json__(self):
62 return 'foo'
63
64 assert '"foo"' == json.dumps(SerializableObject())
64 assert '"foo"' == ext_json.str_json(SerializableObject())
65 65
66 66
67 67 def test_dumps_object_with_json_attribute():
68 class SerializableObject(object):
69 __json__ = 'foo'
70 68
71 assert '"foo"' == json.dumps(SerializableObject())
69 assert '"foo"' == ext_json.str_json(SerializableObject())
72 70
73 71
74 72 def test_dumps_time():
75 assert '"03:14:15.926"' == json.dumps(datetime.time(3, 14, 15, 926535))
73 assert '"03:14:15.926535"' == ext_json.str_json(datetime.time(3, 14, 15, 926535))
76 74
77 75
78 76 def test_dumps_time_no_microseconds():
79 assert '"03:14:15"' == json.dumps(datetime.time(3, 14, 15))
77 assert '"03:14:15"' == ext_json.str_json(datetime.time(3, 14, 15))
80 78
81 79
82 80 def test_dumps_time_with_timezone():
83 81 with pytest.raises(TypeError) as excinfo:
84 json.dumps(datetime.time(3, 14, 15, 926535, Timezone(0)))
82 ext_json.json.dumps(datetime.time(3, 14, 15, 926535, Timezone(0)))
85 83
86 84 error_msg = str(excinfo.value)
87 assert 'Time-zone aware times are not JSON serializable' in error_msg
85
86 assert 'timezone library is not supported' in error_msg
87 # only for simplejson
88 #assert 'Time-zone aware times are not JSON serializable' in error_msg
88 89
89 90
90 91 def test_dumps_date():
91 assert '"1969-07-20"' == json.dumps(datetime.date(1969, 7, 20))
92 assert b'"1969-07-20"' == ext_json.json.dumps(datetime.date(1969, 7, 20))
92 93
93 94
94 95 def test_dumps_datetime():
95 json_data = json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15, 926535))
96 assert '"1969-07-20T03:14:15.926"' == json_data
96 json_data = ext_json.json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15, 926535))
97 assert b'"1969-07-20T03:14:15.926535"' == json_data
97 98
98 99
99 100 def test_dumps_datetime_no_microseconds():
100 json_data = json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15))
101 assert '"1969-07-20T03:14:15"' == json_data
101 json_data = ext_json.json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15))
102 assert b'"1969-07-20T03:14:15"' == json_data
102 103
103 104
104 105 def test_dumps_datetime_with_utc_timezone():
105 json_data = json.dumps(
106 json_data = ext_json.json.dumps(
106 107 datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(0)))
107 assert '"1969-07-20T03:14:15.926Z"' == json_data
108 assert b'"1969-07-20T03:14:15.926535+00:00"' == json_data
108 109
109 110
110 111 def test_dumps_datetime_with_plus1_timezone():
111 json_data = json.dumps(
112 json_data = ext_json.json.dumps(
112 113 datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(1)))
113 assert '"1969-07-20T03:14:15.926+01:00"' == json_data
114 assert b'"1969-07-20T03:14:15.926535+01:00"' == json_data
114 115
115 116
116 117 def test_dumps_unserializable_class():
117 118 unserializable_obj = object()
118 119 with pytest.raises(TypeError) as excinfo:
119 json.dumps(unserializable_obj)
120 ext_json.json.dumps(unserializable_obj)
120 121
121 assert repr(unserializable_obj) in str(excinfo.value)
122 assert 'object' in str(excinfo.value)
122 123 assert 'is not JSON serializable' in str(excinfo.value)
123 124
124 125
@@ -131,10 +132,10 b' def test_dump_is_like_dumps():'
131 132 'time': datetime.time(3, 14, 15, 926535),
132 133 'date': datetime.date(1969, 7, 20),
133 134 }
134 json_buffer = io.BytesIO()
135 json.dump(data, json_buffer)
135 json_buffer = io.StringIO() # StringIO because dump uses simplejson not orjson
136 ext_json.json.dump(data, json_buffer)
136 137
137 assert json.dumps(data) == json_buffer.getvalue()
138 assert ext_json.sjson.dumps(data) == json_buffer.getvalue()
138 139
139 140
140 141 def test_formatted_json():
@@ -145,22 +146,22 b' def test_formatted_json():'
145 146
146 147 expected_data = textwrap.dedent('''
147 148 {
148 "a": {
149 "3": 3,
150 "4": 4
151 },
152 "b": {
153 "1": 1,
154 "2": 2
155 }
149 "a": {
150 "3": 3,
151 "4": 4
152 },
153 "b": {
154 "1": 1,
155 "2": 2
156 }
156 157 }''').strip()
157 158
158 assert formatted_json(data) == expected_data
159 assert expected_data == ext_json.formatted_str_json(data)
159 160
160 161
161 162 def test_lazy_translation_string(baseapp):
162 163 data = {'label': _('hello')}
163 164 data2 = {'label2': _pluralize('singular', 'plural', 1)}
164 165
165 assert json.dumps(data) == '{"label": "hello"}'
166 assert json.dumps(data2) == '{"label2": "singular"}'
166 assert b'{"label":"hello"}' == ext_json.json.dumps(data)
167 assert b'{"label2":"singular"}' == ext_json.json.dumps(data2)
General Comments 0
You need to be logged in to leave comments. Login now