Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_load_object_with_default_value(self):
class A:
def __init__(self, x, y = 2):
self.x = x
self.y = y
a = A(1)
loaded_a = jsons.load({'x': 1}, A)
self.assertEqual(a.x, loaded_a.x)
self.assertEqual(a.y, loaded_a.y)
def test_validation_valid(self):
jsons.set_validator(lambda c: c.x > 5, C)
jsons.load({'x': 6}, C) # Should be fine.
def test_load_float(self):
self.assertEqual(123.456, jsons.load(123.456))
def test_load_namedtuple(self):
dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
tzinfo=datetime.timezone.utc)
T = NamedTuple('T', [('x', str), ('y', datetime.datetime)])
t = T('test', dat)
loaded = jsons.load(['test', '2018-07-08T21:34:00Z'], T)
self.assertEqual(t, loaded)
T._field_defaults = dict(y=dat)
loaded2 = jsons.load(['test'], T)
self.assertEqual(t, loaded)
with self.assertRaises(UnfulfilledArgumentError):
jsons.load([], T)
try:
jsons.load([], T)
except UnfulfilledArgumentError as err:
self.assertEqual([], err.source)
self.assertEqual(T, err.target)
self.assertEqual('x', err.argument)
def test_load_complex_number(self):
dumped = {'real': 1.0, 'imag': 2.0}
loaded = jsons.load(dumped, complex)
self.assertEqual(loaded, 1+2j)
bad_keys_dump = {'some_key': 3.0}
with self.assertRaises(DeserializationError):
jsons.load(bad_keys_dump, complex)
bad_types_dump = {'real': 'some_string', 'imag': {'a': 2}}
with self.assertRaises(DeserializationError):
jsons.load(bad_types_dump, complex)
def test_uuid_serialization(self):
from version_with_dataclasses import User
user = User(uuid.uuid4(), 'name')
dumped = jsons.dump(user)
self.assertEqual(dumped['user_uuid'], str(user.user_uuid))
loaded = jsons.load(dumped, User)
self.assertEqual(user.user_uuid, loaded.user_uuid)
self.assertEqual('name', loaded.name)
def test_load_list_multithreaded(self):
dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
tzinfo=datetime.timezone.utc)
list_ = [1, 2, 3, [4, 5, [dat]]]
expectation = [1, 2, 3, [4, 5, ['2018-07-08T21:34:00Z']]]
self.assertEqual(list_, jsons.load(expectation, tasks=3,
task_type=Thread))
with self.assertRaises(JsonsError):
jsons.load(expectation, tasks=-1,
task_type=Thread)
self.assertEqual([1], jsons.load(['1'], List[int], tasks=2,
task_type=Thread))
# More tasks than elements should still work.
self.assertEqual([1, 1, 1, 1], jsons.load(['1', '1', '1', '1'],
List[int], tasks=16,
task_type=Thread))
def test_exception_unfulfilled_arg(self):
class C:
def __init__(self, x: int, y: int):
self.x = x
self.y = y
with self.assertRaises(UnfulfilledArgumentError):
jsons.load({"x": 1}, C)
try:
jsons.load({"x": 1}, C)
except UnfulfilledArgumentError as err:
self.assertDictEqual({"x": 1}, err.source)
self.assertEqual(C, err.target)
self.assertEqual('y', err.argument)
def test_validate_primitive_attribute_invalid(self):
jsons.set_validator(lambda x: x == 1, int)
with self.assertRaises(ValidationError):
jsons.load({'x': 3}, C)
def test_load_namedtuple(self):
dat = datetime.datetime(year=2018, month=7, day=8, hour=21, minute=34,
tzinfo=datetime.timezone.utc)
T = NamedTuple('T', [('x', str), ('y', datetime.datetime)])
t = T('test', dat)
loaded = jsons.load(['test', '2018-07-08T21:34:00Z'], T)
self.assertEqual(t, loaded)
T._field_defaults = dict(y=dat)
loaded2 = jsons.load(['test'], T)
self.assertEqual(t, loaded)
with self.assertRaises(UnfulfilledArgumentError):
jsons.load([], T)
try:
jsons.load([], T)
except UnfulfilledArgumentError as err:
self.assertEqual([], err.source)
self.assertEqual(T, err.target)
self.assertEqual('x', err.argument)