##// END OF EJS Templates
deps: bumped prompt_toolkit==3.0.48
deps: bumped prompt_toolkit==3.0.48

File last commit:

r5169:2045434b default
r5600:4277d591 default
Show More
test_ext_json.py
178 lines | 5.2 KiB | text/x-python | PythonLexer
slack: fixed celery serialization breaking slack_data dataclass
r5169 import dataclasses
copyrights: updated for 2023
r5088 # Copyright (C) 2010-2023 RhodeCode GmbH
project: added all source files and assets
r1 #
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 import io
project: added all source files and assets
r1 import datetime
import decimal
import textwrap
import pytest
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 from rhodecode.lib import ext_json
pylons: remove pylons as dependency...
r2351 from rhodecode.translation import _, _pluralize
project: added all source files and assets
r1
class Timezone(datetime.tzinfo):
def __init__(self, hours):
self.hours = hours
def utcoffset(self, unused_dt):
return datetime.timedelta(hours=self.hours)
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 class SerializableObject(object):
def __json__(self):
return 'foo'
project: added all source files and assets
r1 def test_dumps_set():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 result = ext_json.json.dumps(set((1, 2, 3)))
project: added all source files and assets
r1 # We cannot infer what the order of result is going to be
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 result = ext_json.json.loads(result)
project: added all source files and assets
r1 assert isinstance(result, list)
assert [1, 2, 3] == sorted(result)
def test_dumps_decimal():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert b'"1.5"' == ext_json.json.dumps(decimal.Decimal('1.5'))
project: added all source files and assets
r1
def test_dumps_complex():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert b"[0.0,1.0]" == ext_json.json.dumps(1j)
assert b"[1.0,0.0]" == ext_json.json.dumps(1 + 0j)
assert b"[1.1,1.2]" == ext_json.json.dumps(1.1 + 1.2j)
project: added all source files and assets
r1
def test_dumps_object_with_json_method():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert '"foo"' == ext_json.str_json(SerializableObject())
project: added all source files and assets
r1
def test_dumps_object_with_json_attribute():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert '"foo"' == ext_json.str_json(SerializableObject())
project: added all source files and assets
r1
def test_dumps_time():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert '"03:14:15.926535"' == ext_json.str_json(datetime.time(3, 14, 15, 926535))
project: added all source files and assets
r1
def test_dumps_time_no_microseconds():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert '"03:14:15"' == ext_json.str_json(datetime.time(3, 14, 15))
project: added all source files and assets
r1
def test_dumps_time_with_timezone():
with pytest.raises(TypeError) as excinfo:
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 ext_json.json.dumps(datetime.time(3, 14, 15, 926535, Timezone(0)))
project: added all source files and assets
r1
error_msg = str(excinfo.value)
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012
assert 'timezone library is not supported' in error_msg
# only for simplejson
#assert 'Time-zone aware times are not JSON serializable' in error_msg
project: added all source files and assets
r1
def test_dumps_date():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert b'"1969-07-20"' == ext_json.json.dumps(datetime.date(1969, 7, 20))
project: added all source files and assets
r1
def test_dumps_datetime():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 json_data = ext_json.json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15, 926535))
assert b'"1969-07-20T03:14:15.926535"' == json_data
project: added all source files and assets
r1
def test_dumps_datetime_no_microseconds():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 json_data = ext_json.json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15))
assert b'"1969-07-20T03:14:15"' == json_data
project: added all source files and assets
r1
def test_dumps_datetime_with_utc_timezone():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 json_data = ext_json.json.dumps(
project: added all source files and assets
r1 datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(0)))
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert b'"1969-07-20T03:14:15.926535+00:00"' == json_data
project: added all source files and assets
r1
def test_dumps_datetime_with_plus1_timezone():
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 json_data = ext_json.json.dumps(
project: added all source files and assets
r1 datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(1)))
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert b'"1969-07-20T03:14:15.926535+01:00"' == json_data
project: added all source files and assets
r1
def test_dumps_unserializable_class():
unserializable_obj = object()
with pytest.raises(TypeError) as excinfo:
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 ext_json.json.dumps(unserializable_obj)
project: added all source files and assets
r1
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert 'object' in str(excinfo.value)
project: added all source files and assets
r1 assert 'is not JSON serializable' in str(excinfo.value)
def test_dump_is_like_dumps():
data = {
'decimal': decimal.Decimal('1.5'),
'set': set([1]), # Just one element to guarantee the order
'complex': 1 - 1j,
'datetime': datetime.datetime(1969, 7, 20, 3, 14, 15, 926535),
'time': datetime.time(3, 14, 15, 926535),
'date': datetime.date(1969, 7, 20),
}
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 json_buffer = io.StringIO() # StringIO because dump uses simplejson not orjson
ext_json.json.dump(data, json_buffer)
project: added all source files and assets
r1
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert ext_json.sjson.dumps(data) == json_buffer.getvalue()
project: added all source files and assets
r1
def test_formatted_json():
data = {
'b': {'2': 2, '1': 1},
'a': {'3': 3, '4': 4},
}
expected_data = textwrap.dedent('''
{
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 "a": {
"3": 3,
"4": 4
},
"b": {
"1": 1,
"2": 2
}
project: added all source files and assets
r1 }''').strip()
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert expected_data == ext_json.formatted_str_json(data)
project: added all source files and assets
r1
pylons: fixed code and test suite after removal of pylons.
r2358 def test_lazy_translation_string(baseapp):
project: added all source files and assets
r1 data = {'label': _('hello')}
pylons: remove pylons as dependency...
r2351 data2 = {'label2': _pluralize('singular', 'plural', 1)}
project: added all source files and assets
r1
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 assert b'{"label":"hello"}' == ext_json.json.dumps(data)
assert b'{"label2":"singular"}' == ext_json.json.dumps(data2)
slack: fixed celery serialization breaking slack_data dataclass
r5169
def test_serialize_dataclass():
@dataclasses.dataclass
class ExampleStruct:
field_str: str
field_int: int
struct = ExampleStruct(field_int=1, field_str='hello')
raw_struct = b'{"field_str":"hello","field_int":1}'
assert raw_struct == ext_json.json.dumps(struct)
assert struct == ExampleStruct(**ext_json.json.loads(raw_struct))