コード例 #1
0
    def test__register_injectable__with_explicit_values(
            self, patch_injection_container):
        # given
        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_injectable(klass, filepath, 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 klass
        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
コード例 #2
0
 def decorator(klass: T, direct_call: bool = False) -> T:
     steps_back = 3 if direct_call else 2
     caller_filepath = get_caller_filepath(steps_back)
     if caller_filepath == InjectionContainer.LOADING_FILEPATH:
         InjectionContainer._register_injectable(
             klass, caller_filepath, qualifier, primary, namespace, group, singleton
         )
     return klass