def test_class_mapping(): container.initialize() container.register_class(Example, Example2) instance = container.get(Example) assert type(instance) is Example2
def test_class_with_default_arguments(): container._clear_all() instance = container.get(HasDefaultArguments) assert instance.i == 1 assert instance.s == "s" assert instance.d is None
def test_register_multiple(): container.register_many(Example, [Example1, Example2]) instance = container.get(List[Example]) assert type(instance) is list assert len(instance) == 2 assert type(instance[0]) is Example1 assert type(instance[1]) is Example2
def test_recursive_injection(): instance = container.get(Hierarchy3) assert_type(instance, Hierarchy3) assert_type(instance.arg1, Hierarchy10) assert_type(instance.arg2, Hierarchy2) assert_type(instance.arg1.arg, Hierarchy0) assert_type(instance.arg2.arg1, Hierarchy10) assert_type(instance.arg2.arg2, Hierarchy11) assert_type(instance.arg2.arg2.arg, Hierarchy0) assert_type(instance.arg2.arg2.arg, Hierarchy0)
def test_register_multiple_when_onc_instance_is_already_registered(): container._clear_all() ex1 = Example2() container.register(Example1, ex1) container.register_many(Example, [Example1, Example3]) instance = container.get(Main2) assert type(instance) is Main2 assert len(instance.examples) == 2 assert instance.examples[0] == ex1 assert type(instance.examples[1]) is Example3
def test_argument_injection(): config_value = "config value" container.initialize(MemoryConfig({CONFIG_ARGUMENT: config_value})) container.register_instance_by_name("test_service", ExampleServiceByName()) instance = container.get(Example) assert type(instance) is Example assert instance.arg == FREE_ARGUMENT assert instance.arg2 == PLAIN_ARGUMENT assert instance.arg3 == config_value assert instance.arg4 == CONFIG_DEFAULT assert isinstance(instance.arg5, ExampleServiceByName)
def test_multiple_list_arguments_with_wrap(): container._clear_all() container.register_many(Example, [Example2, Example3]) container.register_many(Example1, [Example3, Example4]) instance = container.get(MainWrapper) assert type(instance) is MainWrapper assert len(instance.main.examples) == 2 assert len(instance.main.examples1) == 2 assert type(instance.main.examples[0]) is Example2 assert type(instance.main.examples[1]) is Example3 assert type(instance.main.examples1[0]) is Example3 assert type(instance.main.examples1[1]) is Example4
def test_class_mapping_from_init(): container.initialize(class_mapping={Example: Example2}) instance = container.get(Example) assert type(instance) is Example2
def test_config_provider_is_not_initialized(): container.initialize() with pytest.raises(ConfigProviderRequiredException): container.get(Example)
def test_argument_not_in_config_and_no_default(): container.initialize(MemoryConfig({})) with pytest.raises(KeyError): container.get(ExampleThrow)
def assert_instance_of(cls: Type, expected_count): container.get(cls) container.get(cls) assert cls.count == expected_count