def test_indeterminate_context_being_resolved(self, container):
     get_qualifier = lambda target: "bar"
     context = DIContext(name="foo", get_qualifier=get_qualifier)
     with pytest.raises(ConfigError) as error_info:
         context.get(container)
     assert error_info.value == DIErrors.INDETERMINATE_CONTEXT_BEING_RESOLVED
     assert error_info.value.params == dict(name="foo",
                                            interface=None,
                                            get_qualifier=get_qualifier)
Beispiel #2
0
 def test_dao_injection_error(self, repo):
     assert repo.entity is Bike
     with pytest.raises(ConfigError) as error_info:
         assert repo.dao
     assert error_info.value == DIErrors.DEFINITION_NOT_FOUND
     assert error_info.value.params == {
         'context': DIContext(interface=IDao, qualifier=Bike)
     }
 def test_container_name_not_found(self, container):
     name = 'frame'
     qualifier = 'qualifier'
     with pytest.raises(ConfigError) as error_info:
         container.find_by_name(name, qualifier)
     assert error_info.value == DIErrors.DEFINITION_NOT_FOUND
     assert error_info.value.params == {
         'context': DIContext(name='frame', qualifier='qualifier')
     }
 def test_get_di_context_by_interface(self, container: Container):
     qualifier = 'qualifier'
     container.register_by_interface(interface=WheelInterface,
                                     qualifier=qualifier,
                                     constructor=RoadWheel)
     instance = container.find_by_interface(WheelInterface,
                                            qualifier=qualifier)
     assert get_di_context(instance) == DIContext(interface=WheelInterface,
                                                  qualifier=qualifier)
 def test_container_name_not_found(self, container):
     name = "frame"
     qualifier = "qualifier"
     with pytest.raises(ConfigError) as error_info:
         container.find_by_name(name, qualifier)
     assert error_info.value == DIErrors.DEFINITION_NOT_FOUND
     assert error_info.value.params == {
         "context": DIContext(name="frame", qualifier="qualifier")
     }
 def test_contradictory_qualifier_defined(self):
     get_qualifier = lambda target: "qualifier"
     with pytest.raises(ConfigError) as error_info:
         DIContext(name="name",
                   qualifier="qualifier",
                   get_qualifier=get_qualifier)
     assert error_info.value == DIErrors.CONTRADICTORY_QUALIFIER_DEFINED
     assert error_info.value.params == dict(qualifier="qualifier",
                                            get_qualifier=get_qualifier)
 def test_container_interface_not_found(self, container):
     interface = FrameInterface
     qualifier = 'qualifier'
     with pytest.raises(ConfigError) as error_info:
         container.find_by_interface(interface, qualifier)
     assert error_info.value == DIErrors.DEFINITION_NOT_FOUND
     assert error_info.value.params == {
         'context': DIContext(interface=FrameInterface,
                              qualifier='qualifier')
     }
 def test_name_get_qualifier_on_instance(self,
                                         class_with_indeterminate_contexts,
                                         container):
     foo = create_component(class_with_indeterminate_contexts, container)
     bar_context = get_di_context(foo.bar)
     # the dependent on component has the dependency context already determined, based
     # on the dependant state
     assert bar_context == DIContext(
         name="bar", qualifier=class_with_indeterminate_contexts.qualifier)
     assert bar_context.is_determined()
 def test_container_name_duplicates(self, container):
     name = 'frame'
     container.register_by_name(name=name, constructor=RoadFrame)
     # registering again the same signature results with an error
     with pytest.raises(ConfigError) as error_info:
         container.register_by_name(name=name, constructor=GravelFrame)
     assert error_info.value == DIErrors.AMBIGUOUS_DEFINITION
     assert error_info.value.params == {'context': DIContext(name='frame')}
     # but registering with the same interface and different qualifier is a different signature
     # and ends with a success
     container.register_by_name(name=name,
                                constructor=GravelFrame,
                                qualifier='gravel')
 def test_container_name_duplicates(self, container):
     name = "frame"
     container.register_by_name(name=name, constructor=RoadFrame)
     # registering again the same signature results with an error
     with pytest.raises(ConfigError) as error_info:
         container.register_by_name(name=name, constructor=GravelFrame)
     assert error_info.value == DIErrors.ALREADY_REGISTERED
     assert error_info.value.params == {"context": DIContext(name="frame")}
     # but registering with the same interface and different qualifier is a different signature
     # and ends with a success
     container.register_by_name(name=name,
                                constructor=GravelFrame,
                                qualifier="gravel")
Beispiel #11
0
 def test_container_interface_duplicates(self, container):
     interface = FrameInterface
     container.register_by_interface(interface, RoadFrame)
     # registering again the same signature results with an error
     with pytest.raises(ConfigError) as error_info:
         container.register_by_interface(interface, GravelFrame)
     assert error_info.value == DIErrors.ALREADY_REGISTERED
     assert error_info.value.params == {
         'context': DIContext(interface=FrameInterface)
     }
     # but registering with the same interface and different qualifier is a different signature
     # and ends with a success
     container.register_by_interface(interface,
                                     GravelFrame,
                                     qualifier='gravel')
 def test_get_di_context_by_name(self, container: Container):
     name = 'frame'
     container.register_by_name(name=name, constructor=RoadFrame)
     instance = container.find_by_name(name)
     assert get_di_context(instance) == DIContext(name=name)
Beispiel #13
0
 def context(self):
     """An empty DI context"""
     return DIContext()
 def test_ambiguous_definition(self, container):
     with pytest.raises(ConfigError) as error_info:
         DIContext(name="name", interface=str).get(container)
     assert error_info.value == DIErrors.AMBIGUOUS_DEFINITION
     assert error_info.value.params == dict(name="name", interface=str)
 def test_empty_definition(self, container):
     with pytest.raises(ConfigError) as error_info:
         DIContext().get(container)
     assert error_info.value == DIErrors.NO_IDENTIFIER_SPECIFIED