def test_namedArguments(self):
        resolvedArguments = [
            ResolvedArgument(
                'myNumber',
                PrimitiveArgument(123, 'myNumber'),
                InspectedArgument('myNumber', DType('builtins', 'int'))
            ),
            ResolvedArgument(
                'manuallyWiredService',
                ServiceArgument('my.module.ManuallyWiredClass', 'manuallyWiredService'),
                InspectedArgument('manuallyWiredService', DType('my.module.ManuallyWiredClass', 'ManuallyWiredClass'))
            ),
            ResolvedArgument(
                'autowiredService',
                None,
                InspectedArgument('autowiredService', DType('my.module.OtherClass', 'OtherClass')),
            )
        ]

        classes2Services = {
            'my.module.OtherClass': {'OtherClass': ['my.module.OtherClass']}
        }

        newResolvedArguments = self.__argumentsAutowirer.autowire(
            'my.module.MyClass',
            resolvedArguments,
            classes2Services,
        )

        self.assertEqual(3, len(newResolvedArguments))
        self.assertEqual(PrimitiveArgument(123, 'myNumber'), newResolvedArguments[0].argument)
        self.assertEqual(ServiceArgument('my.module.ManuallyWiredClass', 'manuallyWiredService'), newResolvedArguments[1].argument)
        self.assertEqual(ServiceArgument('my.module.OtherClass', 'autowiredService'), newResolvedArguments[2].argument)
    def parse(self, argument, name=None):
        if isinstance(argument, str):
            if argument[0:1] == '@':
                return ServiceArgument(argument[1:], name)

            return PrimitiveArgument(argument, name)

        if isinstance(argument, TaggedServices):
            return TaggedServicesArgument(argument.val, name)

        if isinstance(argument, TaggedAliasedService):
            return TaggedAliasedServiceArgument(argument.tagName,
                                                argument.tagAlias, name)

        if isinstance(argument, (int, bool)):
            return PrimitiveArgument(argument, name)

        if isinstance(argument, list):
            return ListArgument(list(map(self.parse, argument)), name)

        if isinstance(argument, dict):
            return DictArgument(
                {k: self.parse(v)
                 for k, v in argument.items()}, name)

        raise Exception('Unexpected argument type: {}'.format(type(argument)))
    def test_args(self):
        arguments = [
            PrimitiveArgument('George', 'name'),
        ]
        inspectedArguments = self.__inspectedArgumentsResolver.resolveConstructor(
            DType('injecta.mocks.Args', 'Args'))

        resolvedArguments = self.__namedArgumentsResolver.resolve(
            arguments, inspectedArguments, 'injecta.mocks.Args')

        self.assertEqual(1, len(resolvedArguments))
        self.assertEqual(PrimitiveArgument('George', 'name'),
                         resolvedArguments[0].argument)
Exemple #4
0
    def test_args(self):
        arguments = [
            PrimitiveArgument("George", "name"),
        ]
        inspected_arguments = self.__inspected_arguments_resolver.resolve_constructor(
            DType("injecta.mocks.Args", "Args"))

        resolved_arguments = self.__named_arguments_resolver.resolve(
            arguments, inspected_arguments, "injecta.mocks.Args")

        self.assertEqual(1, len(resolved_arguments))
        self.assertEqual(PrimitiveArgument("George", "name"),
                         resolved_arguments[0].argument)
Exemple #5
0
    def test_multiline_script(self):
        d1 = Service("foo.Bar", DType("foo.Bar", "Bar"),
                     [PrimitiveArgument("ahoj"),
                      PrimitiveArgument(52)], ["console.command"])
        d2 = Service("hello.World_service", DType("hello.World", "World"), [],
                     ["my.tag"])
        d3 = Service("my.Name", DType("my.Name", "Name"), [],
                     ["console.command"])

        result = self.__tag2_services_preparer.prepare([d1, d2, d3])

        expected = {"console.command": [d1, d3], "my.tag": [d2]}

        self.assertEqual(expected, result)
    def test_multilineScript(self):
        d1 = Service('foo.Bar', DType('foo.Bar', 'Bar'),
                     [PrimitiveArgument('ahoj'),
                      PrimitiveArgument(52)], ['console.command'])
        d2 = Service('hello.World_service', DType('hello.World', 'World'), [],
                     ['my.tag'])
        d3 = Service('my.Name', DType('my.Name', 'Name'), [],
                     ['console.command'])

        result = self.__tag2ServicesPreparer.prepare([d1, d2, d3])

        expected = {'console.command': [d1, d3], 'my.tag': [d2]}

        self.assertEqual(expected, result)
    def test_no_change(self):
        resolved_argument = ResolvedArgument("my_number", PrimitiveArgument(123), InspectedArgument("my_number", DType("builtins", "int")))
        container_build = ContainerBuild({}, [], {}, {}, {})

        new_resolved_argument = self.__tagged_argument_resolver.resolve(resolved_argument, container_build)

        self.assertEqual(resolved_argument, new_resolved_argument)
    def test_basicWithDefaultValue(self):
        arguments = [
            PrimitiveArgument(111, 'numberWithDefault'),
            PrimitiveArgument('Peter', 'name'),
        ]
        inspectedArguments = self.__inspectedArgumentsResolver.resolveConstructor(
            DType('injecta.mocks.Bar', 'Bar'))

        resolvedArguments = self.__namedArgumentsResolver.resolve(
            arguments, inspectedArguments, 'injecta.mocks.Bar')

        self.assertEqual(2, len(resolvedArguments))
        self.assertEqual(PrimitiveArgument('Peter', 'name'),
                         resolvedArguments[0].argument)
        self.assertEqual(PrimitiveArgument(111, 'numberWithDefault'),
                         resolvedArguments[1].argument)
Exemple #9
0
    def test_basic_with_default_value(self):
        arguments = [
            PrimitiveArgument(111, "number_with_default"),
            PrimitiveArgument("Peter", "name"),
        ]
        inspected_arguments = self.__inspected_arguments_resolver.resolve_constructor(
            DType("injecta.mocks.Bar", "Bar"))

        resolved_arguments = self.__named_arguments_resolver.resolve(
            arguments, inspected_arguments, "injecta.mocks.Bar")

        self.assertEqual(2, len(resolved_arguments))
        self.assertEqual(PrimitiveArgument("Peter", "name"),
                         resolved_arguments[0].argument)
        self.assertEqual(PrimitiveArgument(111, "number_with_default"),
                         resolved_arguments[1].argument)
    def test_basic(self):
        raw_services = {
            "injecta.mocks.Bar.Bar": {"arguments": ["Jiri Koutny"]},
            "injecta.mocks.Foo.Foo": {"arguments": ["@injecta.mocks.Bar.Bar"]},
            "injecta.mocks.Bar.BarAlias": "@injecta.mocks.Bar.Bar",
        }

        expected_service1 = Service(
            "injecta.mocks.Bar.Bar",
            DType("injecta.mocks.Bar", "Bar"),
            [
                PrimitiveArgument("Jiri Koutny"),
            ],
        )

        expected_service2 = Service(
            "injecta.mocks.Foo.Foo",
            DType("injecta.mocks.Foo", "Foo"),
            [
                ServiceArgument("injecta.mocks.Bar.Bar"),
            ],
        )

        expected_alias1 = ServiceAlias("injecta.mocks.Bar.BarAlias", "injecta.mocks.Bar.Bar")

        services, aliases = self.__services_preparer.prepare(raw_services)

        self.assertEqual(expected_service1, services[0])
        self.assertEqual(expected_service2, services[1])
        self.assertEqual(expected_alias1, aliases[0])
Exemple #11
0
    def test_basic(self):
        self.__arguments_validator.validate(
            "injecta.mocks.Bar",
            [ResolvedArgument("name", PrimitiveArgument("Jiri Koutny"), InspectedArgument("name", DType("builtins", "str")))],
            {},
            {},
        )

        self.assertTrue(True)
    def test_tooManyArguments(self):
        arguments = [
            PrimitiveArgument('Peter', 'name'),
            PrimitiveArgument(111, 'numberWithDefault'),
            PrimitiveArgument(True, 'boolWithDefault'),
            PrimitiveArgument(222, 'someNonexistentName'),
        ]
        inspectedArguments = self.__inspectedArgumentsResolver.resolveConstructor(
            DType('injecta.mocks.Bar', 'Bar'))

        with self.assertRaises(Exception) as error:
            self.__namedArgumentsResolver.resolve(arguments,
                                                  inspectedArguments,
                                                  'injecta.mocks.Bar')

        self.assertEqual(
            'Unknown argument "someNonexistentName" in service "injecta.mocks.Bar"',
            str(error.exception))
Exemple #13
0
    def test_too_many_arguments(self):
        arguments = [
            PrimitiveArgument("Peter", "name"),
            PrimitiveArgument(111, "number_with_default"),
            PrimitiveArgument(True, "bool_with_default"),
            PrimitiveArgument(222, "some_nonexistent_name"),
        ]
        inspected_arguments = self.__inspected_arguments_resolver.resolve_constructor(
            DType("injecta.mocks.Bar", "Bar"))

        with self.assertRaises(Exception) as error:
            self.__named_arguments_resolver.resolve(arguments,
                                                    inspected_arguments,
                                                    "injecta.mocks.Bar")

        self.assertEqual(
            'Unknown argument "some_nonexistent_name" in service "injecta.mocks.Bar"',
            str(error.exception))
    def test_named_arguments(self):
        resolved_arguments = [
            ResolvedArgument(
                "my_number", PrimitiveArgument(123, "my_number"),
                InspectedArgument("my_number", DType("builtins", "int"))),
            ResolvedArgument(
                "manually_wired_service",
                ServiceArgument("my.module.ManuallyWiredClass",
                                "manually_wired_service"),
                InspectedArgument(
                    "manually_wired_service",
                    DType("my.module.ManuallyWiredClass",
                          "ManuallyWiredClass")),
            ),
            ResolvedArgument(
                "autowired_service",
                None,
                InspectedArgument("autowired_service",
                                  DType("my.module.OtherClass", "OtherClass")),
            ),
        ]

        classes2_services = {
            "my.module.OtherClass": {
                "OtherClass": ["my.module.OtherClass"]
            }
        }

        new_resolved_arguments = self.__arguments_autowirer.autowire(
            "my.module.MyClass",
            resolved_arguments,
            classes2_services,
        )

        self.assertEqual(3, len(new_resolved_arguments))
        self.assertEqual(PrimitiveArgument(123, "my_number"),
                         new_resolved_arguments[0].argument)
        self.assertEqual(
            ServiceArgument("my.module.ManuallyWiredClass",
                            "manually_wired_service"),
            new_resolved_arguments[1].argument)
        self.assertEqual(
            ServiceArgument("my.module.OtherClass", "autowired_service"),
            new_resolved_arguments[2].argument)
    def test_noChange(self):
        resolvedArgument = ResolvedArgument(
            'myNumber', PrimitiveArgument(123),
            InspectedArgument('myNumber', DType('builtins', 'int')))
        containerBuild = ContainerBuild({}, [], {}, {}, {})

        newResolvedArgument = self.__taggedArgumentResolver.resolve(
            resolvedArgument, containerBuild)

        self.assertEqual(resolvedArgument, newResolvedArgument)
Exemple #16
0
    def test_exception_string_for_object(self):
        with self.assertRaises(Exception) as error:
            self.__arguments_validator.validate(
                "injecta.mocks.Foo",
                [ResolvedArgument("bar", PrimitiveArgument("Jiri Koutny"), InspectedArgument("bar", DType("injecta.mocks.Bar", "Bar")))],
                {},
                {},
            )

        self.assertEqual(
            'Expected dtype "injecta.mocks.Bar.Bar", got "str" (argument "bar", service "injecta.mocks.Foo")', str(error.exception)
        )
Exemple #17
0
    def test_basic(self):
        result = self.__service_parser.parse(
            "injecta.api.ApiClient_test",
            {
                "class": "injecta.api.ApiClient.ApiClient",
                "autowire": True,
                "arguments": ["Jirka", 15, False, "@injecta.api.Connector"]
            },
        )
        expected = Service(
            "injecta.api.ApiClient_test",
            DType("injecta.api.ApiClient", "ApiClient"),
            [
                PrimitiveArgument("Jirka"),
                PrimitiveArgument(15),
                PrimitiveArgument(False),
                ServiceArgument("injecta.api.Connector"),
            ],
        )
        expected.set_autowire(True)

        self.assertEqual(expected, result)
    def test_basic(self):
        self.__argumentsValidator.validate(
            'injecta.mocks.Bar',
            [
                ResolvedArgument(
                    'name', PrimitiveArgument('Jiri Koutny'),
                    InspectedArgument('name', DType('builtins', 'str')))
            ],
            {},
            {},
        )

        self.assertTrue(True)
    def test_basic(self):
        result = self.__serviceParser.parse('injecta.api.ApiClient_test', {
            'class': 'injecta.api.ApiClient.ApiClient',
            'autowire': True,
            'arguments': [
                'Jirka',
                15,
                False,
                '@injecta.api.Connector'
            ]
        })
        expected = Service(
            'injecta.api.ApiClient_test',
            DType('injecta.api.ApiClient', 'ApiClient'),
            [
                PrimitiveArgument('Jirka'),
                PrimitiveArgument(15),
                PrimitiveArgument(False),
                ServiceArgument('injecta.api.Connector'),
            ]
        )
        expected.setAutowire(True)

        self.assertEqual(expected, result)
    def test_factory(self):
        result = self.__serviceParser.parse('injecta.api.ApiClient', {
            'factory': ['@injecta.api.ApiClientFactory.ApiClientFactory', 'create'],
            'arguments': [
                'Jirka',
            ]
        })
        expected = Service(
            'injecta.api.ApiClient',
            DType('injecta.api', 'ApiClient'),
            [
                PrimitiveArgument('Jirka'),
            ]
        )
        expected.setFactory(ServiceArgument('injecta.api.ApiClientFactory.ApiClientFactory'), 'create')

        self.assertEqual(expected, result)
    def test_exceptionStringForObject(self):
        with self.assertRaises(Exception) as error:
            self.__argumentsValidator.validate(
                'injecta.mocks.Foo',
                [
                    ResolvedArgument(
                        'bar', PrimitiveArgument('Jiri Koutny'),
                        InspectedArgument('bar',
                                          DType('injecta.mocks.Bar', 'Bar')))
                ],
                {},
                {},
            )

        self.assertEqual(
            'Expected dtype "injecta.mocks.Bar.Bar", got "str" (argument "bar", service "injecta.mocks.Foo")',
            str(error.exception))
Exemple #22
0
    def test_basic(self):
        rawServices = {
            'injecta.mocks.Bar.Bar': {
                'arguments': [
                    'Jiri Koutny'
                ]
            },
            'injecta.mocks.Foo.Foo': {
                'arguments': [
                    '@injecta.mocks.Bar.Bar'
                ]
            },
            'injecta.mocks.Bar.BarAlias': '@injecta.mocks.Bar.Bar',
        }

        expectedService1 = Service(
            'injecta.mocks.Bar.Bar',
            DType('injecta.mocks.Bar', 'Bar'),
            [
                PrimitiveArgument('Jiri Koutny'),
            ]
        )

        expectedService2 = Service(
            'injecta.mocks.Foo.Foo',
            DType('injecta.mocks.Foo', 'Foo'),
            [
                ServiceArgument('injecta.mocks.Bar.Bar'),
            ]
        )

        expectedAlias1 = ServiceAlias('injecta.mocks.Bar.BarAlias', 'injecta.mocks.Bar.Bar')

        services, aliases = self.__servicesPreparer.prepare(rawServices)

        self.assertEqual(expectedService1, services[0])
        self.assertEqual(expectedService2, services[1])
        self.assertEqual(expectedAlias1, aliases[0])
Exemple #23
0
    def test_factory(self):
        result = self.__service_parser.parse(
            "injecta.api.ApiClient",
            {
                "factory":
                ["@injecta.api.ApiClientFactory.ApiClientFactory", "create"],
                "arguments": [
                    "Jirka",
                ],
            },
        )
        expected = Service(
            "injecta.api.ApiClient",
            DType("injecta.api", "ApiClient"),
            [
                PrimitiveArgument("Jirka"),
            ],
        )
        expected.set_factory(
            ServiceArgument("injecta.api.ApiClientFactory.ApiClientFactory"),
            "create")

        self.assertEqual(expected, result)
Exemple #24
0
    def test_args_kwargs(self):
        arguments = [
            PrimitiveArgument(111, "some_number"),
            PrimitiveArgument("George", "name"),
            PrimitiveArgument(True, "some_bool"),
        ]
        inspected_arguments = self.__inspected_arguments_resolver.resolve_constructor(
            DType("injecta.mocks.ArgsKwargs", "ArgsKwargs"))

        resolved_arguments = self.__named_arguments_resolver.resolve(
            arguments, inspected_arguments, "injecta.mocks.ArgsKwargs")

        self.assertEqual(3, len(resolved_arguments))
        self.assertEqual(PrimitiveArgument("George", "name"),
                         resolved_arguments[0].argument)
        self.assertEqual(PrimitiveArgument(111, "some_number"),
                         resolved_arguments[1].argument)
        self.assertEqual(PrimitiveArgument(True, "some_bool"),
                         resolved_arguments[2].argument)
    def test_argsKwargs(self):
        arguments = [
            PrimitiveArgument(111, 'someNumber'),
            PrimitiveArgument('George', 'name'),
            PrimitiveArgument(True, 'someBool'),
        ]
        inspectedArguments = self.__inspectedArgumentsResolver.resolveConstructor(
            DType('injecta.mocks.ArgsKwargs', 'ArgsKwargs'))

        resolvedArguments = self.__namedArgumentsResolver.resolve(
            arguments, inspectedArguments, 'injecta.mocks.ArgsKwargs')

        self.assertEqual(3, len(resolvedArguments))
        self.assertEqual(PrimitiveArgument('George', 'name'),
                         resolvedArguments[0].argument)
        self.assertEqual(PrimitiveArgument(111, 'someNumber'),
                         resolvedArguments[1].argument)
        self.assertEqual(PrimitiveArgument(True, 'someBool'),
                         resolvedArguments[2].argument)
    def test_envPlaceholder(self):
        argument = PrimitiveArgument('ahoj %env(APP_ENV)%')

        self.assertEqual('\'ahoj \' + os.environ[\'APP_ENV\']', argument.getStringValue())
    def test_valuePlaceholder(self):
        argument = PrimitiveArgument('ahoj %foo.bar%')

        self.assertEqual('\'ahoj \' + self.__parameters.foo.bar', argument.getStringValue())
    def test_placeholderCombination(self):
        argument = PrimitiveArgument('ahoj %foo.bar% svete %env(APP_ENV)% novy')

        self.assertEqual('\'ahoj \' + self.__parameters.foo.bar + \' svete \' + os.environ[\'APP_ENV\'] + \' novy\'', argument.getStringValue())
    def test_placeholderCombination2(self):
        argument = PrimitiveArgument('%foo.bar% svete %env(APP_ENV)%')

        self.assertEqual('self.__parameters.foo.bar + \' svete \' + os.environ[\'APP_ENV\']', argument.getStringValue())
    def test_string(self):
        argument = PrimitiveArgument('hello world')

        self.assertEqual('\'hello world\'', argument.getStringValue())