def test_the_container_can_be_nested_though_this_has_no_meaning():
    original = ContextContainer(container, context_types=[SomeDep])
    with original as context_container_1:
        a = context_container_1.resolve(SomeDep)
        with context_container_1 as context_container_2:
            b = context_container_2.resolve(SomeDep)
    assert a != b
def test_the_container_can_be_reused():
    original = ContextContainer(container, context_types=[SomeDep])
    with original as context_container_1:
        a = context_container_1.resolve(SomeDep)
    with original as context_container_2:
        b = context_container_2.resolve(SomeDep)
    assert a != b
def test_clean_up_of_loaded_contexts_happens_on_container_exit():
    SomeDep.global_clean_up_has_happened = False

    with ContextContainer(container,
                          context_types=[SomeDep]) as context_container:
        assert isinstance(context_container[SomeDep], SomeDep)
        assert not SomeDep.global_clean_up_has_happened
    assert SomeDep.global_clean_up_has_happened
def test_it_fails_if_the_dependencies_arent_defined_correctly():
    with pytest.raises(InvalidDependencyDefinition) as failure:
        with ContextContainer(container,
                              context_types=[SomeNotProperlySetupDef
                                             ]) as context_container:
            context_container.resolve(SomeNotProperlySetupDef)
    assert f"A ContextManager[{SomeNotProperlySetupDef}] should be defined" in str(
        failure.value)
def test_context_instances_can_be_made_singletons():
    SomeDep.global_clean_up_has_happened = False
    with ContextContainer(container,
                          context_types=[],
                          context_singletons=[SomeDep]) as context_container:
        one = context_container[SomeDep]
        two = context_container[SomeDep]
        assert one is two
    assert SomeDep.global_clean_up_has_happened
def test_it_works_with_actual_context_managers():
    class ThingManager:
        def __enter__(self):
            return Thing("managed thing")

        def __exit__(self, exc_type, exc_val, exc_tb):
            pass

    container[ContextManager[Thing]] = ThingManager  # type: ignore

    with ContextContainer(container,
                          context_types=[Thing]) as context_container:
        assert context_container.resolve(Thing).contents == "managed thing"
def test_context_instances_are_not_singletons():
    with ContextContainer(container,
                          context_types=[SomeDep]) as context_container:
        one = context_container[SomeDep]
        two = context_container[SomeDep]
        assert one is not two