def test__inject_multiple__with_no_matches_for_group_when_non_optional( self, get_namespace_injectables_mock, filter_by_group_mock, ): # given matches = {MagicMock(spec=Injectable), MagicMock(spec=Injectable)} lookup_key = "TEST" lookup_type = "class" get_namespace_injectables_mock.return_value = [ matches, lookup_key, lookup_type ] filter_by_group_mock.return_value = {} dependency = "TEST" # when with pytest.raises(InjectionError): inject_multiple(dependency, group="TEST_GROUP") # then assert get_namespace_injectables_mock.called is True assert filter_by_group_mock.called is True
def test__inject_multiple__with_no_matches_for_dependency_when_non_optional( self, get_dependency_name_mock, get_dependency_registry_type_mock, get_namespace_injectables_mock, filter_by_group_mock, ): # given matches = {} dependency_name = "TEST" get_dependency_name_mock.return_value = dependency_name registry_type = RegistryType.CLASS get_dependency_registry_type_mock.return_value = registry_type get_namespace_injectables_mock.return_value = matches dependency = "TEST" # when with pytest.raises(InjectionError): inject_multiple(dependency) # then assert get_namespace_injectables_mock.called is True assert filter_by_group_mock.called is False
def test__inject_multiple__with_default_values( self, get_dependency_name_mock, get_dependency_registry_type_mock, get_namespace_injectables_mock, filter_by_group_mock, ): # given expected_instances = [MagicMock(), MagicMock()] injectables = [MagicMock(spec=Injectable), MagicMock(spec=Injectable)] for i in range(len(expected_instances)): injectables[i].get_instance.return_value = expected_instances[i] matches = {*injectables} dependency_name = "TEST" get_dependency_name_mock.return_value = dependency_name registry_type = RegistryType.CLASS get_dependency_registry_type_mock.return_value = registry_type get_namespace_injectables_mock.return_value = matches dependency = "TEST" # when instances = inject_multiple(dependency) # then assert get_namespace_injectables_mock.called is True ( dependency_name_arg, registry_type_arg, namespace_arg, ) = get_namespace_injectables_mock.call_args[0] assert dependency_name_arg is dependency assert registry_type_arg is registry_type assert namespace_arg is DEFAULT_NAMESPACE assert filter_by_group_mock.called is False assert all(injectable.get_instance.called is True for injectable in injectables) assert all(injectable.get_instance.call_args[1]["lazy"] is False for injectable in injectables) assert len(instances) == len(expected_instances) assert all(instance in expected_instances for instance in instances)
def __init__(self, ): self.primary_basic_service = inject(SampleService) self.all_basic_service_implementations = inject_multiple(SampleService)
def pytest_generate_tests(metafunc): if "example" in metafunc.fixturenames: InjectionContainer.load("../../examples") examples = inject_multiple(Example) ids = [example.__class__.__qualname__ for example in examples] metafunc.parametrize("example", examples, ids=ids)
def test__inject_multiple__with_explicit_values( self, get_dependency_name_mock, get_dependency_registry_type_mock, get_namespace_injectables_mock, filter_by_group_mock, ): # given expected_instances = [MagicMock(), MagicMock()] injectables = [ MagicMock(spec=Injectable), MagicMock(spec=Injectable), MagicMock(spec=Injectable), ] for i in range(len(expected_instances)): injectables[i].get_instance.return_value = expected_instances[i] matches = {*injectables} dependency_name = "TEST" get_dependency_name_mock.return_value = dependency_name registry_type = RegistryType.CLASS get_dependency_registry_type_mock.return_value = registry_type get_namespace_injectables_mock.return_value = matches filtered_matches = {*injectables[:2]} filter_by_group_mock.return_value = filtered_matches dependency = "TEST" namespace = "TEST_NAMESPACE" group = "TEST_GROUP" exclude_groups = ["A", "B"] # when instances = inject_multiple( dependency, namespace=namespace, group=group, exclude_groups=exclude_groups, lazy=True, optional=False, ) # then assert get_namespace_injectables_mock.called is True ( dependency_name_arg, registry_type_arg, namespace_arg, ) = get_namespace_injectables_mock.call_args[0] assert dependency_name_arg is dependency_name assert registry_type_arg is registry_type assert namespace_arg is namespace assert filter_by_group_mock.called is True matches_arg, group_arg, exclude_groups_arg = filter_by_group_mock.call_args[ 0] assert matches_arg == matches assert group_arg == group assert exclude_groups_arg == exclude_groups assert injectables[0].get_instance.called is True assert injectables[1].get_instance.called is True assert injectables[2].get_instance.called is False assert all(injectable.get_instance.call_args[1]["lazy"] is True for injectable in injectables[:2]) assert len(instances) == len(expected_instances) assert all(instance in expected_instances for instance in instances)