Beispiel #1
0
    def test_should_expect_a_list_when_parsing_as_list(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

        with self.assertRaises(TypeError):
            parse(List[Foo], {'a': 1})
Beispiel #2
0
    def test_basic_parser_should_fail_on_uncastable_values(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

        with self.assertRaises(ValueError):
            parse(Foo, {'a': 'b'})
Beispiel #3
0
    def test_class_with_missing_attribute(self):
        @pyckson
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        with self.assertRaises(ValueError):
            parse(Foo, {})
Beispiel #4
0
    def test_should_not_allow_dict_of_not_str(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

        class Bar:
            def __init__(self, b: Dict[int, Foo]):
                self.b = b

        with self.assertRaises(TypeError):
            parse(Bar, {'b': {'x': {'a': 1}}})
Beispiel #5
0
def test_should_parse_decimal():
    class Foo:
        def __init__(self, x: Decimal):
            self.x = x

    result = parse(Foo, {'x': '3.141592653589793238462643383279502884197'})
    assert result.x == Decimal('3.141592653589793238462643383279502884197')
Beispiel #6
0
    def test_parse_absent_optional_type(self):
        class Foo:
            def __init__(self, bar: Optional[str] = 'b'):
                self.bar = bar

        result = parse(Foo, {})

        self.assertEqual(result.bar, 'b')
Beispiel #7
0
    def test_without_annotation(self):
        class Foo:
            def __init__(self, bar: List[str]):
                self.bar = bar

        result = parse(Foo, {'bar': ['a', 'b']})

        self.assertListEqual(result.bar, ['a', 'b'])
Beispiel #8
0
    def test_parse_set_as_list(self):
        class Foo:
            def __init__(self, x: Set[int]):
                self.x = x

        result = parse(Foo, {'x': [1, 2, 3]})

        self.assertEqual(result.x, {1, 2, 3})
Beispiel #9
0
    def test_parse_double_list(self):
        class Foo:
            def __init__(self, bar: List[List[str]]):
                self.bar = bar

        result = parse(Foo, {'bar': [['a', 'b'], ['c']]})

        self.assertListEqual(result.bar, [['a', 'b'], ['c']])
Beispiel #10
0
    def test_class_with_generic_list(self):
        @pyckson
        class Foo:
            def __init__(self, bar: List[str]):
                self.bar = bar

        result = parse(Foo, {'bar': ['a', 'b']})
        self.assertListEqual(result.bar, ['a', 'b'])
Beispiel #11
0
    def test_parsing_optional_without_default_should_set_as_none(self):
        class Foo:
            def __init__(self, bar: Optional[str]):
                self.bar = bar

        result = parse(Foo, {})

        self.assertEqual(result.bar, None)
Beispiel #12
0
    def test_simple_class(self):
        @pyckson
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        result = parse(Foo, {'bar': 'bar'})

        self.assertEqual(result.bar, 'bar')
Beispiel #13
0
    def test_parse_old_dict(self):
        class Foo:
            def __init__(self, foo: dict):
                self.foo = foo

        result = parse(Foo, {'foo': {'a': 1, 'b': 2}})

        self.assertEqual(result.foo['a'], 1)
        self.assertEqual(result.foo['b'], 2)
Beispiel #14
0
    def test_class_with_list(self):
        @pyckson
        @listtype('bar', str)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = parse(Foo, {'bar': ['a', 'b']})

        self.assertListEqual(result.bar, ['a', 'b'])
Beispiel #15
0
    def test_class_with_legacy_set(self):
        @pyckson
        @settype('bar', str)
        class Foo:
            def __init__(self, bar: set):
                self.bar = bar

        result = parse(Foo, {'bar': ['a', 'b']})

        self.assertEqual(result.bar, {'a', 'b'})
Beispiel #16
0
    def test_should_parse_list_type_as_a_list(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

        result = parse(List[Foo], [{'a': 1}, {'a': 2}])

        self.assertEqual(len(result), 2)
        self.assertEqual(result[0].a, 1)
        self.assertEqual(result[1].a, 2)
Beispiel #17
0
    def test_with_bytes(self):
        @pyckson
        class Foo:
            def __init__(self, x: bytes):
                self.x = x

        data = b"bar"
        result = parse(Foo, {'x': data})

        self.assertEqual(result.x, data)
Beispiel #18
0
    def test_with_date(self):
        @pyckson
        class Foo:
            def __init__(self, x: datetime):
                self.x = x

        date = datetime(2016, 2, 18, 10, 59, 0)
        result = parse(Foo, {'x': date})

        self.assertEqual(result.x, date)
Beispiel #19
0
    def test_should_parse_optional_that_is_null(self):
        class Foo:
            def __init__(self, x: int):
                self.x = x

        class Bar:
            def __init__(self, y: Optional[Foo] = None):
                self.y = y

        result = parse(Bar, {'y': None})
        self.assertEqual(result.y, None)
Beispiel #20
0
    def test_class_with_optional_attribute(self):
        @pyckson
        class Foo:
            def __init__(self, a: int, b: str = None):
                self.a = a
                self.b = b

        result = parse(Foo, {'a': 42})

        self.assertEqual(result.a, 42)
        self.assertIsNone(result.b)
Beispiel #21
0
    def test_class_with_optional_object_param_type(self):
        class Foo:
            def __init__(self, arg1: str):
                self.arg1 = arg1

        class Bar:
            def __init__(self, a_foo: Optional[Foo]):
                self.a_foo = a_foo

        result = parse(Bar, {'aFoo': {'arg1': 'foo'}})

        self.assertEqual(result.a_foo.arg1, 'foo')
Beispiel #22
0
    def test_should_be_able_to_parse_dict_of_objects(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

        class Bar:
            def __init__(self, b: Dict[str, Foo]):
                self.b = b

        result = parse(Bar, {'b': {'x': {'a': 1}}})

        self.assertEqual(result.b['x'].a, 1)
        self.assertEqual(type(result.b['x']), Foo)
Beispiel #23
0
    def test_basic_parser_should_try_to_convert_types(self):
        class Foo:
            def __init__(self, a: int, b: str, c: float, e: float):
                self.a = a
                self.b = b
                self.c = c
                self.e = e

        result = parse(Foo, {'a': '42', 'b': 1, 'c': 5, 'e': '1.5'})
        self.assertEqual(result.a, 42)
        self.assertEqual(result.b, '1')
        self.assertEqual(result.c, 5)
        self.assertEqual(result.e, 1.5)
Beispiel #24
0
    def test_parse_with_insensitive_enum(self):
        @caseinsensitive
        class Foo(Enum):
            a = 1

        @pyckson
        class Bar:
            def __init__(self, foo: Foo):
                self.foo = foo

        result = parse(Bar, {'foo': 'A'})

        self.assertEqual(result.foo, Foo.a)
Beispiel #25
0
    def test_custom_parser(self):
        class Foo:
            def __init__(self, bar):
                self.bar = bar

        class FooParser(Parser):
            def parse(self, json_value) -> Foo:
                return Foo(json_value['x'])

        custom_parser(FooParser)(Foo)

        result = parse(Foo, {'x': 42})

        self.assertEqual(result.bar, 42)
Beispiel #26
0
    def test_class_with_generic_object_list(self):
        @pyckson
        class Bar:
            def __init__(self, x: str):
                self.x = x

            def __eq__(self, other):
                return self.x == other.x

        @pyckson
        class Foo:
            def __init__(self, bar: List[Bar]):
                self.bar = bar

        result = parse(Foo, {'bar': [{'x': 'a'}, {'x': 'b'}]})
        self.assertListEqual(result.bar, [Bar('a'), Bar('b')])
Beispiel #27
0
    def test_custom_parser_on_param(self):
        class Bar:
            def __init__(self, x):
                self.x = x

        class BarParser(Parser):
            def parse(self, json_value) -> Bar:
                return Bar(42)

        class Foo:
            def __init__(self, bar: Bar):
                self.bar = bar

        custom_parser(BarParser)(Bar)

        result = parse(Foo, {'bar': {}})

        self.assertEqual(result.bar.x, 42)
Beispiel #28
0
def pyckson(cls):
    ModelProviderImpl().get_or_build(cls)
    setattr(cls, 'parse', lambda json: parse(cls, json))
    return cls
Beispiel #29
0
def load(cls, fp, **kwargs):
    """wrapper for :py:func:`json.load`"""
    json_obj = json.load(fp, **kwargs)
    return parse(cls, json_obj)
Beispiel #30
0
def loads(cls, s, **kwargs):
    """wrapper for :py:func:`json.loads`"""
    json_obj = json.loads(s, **kwargs)
    return parse(cls, json_obj)
Beispiel #31
0
def pyckson(cls):
    ModelProviderImpl().get_or_build(cls)
    setattr(cls, 'parse', lambda json: parse(cls, json))
    return cls