def __load_by_absolute_path(self, resource: str) -> None: try: resource_loaded: dict = self.__resource_loaded(resource) or {} except FileNotFoundError as e: raise exceptions.ResourceNotFound(resource) from e except yaml.YAMLError as e: raise exceptions.ParsingErrorOnResource(resource) from e for identifier, definition_content in resource_loaded.items(): arguments = [ Argument.no_kw_argument(arg) for arg in definition_content.get('args', []) ] arguments += [ Argument(arg_name, arg_value) for arg_name, arg_value in definition_content.get('kwargs', {}).items() ] self.__container.set_definition( Definition( identifier, definition_content['fqn'], arguments, ) )
def load(container_builder: ContainerBuilder): container_builder.set_definition( Definition("test.resources.test_full.autodiscover2.b.B", "test.resources.test_full.autodiscover2.b.B", [ Argument.no_kw_argument( "@test.resources.test_full.autodiscover1.a.A") ]))
def setUp(self) -> None: self.container = Container([]) self.definition_a = Definition("example.A", "tests.resources.class_a.A") self.definition_b = Definition( "example.B", "tests.resources.class_b.B", [Argument.no_kw_argument("@example.A")], ) self.definition_c = Definition( "example.C", "tests.resources.class_c.C", [ Argument.no_kw_argument("@example.A"), Argument("kw_arg", "test_param"), Argument("b", "@example.B"), ], )
def test_get_arg_from_dependencies(self, locate_mock): """ Test getting a dependency with an argument with an @ instances the dependency with the argument from the own service mapping """ dependency_argument_instance_mock = MagicMock() dependency_argument_mock = MagicMock() dependency_argument_mock.return_value = dependency_argument_instance_mock dependency_mock = MagicMock() locate_mock.side_effect = [dependency_argument_mock, dependency_mock] test_definition_arg = Definition('test_argument', 'test', [Argument.no_kw_argument('test')]) test_definition = Definition( 'test', 'test', [Argument.no_kw_argument('@test_argument')]) test_config = Mock(spec=Settings) container = ConfigurableContainer( [test_definition_arg, test_definition], test_config) container.get('test') dependency_mock.assert_called_with(dependency_argument_instance_mock)
def test_set_definition(self): """ Test setting definition adds the definition to the service mapping """ test_definition = Definition('test', 'test', [Argument.no_kw_argument('test')]) test_config = Mock(spec=Settings) container = ConfigurableContainer([], test_config) container.set_definition(test_definition) self.assertIn('test', container._service_mapping) self.assertEqual(container._service_mapping['test'], test_definition)
def test_reset(self): """ Test reset clears the service mapping and sets the resolved to false """ test_definition = Definition('test', 'test', [Argument.no_kw_argument('test')]) test_config = Mock(spec=Settings) container = ConfigurableContainer([test_definition], test_config) container.resolve() container.reset() self.assertEqual(len(container._service_mapping), 0) self.assertFalse(container.is_resolved())
def load(container_builder: ContainerBuilder): container_builder.set_definition( Definition('example.A', 'tests.resources.class_a.A') ) container_builder.set_definition( Definition( 'example.B', 'tests.resources.class_b.B', [Argument.no_kw_argument("@example.A")] ) ) container_builder.set_definition( Definition( 'example.C', 'tests.resources.class_c.C', [ Argument.no_kw_argument("@example.A"), Argument("b", "@example.B"), Argument("kw_arg", "test_param"), ] ) )
def test_get_with_more_parameters_than_expected(self): container = Container([ Definition( "example.A", "tests.resources.class_a.A", [Argument("unexpected_argument", "test_param")], ), self.definition_b, self.definition_c ]) with self.assertRaisesRegex( exceptions.ServiceInstantiationFailed, "Type tests.resources.class_a.A cannot be instantiated by the container" ): container.get("example.C")
def test_get_arg_from_config(self, locate_mock): """ Test getting a dependency with an argument with an # instances the dependency with the argument value from the config container """ dependency_class_mock = MagicMock() dependency_mock = MagicMock() dependency_mock.return_value = dependency_class_mock locate_mock.side_effect = dependency_mock test_definition = Definition( 'test', 'test', [Argument.no_kw_argument('#test_argument')]) test_config = Mock(spec=Settings) test_arg_value = 'test_argument_value' test_config.get.return_value = test_arg_value container = ConfigurableContainer([test_definition], test_config) container.get('test') dependency_class_mock.assert_called_with(test_arg_value)
def test_definition_supports_adding_arguments(self): definition = Definition("example1", "example2") definition.arguments.append(Argument.no_kw_argument("example1")) self.assertEqual(1, len(definition.arguments))
def load(): """ Load all the application services in the container """ container.set_definition(Definition( "apm", "elasticapm.Client", [ Argument('transactions_ignore_patterns', ['^OPTIONS ']), Argument('service_name', '#elastic_apm.service_name'), Argument('secret_token', '#elastic_apm.secret_token'), Argument('server_url', '#elastic_apm.url'), ] ) ) container.set_definition(Definition( "storage_engine", "news_service_lib.storage.sql.create_sql_engine", [ Argument.no_kw_argument(SqlEngineType.MYSQL), Argument('host', '#storage.host'), Argument('port', '#storage.port'), Argument('user', '#storage.user'), Argument('password', '#storage.password'), Argument('database', '#storage.database') ] ) ) container.set_definition(Definition( "session_provider", "news_service_lib.storage.sql.SqlSessionProvider", [ Argument.no_kw_argument('@storage_engine') ] ) ) container.set_definition(Definition( "user_service", "services.users_service.UserService", [ Argument.no_kw_argument('@session_provider') ] ) ) container.set_definition(Definition( "auth_service", "services.authentication_service.AuthService", [ Argument.no_kw_argument('@user_service') ] ) )
def load(): """ Load all the application services in the container """ container.set_definition(Definition( "apm", "elasticapm.Client", [ Argument('transactions_ignore_patterns', ['^OPTIONS ']), Argument('service_name', '#elastic_apm.service_name'), Argument('secret_token', '#elastic_apm.secret_token'), Argument('server_url', '#elastic_apm.url'), ] ) ) container.set_definition(Definition( "storage_engine", "news_service_lib.storage.sql.create_sql_engine", [ Argument.no_kw_argument(SqlEngineType.MYSQL), Argument('host', '#storage.host'), Argument('port', '#storage.port'), Argument('user', '#storage.user'), Argument('password', '#storage.password'), Argument('database', '#storage.database') ] ) ) container.set_definition(Definition( "session_provider", "news_service_lib.storage.sql.SqlSessionProvider", [ Argument.no_kw_argument('@storage_engine') ] ) ) crud_service_args = [Argument.no_kw_argument('@session_provider')] container.set_definition(Definition( "named_entity_service", "services.crud.named_entity_service.NamedEntityService", crud_service_args ) ) container.set_definition(Definition( "named_entity_type_service", "services.crud.named_entity_type_service.NamedEntityTypeService", crud_service_args ) ) container.set_definition(Definition( "new_service", "services.crud.new_service.NewService", crud_service_args ) ) container.set_definition(Definition( "newspaper_service", "services.crud.newspaper_service.NewspaperService", crud_service_args ) ) container.set_definition(Definition( "noun_chunk_service", "services.crud.noun_chunk_service.NounChunkService", crud_service_args ) ) container.set_definition(Definition( "source_service", "services.crud.source_service.SourceService", crud_service_args ) ) container.set_definition(Definition( "user_service", "services.crud.user_service.UserService", crud_service_args ) ) container.set_definition(Definition( "index_service", "services.index_service.IndexService" ) ) container.set_definition(Definition( "news_manager_service", "services.news_manager_service.NewsManagerService", [ Argument('protocol', '#news_manager.protocol'), Argument('host', '#news_manager.host'), Argument('port', '#news_manager.port') ] ) ) container.set_definition(Definition( "uaa_service", "news_service_lib.uaa_service.get_uaa_service", [ Argument.no_kw_argument('#uaa') ] ) )
def load(): """ Load all the application services in the container """ container.set_definition( Definition("apm", "elasticapm.Client", [ Argument('transactions_ignore_patterns', ['^OPTIONS ']), Argument('service_name', 'news-manager'), Argument('secret_token', '#elastic_apm.secret_token'), Argument('server_url', '#elastic_apm.url'), ])) storage_type = config.server.storage container.set_definition( Definition("storage_client", "news_service_lib.storage.storage_factory", [ Argument.no_kw_argument(storage_type), Argument.no_kw_argument(f'#{storage_type}'), Argument.no_kw_argument(get_logger()), ])) locker_type = config.server.locker container.set_definition( Definition("locker", "infrastructure.locker.locker_factory", [ Argument.no_kw_argument(locker_type), Argument.no_kw_argument(f'#{locker_type}') ])) container.set_definition( Definition("news_service", "services.news_service.NewsService", [Argument.no_kw_argument('@storage_client')])) container.set_definition( Definition("uaa_service", "news_service_lib.uaa_service.get_uaa_service", [Argument.no_kw_argument('#uaa')])) container.set_definition( Definition("nlp_service_service", "news_service_lib.NlpServiceService", [ Argument('broker_config', '#rabbit'), Argument('redis_config', '#redis_nlp_worker') ])) container.set_definition( Definition("news_consume_service", "services.news_consume_service.NewsConsumeService", [ Argument.no_kw_argument('@news_service'), Argument.no_kw_argument('@nlp_service_service') ])) container.set_definition( Definition("news_publish_service", "services.news_publish_service.NewsPublishService", [ Argument.no_kw_argument('@news_service'), Argument.no_kw_argument('@locker') ]))
def test_argument_without_keywords(self): self.assertEqual( Argument(None, "value"), Argument.no_kw_argument("value") )