Exemple #1
0
    def test_should_refuse_unspecified_list(self):
        parameter = Parameter('foo', Parameter.POSITIONAL_OR_KEYWORD, annotation=list)

        with self.assertRaises(TypeError):
            PycksonModelBuilder(DummyClass,
                                SerializerProviderImpl(ModelProviderImpl()),
                                ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)
Exemple #2
0
    def test_should_not_accept_positional_only_parameter(self):
        parameter = Parameter('foo', Parameter.POSITIONAL_ONLY, annotation=int)

        with self.assertRaises(TypeError):
            PycksonModelBuilder(DummyClass,
                                SerializerProviderImpl(ModelProviderImpl()),
                                ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)
Exemple #3
0
    def test_should_not_accept_var_keyword_parameter(self):
        parameter = Parameter('foo', Parameter.VAR_KEYWORD, annotation=int)

        with self.assertRaises(TypeError):
            PycksonModelBuilder(DummyClass,
                                SerializerProviderImpl(ModelProviderImpl()),
                                ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)
Exemple #4
0
    def test_should_not_accept_untyped_parameter(self):
        parameter = Parameter('foo', Parameter.POSITIONAL_OR_KEYWORD)

        with self.assertRaises(TypeError):
            PycksonModelBuilder(DummyClass,
                                SerializerProviderImpl(ModelProviderImpl()),
                                ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)
Exemple #5
0
    def test_should_camel_case_name(self):
        parameter = Parameter('foo_bar', Parameter.POSITIONAL_OR_KEYWORD, annotation=int)

        attribute = PycksonModelBuilder(DummyClass,
                                        SerializerProviderImpl(ModelProviderImpl()),
                                        ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)

        self.assertEqual(attribute.python_name, 'foo_bar')
        self.assertEqual(attribute.json_name, 'fooBar')
Exemple #6
0
    def test_should_build_optional_parameter(self):
        parameter = Parameter('foo', Parameter.POSITIONAL_OR_KEYWORD, annotation=int, default=None)

        attribute = PycksonModelBuilder(DummyClass,
                                        SerializerProviderImpl(ModelProviderImpl()),
                                        ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)

        self.assertEqual(attribute.python_name, 'foo')
        self.assertEqual(attribute.attr_type, int)
        self.assertEqual(attribute.optional, True)
Exemple #7
0
    def test_optional_type(self):
        class Foo:
            def __init__(self, bar: Optional[str]):
                pass

        model = PycksonModelBuilder(Foo,
                                    SerializerProviderImpl(ModelProviderImpl()),
                                    ParserProviderImpl(ModelProviderImpl())).build_model()

        self.assertTrue(model.get_attribute(python_name='bar').optional)
        self.assertEqual(model.get_attribute(python_name='bar').attr_type, str)
Exemple #8
0
    def test_should_parse_basic_signature(self):
        class Foo:
            def __init__(self, foo: int, bar: str):
                pass

        model = PycksonModelBuilder(Foo,
                                    SerializerProviderImpl(ModelProviderImpl()),
                                    ParserProviderImpl(ModelProviderImpl())).build_model()

        self.assertEqual(model.get_attribute(python_name='foo').python_name, 'foo')
        self.assertEqual(model.get_attribute(python_name='bar').python_name, 'bar')
Exemple #9
0
    def test_should_ignore_self_parameter(self):
        class Foo:
            def __init__(self, foo: int):
                pass

        model = PycksonModelBuilder(Foo,
                                    SerializerProviderImpl(ModelProviderImpl()),
                                    ParserProviderImpl(ModelProviderImpl())).build_model()

        self.assertEqual(model.get_attribute(python_name='foo').python_name, 'foo')
        with self.assertRaises(KeyError):
            model.get_attribute(python_name='self')
Exemple #10
0
    def test_should_accept_specified_list(self):
        @listtype('foo', int)
        class ListDummyClass:
            pass

        parameter = Parameter('foo', Parameter.POSITIONAL_OR_KEYWORD, annotation=list)

        attribute = PycksonModelBuilder(ListDummyClass,
                                        SerializerProviderImpl(ModelProviderImpl()),
                                        ParserProviderImpl(ModelProviderImpl())).build_attribute(parameter)

        self.assertEqual(type(attribute.parser), ListParser)
        self.assertEqual(type(attribute.parser.sub_parser), BasicParserWithCast)
Exemple #11
0
    def test_should_use_typeinfo_for_lists(self):
        @listtype('foo', int)
        @listtype('bar', int)
        class Foo:
            def __init__(self, foo: list, bar: list):
                pass

        model = PycksonModelBuilder(Foo,
                                    SerializerProviderImpl(ModelProviderImpl()),
                                    ParserProviderImpl(ModelProviderImpl())).build_model()

        self.assertEqual(model.get_attribute(python_name='foo').python_name, 'foo')
        with self.assertRaises(KeyError):
            model.get_attribute(python_name='self')
Exemple #12
0
def serialize(obj):
    """Takes a object and produces a dict-like representation

    :param obj: the object to serialize
    """
    if isinstance(obj, list):
        return [serialize(o) for o in obj]
    return GenericSerializer(ModelProviderImpl()).serialize(obj)
Exemple #13
0
def parse(cls, value):
    """Takes a class and a dict and try to build an instance of the class

    :param cls: The class to parse
    :param value: either a dict, a list or a scalar value
    """
    if is_list_annotation(cls):
        if not isinstance(value, list):
            raise TypeError(
                'Could not parse {} because value is not a list'.format(cls))
        return [parse(cls.__args__[0], o) for o in value]
    else:
        return GenericParser(cls, ModelProviderImpl()).parse(value)
Exemple #14
0
def pyckson(cls):
    ModelProviderImpl().get_or_build(cls)
    setattr(cls, 'parse', lambda json: parse(cls, json))
    return cls