예제 #1
0
    def test_class_with_missing_attribute(self):
        @pyckson
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        with self.assertRaises(ValueError):
            serialize(Foo(None))
예제 #2
0
    def test_class_with_missing_attribute(self):
        @pyckson
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        with self.assertRaises(ValueError):
            serialize(Foo(None))
예제 #3
0
def test_should_serialize_decimal():
    class Foo:
        def __init__(self, x: Decimal):
            self.x = x

    pi = '3.141592653589793238462643383279502884197'
    assert serialize(Foo(Decimal(pi))) == {'x': pi}
예제 #4
0
    def test_without_annotation(self):
        class Foo:
            def __init__(self, bar: List[str]):
                self.bar = bar

        result = serialize(Foo(['a', 'b']))

        self.assertEqual(result, {'bar': ['a', 'b']})
예제 #5
0
    def test_serialize_old_dict(self):
        class Foo:
            def __init__(self, foo: dict):
                self.foo = foo

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

        self.assertEqual(result, {'foo': {'a': 1, 'b': 2}})
예제 #6
0
    def test_class_with_optional_param_type(self):
        class Foo:
            def __init__(self, bar: Optional[str]):
                self.bar = bar

        result = serialize(Foo('a'))

        self.assertEqual(result, {'bar': 'a'})
예제 #7
0
    def test_serialize_double_list(self):
        class Foo:
            def __init__(self, bar: List[List[str]]):
                self.bar = bar

        result = serialize(Foo([['a', 'b'], ['c']]))

        self.assertEqual(result, {'bar': [['a', 'b'], ['c']]})
예제 #8
0
    def test_class_with_generic_list(self):
        @pyckson
        class Foo:
            def __init__(self, bar: List[str]):
                self.bar = bar

        result = serialize(Foo(['a', 'b']))
        self.assertEqual(result, {'bar': ['a', 'b']})
예제 #9
0
    def test_should_be_able_to_serialize_lists(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

        result = serialize([Foo(1), Foo(2)])

        self.assertEqual(result, [{'a': 1}, {'a': 2}])
예제 #10
0
    def test_without_annotation(self):
        class Foo:
            def __init__(self, bar: List[str]):
                self.bar = bar

        result = serialize(Foo(['a', 'b']))

        self.assertEqual(result, {'bar': ['a', 'b']})
예제 #11
0
    def test_class_with_optional_param_type_absent(self):
        class Foo:
            def __init__(self, bar: Optional[str]):
                self.bar = bar

        result = serialize(Foo(None))

        self.assertEqual(result, {})
예제 #12
0
    def test_class_with_generic_list(self):
        @pyckson
        class Foo:
            def __init__(self, bar: List[str]):
                self.bar = bar

        result = serialize(Foo(['a', 'b']))
        self.assertEqual(result, {'bar': ['a', 'b']})
예제 #13
0
    def test_simple_class(self):
        @pyckson
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        result = serialize(Foo('bar'))

        self.assertEqual(result, {'bar': 'bar'})
예제 #14
0
    def test_no_camel_case(self):
        @pyckson
        @no_camel_case
        class Foo:
            def __init__(self, foo_bar: str):
                self.foo_bar = foo_bar

        foo = Foo('foo')
        self.assertEqual(serialize(foo), {'foo_bar': 'foo'})
예제 #15
0
    def test_simple_class(self):
        @pyckson
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        result = serialize(Foo('bar'))

        self.assertEqual(result, {'bar': 'bar'})
예제 #16
0
    def test_no_camel_case(self):
        @pyckson
        @no_camel_case
        class Foo:
            def __init__(self, foo_bar: str):
                self.foo_bar = foo_bar

        foo = Foo("foo")
        self.assertEqual(serialize(foo), {"foo_bar": "foo"})
예제 #17
0
    def test_serialize_set_as_list(self):
        class Foo:
            def __init__(self, x: Set[int]):
                self.x = x

        result = serialize(Foo({1, 2}))

        # because of unknown ordering
        self.assertSetEqual(set(result['x']), {1, 2})
예제 #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 = serialize(Foo(date))

        self.assertEqual(result, {'x': date})
예제 #19
0
    def test_with_bytes(self):
        @pyckson
        class Foo:
            def __init__(self, x: bytes):
                self.x = x

        data = b"bar"
        result = serialize(Foo(data))

        self.assertEqual(result, {'x': data})
예제 #20
0
    def test_class_with_list(self):
        @pyckson
        @listtype('bar', str)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = serialize(Foo(['a', 'b']))

        self.assertEqual(result, {'bar': ['a', 'b']})
예제 #21
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 = serialize(Foo(42))

        self.assertEqual(result, {'a': 42})
예제 #22
0
    def test_should_serialize_empty_mandatory_list(self):
        @pyckson
        @listtype('bar', str)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = serialize(Foo([]))

        self.assertEqual(result, {'bar': []})
예제 #23
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 = serialize(Foo(date))

        self.assertEqual(result, {'x': date})
예제 #24
0
    def test_with_bytes(self):
        @pyckson
        class Foo:
            def __init__(self, x: bytes):
                self.x = x

        data = b"bar"
        result = serialize(Foo(data))

        self.assertEqual(result, {'x': data})
예제 #25
0
    def test_class_with_list(self):
        @pyckson
        @listtype('bar', str)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = serialize(Foo(['a', 'b']))

        self.assertEqual(result, {'bar': ['a', 'b']})
예제 #26
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 = serialize(Foo(42))

        self.assertEqual(result, {'a': 42})
예제 #27
0
    def test_should_serialize_empty_mandatory_list(self):
        @pyckson
        @listtype('bar', str)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = serialize(Foo([]))

        self.assertEqual(result, {'bar': []})
예제 #28
0
    def test_rename_and_no_camel_case(self):
        @pyckson
        @rename(foo='_foo')
        @no_camel_case
        class Foo:
            def __init__(self, foo: str, bar_bar: str):
                self.foo = foo
                self.bar_bar = bar_bar

        foo = Foo('foo', 'bar')
        self.assertEqual(serialize(foo), {'_foo': 'foo', 'bar_bar': 'bar'})
예제 #29
0
    def test_use_explicit_nulls(self):
        @pyckson
        @explicit_nulls
        class Foo:
            def __init__(self, a: str = None):
                self.a = a

        result = serialize(Foo(a=None))

        self.assertEqual(result, {'a': None})
        self.assertEqual(dumps(result), '{"a": null}')
예제 #30
0
    def test_rename_and_no_camel_case(self):
        @pyckson
        @rename(foo="_foo")
        @no_camel_case
        class Foo:
            def __init__(self, foo: str, bar_bar: str):
                self.foo = foo
                self.bar_bar = bar_bar

        foo = Foo("foo", "bar")
        self.assertEqual(serialize(foo), {"_foo": "foo", "bar_bar": "bar"})
예제 #31
0
    def test_class_with_legacy_set(self):
        @pyckson
        @settype('bar', str)
        class Foo:
            def __init__(self, bar: set):
                self.bar = bar

        result = serialize(Foo({'a', 'b'}))

        # because of unknown ordering
        self.assertSetEqual(set(result['bar']), {'a', 'b'})
예제 #32
0
    def test_with_unicode(self):
        class FakeString(str):
            pass

        @pyckson
        class Foo:
            def __init__(self, x: str):
                self.x = x

        result = serialize(Foo(FakeString('foo')))

        self.assertEqual(result, {'x': 'foo'})
예제 #33
0
    def test_should_be_able_to_serialize_typing_dicts(self):
        class Foo:
            def __init__(self, a: int):
                self.a = a

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

        result = serialize(Bar({'1': Foo(1), '2': Foo(2)}))

        self.assertEqual(result, {'b': {'1': {'a': 1}, '2': {'a': 2}}})
예제 #34
0
    def test_with_unicode(self):
        class FakeString(str):
            pass

        @pyckson
        class Foo:
            def __init__(self, x: str):
                self.x = x

        result = serialize(Foo(FakeString('foo')))

        self.assertEqual(result, {'x': 'foo'})
예제 #35
0
    def test_custom_name_rule(self):
        def my_rule(python_name):
            return 'foo' + python_name

        @pyckson
        @namerule(my_rule)
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        foo = Foo('foo')
        self.assertEqual(serialize(foo), {'foobar': 'foo'})
예제 #36
0
    def test_custom_name_rule(self):
        def my_rule(python_name):
            return "foo" + python_name

        @pyckson
        @namerule(my_rule)
        class Foo:
            def __init__(self, bar: str):
                self.bar = bar

        foo = Foo("foo")
        self.assertEqual(serialize(foo), {"foobar": "foo"})
예제 #37
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 = serialize(Bar(Foo("foo")))

        self.assertEqual(result, {'aFoo': {'arg1': 'foo'}})
예제 #38
0
    def test_rename_rule(self):
        @pyckson
        @rename(foo="_foo", bar="bar_")
        class Foo:
            def __init__(self, foo: str, bar: str):
                self.foo = foo
                self.bar = bar

        foo = Foo("foo", "bar")
        self.assertEqual(serialize(foo), {"_foo": "foo", "bar_": "bar"})
        foo = parse(Foo, {"_foo": "foo", "bar_": "bar"})
        self.assertEqual(foo.foo, "foo")
        self.assertEqual(foo.bar, "bar")
예제 #39
0
    def test_rename_rule(self):
        @pyckson
        @rename(foo='_foo', bar='bar_')
        class Foo:
            def __init__(self, foo: str, bar: str):
                self.foo = foo
                self.bar = bar

        foo = Foo('foo', 'bar')
        self.assertEqual(serialize(foo), {'_foo': 'foo', 'bar_': 'bar'})
        foo = parse(Foo, {'_foo': 'foo', 'bar_': 'bar'})
        self.assertEqual(foo.foo, 'foo')
        self.assertEqual(foo.bar, 'bar')
예제 #40
0
    def test_class_with_generic_object_list(self):
        @pyckson
        class Bar:
            def __init__(self, x: str):
                self.x = x

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

        result = serialize(Foo([Bar('a'), Bar('b')]))

        self.assertEqual(result, {'bar': [{'x': 'a'}, {'x': 'b'}]})
예제 #41
0
    def test_should_be_able_to_serialize_union_type(self):
        class X:
            def __init__(self, x: str):
                self.x = x

        class Y:
            def __init__(self, y: str):
                self.y = y

        class Foo:
            def __init__(self, foo: Union[X, Y]):
                self.foo = foo

        assert serialize(Foo(X('a'))) == {'foo': {'x': 'a'}}
예제 #42
0
    def test_custom_serializer(self):
        class Foo:
            def __init__(self, bar):
                self.bar = bar

        class FooSerializer(Serializer):
            def serialize(self, obj: Foo) -> dict:
                return {'toto': obj.bar}

        custom_serializer(FooSerializer)(Foo)

        result = serialize(Foo(42))

        self.assertEqual(result, {'toto': 42})
예제 #43
0
    def test_class_with_generic_object_list(self):
        @pyckson
        class Bar:
            def __init__(self, x: str):
                self.x = x

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

        result = serialize(Foo([Bar('a'), Bar('b')]))

        self.assertEqual(result, {'bar': [{'x': 'a'}, {'x': 'b'}]})
예제 #44
0
    def test_should_serialize_list_with_unresolved_type(self):
        @pyckson
        class Bar:
            def __init__(self, x: str):
                self.x = x

        @pyckson
        @listtype('bar', Bar)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = serialize(Foo([Bar('y')]))

        self.assertEqual(result, {'bar': [{'x': 'y'}]})
예제 #45
0
    def test_should_serialize_list_with_unresolved_type(self):
        @pyckson
        class Bar:
            def __init__(self, x: str):
                self.x = x

        @pyckson
        @listtype('bar', Bar)
        class Foo:
            def __init__(self, bar: list):
                self.bar = bar

        result = serialize(Foo([Bar('y')]))

        self.assertEqual(result, {'bar': [{'x': 'y'}]})
예제 #46
0
    def test_custom_serializer_on_param(self):
        class Bar:
            def __init__(self):
                pass

        class BarSerializer(Serializer):
            def serialize(self, obj: Bar):
                return 42

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

        custom_serializer(BarSerializer)(Bar)

        result = serialize(Foo(Bar()))

        self.assertEqual(result, {'bar': 42})