Exemplo n.º 1
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)
Exemplo n.º 2
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)
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 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')
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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')
Exemplo n.º 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')
Exemplo n.º 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)
Exemplo n.º 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')
Exemplo n.º 12
0
 def set_model(self, cls):
     set_cls_attr(cls, PYCKSON_ATTR, True)
     model = PycksonModelBuilder(cls, SerializerProviderImpl(self),
                                 ParserProviderImpl(self)).build_model()
     set_cls_attr(cls, PYCKSON_MODEL, model)