def test__register_factory__for_qualifier_with_defaults( self, patch_injection_container): # given factory = MagicMock qualifier = "QUALIFIER" filepath = os.path.join("fake", "path", "file.py") namespace = "TEST" InjectionContainer.LOADING_DEFAULT_NAMESPACE = namespace mocked_namespace: Namespace = MagicMock(spec=Namespace) patch_injection_container( "Namespace", return_value=mocked_namespace, ) # when InjectionContainer._register_factory(factory, filepath, qualifier=qualifier) # then assert namespace in InjectionContainer.NAMESPACES assert InjectionContainer.NAMESPACES[namespace] == mocked_namespace assert mocked_namespace.register_injectable.call_count == 1 ( injectable_arg, klass_arg, qualifier_arg, ) = mocked_namespace.register_injectable.call_args[0][:3] assert injectable_arg.constructor is factory assert injectable_arg.unique_id is not None assert injectable_arg.primary is False assert injectable_arg.group is None assert injectable_arg.singleton is False assert klass_arg is None assert qualifier_arg is qualifier
def decorator(fn: Callable[..., T]) -> Callable[..., T]: caller_filepath = get_caller_filepath() if caller_filepath == InjectionContainer.LOADING_FILEPATH: InjectionContainer._register_factory( fn, caller_filepath, dependency, qualifier, primary, namespace, group, singleton, ) return fn
def test__register_factory__with_all_explicit_values( self, patch_injection_container): # given factory = MagicMock klass = TestInjectionContainer filepath = os.path.join("fake", "path", "file.py") qualifier = "QUALIFIER" namespace = "TEST" group = "GROUP" InjectionContainer.LOADING_DEFAULT_NAMESPACE = DEFAULT_NAMESPACE mocked_namespace: Namespace = MagicMock(spec=Namespace) patch_injection_container( "Namespace", return_value=mocked_namespace, ) # when InjectionContainer._register_factory(factory, filepath, klass, qualifier, True, namespace, group, True) # then assert namespace in InjectionContainer.NAMESPACES assert InjectionContainer.NAMESPACES[namespace] == mocked_namespace assert mocked_namespace.register_injectable.call_count == 1 ( injectable_arg, klass_arg, qualifier_arg, ) = mocked_namespace.register_injectable.call_args[0][:3] assert injectable_arg.constructor is factory assert injectable_arg.unique_id is not None assert injectable_arg.primary is True assert injectable_arg.group is group assert injectable_arg.singleton is True assert klass_arg is klass assert qualifier_arg is qualifier