def test_runs_a_single_context_manager(self):
        ControllableContextManager.reset_events()
        mgr = ControllableContextManager()
        with multi_context_manager([mgr]):
            pass

        expected_calls = [(mgr, '__enter__'), (mgr, '__exit__')]
        assert expected_calls == self.get_recorded_calls()
def wrap_cls_method(cls, method_name, collector_id, ctx_key, is_cls_method):
    ctx_value = '{} ({})'.format(
        method_name, unittest.util.strclass(cls))
    ctx = scoped_context(key=ctx_key, value=ctx_value)
    mcm = multi_context_manager(
        [ctx] + list((DjptTestRunnerMixin.collectors[collector_id]))
    )
    wrap_cls_method_in_ctx_manager(
        cls=cls, method_name=method_name, ctx_manager=mcm,
        is_cls_method=is_cls_method)
    def test_when_outer_enter_fails(self):
        ControllableContextManager.reset_events()
        outer = ControllableContextManager(fail_in_method='__enter__')
        inner = ControllableContextManager()

        with pytest.raises(ControllableContextManager.TestException):
            self.run_nested_context_managers(outer, inner)

        expected_calls = self.get_recorded_calls()
        assert expected_calls == [(outer, '__enter__')]

        ControllableContextManager.reset_events()
        with pytest.raises(ControllableContextManager.TestException):
            with multi_context_manager([outer, inner]):
                pass
        assert expected_calls == self.get_recorded_calls()
    def test_runs_all_context_managers(self):
        ControllableContextManager.reset_events()
        outer = ControllableContextManager()
        inner = ControllableContextManager()

        expected_calls = self.get_recorded_calls_for_nested_ctx_managers(
            outer, inner)
        assert expected_calls == [
            (outer, '__enter__'), (inner, '__enter__'),
            (inner, '__exit__'), (outer, '__exit__')
        ]

        ControllableContextManager.reset_events()
        with multi_context_manager([outer, inner]):
            pass

        assert expected_calls == self.get_recorded_calls()
    def test_when_code_inside_context_managers_fails(self):
        ControllableContextManager.reset_events()
        outer = ControllableContextManager()
        inner = ControllableContextManager()

        def fail():
            raise NotImplementedError('hi')

        with pytest.raises(NotImplementedError):
            self.run_nested_context_managers(outer, inner, fn=fail)
        expected_calls = self.get_recorded_calls()
        assert expected_calls == [
            (outer, '__enter__'), (inner, '__enter__'),
            (inner, '__exit__'), (outer, '__exit__')
        ]

        ControllableContextManager.reset_events()
        with pytest.raises(NotImplementedError):
            with multi_context_manager([outer, inner]):
                fail()

        assert expected_calls == self.get_recorded_calls()
    def test_three_managers_middle_one_fails(self):
        """ this is like raising the LimitViolationError """
        ControllableContextManager.reset_events()
        outer = ControllableContextManager()
        middle = ControllableContextManager(fail_in_method='__exit__')
        inner = ControllableContextManager()

        ControllableContextManager.reset_events()
        with pytest.raises(ControllableContextManager.TestException):
            with outer:
                with middle:
                    with inner:
                        pass
        expected_calls = self.get_recorded_calls()
        assert expected_calls == [
            (outer, '__enter__'), (middle, '__enter__'), (inner, '__enter__'),
            (inner, '__exit__'), (middle, '__exit__'), (outer, '__exit__')
        ]
        ControllableContextManager.reset_events()
        with pytest.raises(ControllableContextManager.TestException):
            with multi_context_manager([outer, middle, inner]):
                pass
        assert expected_calls == self.get_recorded_calls()
 def test_cant_construct_it_without_a_managers(self):
     with pytest.raises(ValueError):
         multi_context_manager([])