def test_run_already_handled_event(self, app_context: ApplicationContext): """Tests that you can't run the same types of callbacks twice.""" yield from app_context.run_callbacks(ContextEventType.started) with pytest.raises(ValueError) as exc: yield from app_context.run_callbacks(ContextEventType.started) assert str(exc.value) == 'the started callbacks for this context have already been run'
def test_add_lazy_property_conflicting_resource(self, app_context: ApplicationContext): app_context.add_lazy_property(ContextScope.application, 'a', lambda ctx: 2) exc = pytest.raises(ResourceConflict, app_context.resources.add, 2, context_var='a') assert str(exc.value) == ( "Resource(types=('int',), alias='default', value=2, context_var='a') " "conflicts with an application scoped lazy property") with pytest.raises(ResourceNotFoundError): yield from app_context.resources.request(int, timeout=0)
def start(self, app_ctx: ApplicationContext): def started_callback(ctx): self.start_callback_called = True def finished_callback(ctx): self.finish_callback_called = True app_ctx.add_callback(ContextEventType.started, started_callback) app_ctx.add_callback(ContextEventType.finished, finished_callback) app_ctx.shutter.shutdown()
def test_add_already_handled_event(self, app_context: ApplicationContext): """Tests that you can't add a callback to an event that's already been handled.""" yield from app_context.run_callbacks(ContextEventType.started) exc = pytest.raises(ValueError, app_context.add_callback, ContextEventType.started, lambda ctx: None) assert str(exc.value) == 'cannot add started callbacks to this context any more'
def test_callbacks(self, app_context: ApplicationContext, event, results1, results2): """Tests that both default and local callbacks are run and they're sorted by priority.""" @coroutine def callback(ctx, *args, **kwargs): nonlocal events assert ctx is context events.append((args, kwargs)) events = [] context = TransportContext(app_context) app_context.add_default_callback(ContextScope.transport, ContextEventType.started, callback, [1, 2], {'a': 3}, CallbackPriority.last) context.add_callback(ContextEventType.finished, callback, [4, 5], {'a': 2}) context.add_callback(ContextEventType.started, callback, [7, 9], {'b': 4}) app_context.add_default_callback(ContextScope.transport, ContextEventType.finished, callback, [6, 1], {'a': 5}, CallbackPriority.first) yield from context.run_callbacks(event) assert len(events) == 2 assert events[0] == results1 assert events[1] == results2
def start(app_ctx: ApplicationContext): app_ctx.add_callback(ContextEventType.finished, finish) raise Exception('bad component')
def test_lazy_property_duplicate(self, app_context: ApplicationContext): app_context.add_lazy_property(ContextScope.application, 'foo', lambda ctx: None) exc = pytest.raises(ValueError, app_context.add_lazy_property, ContextScope.application, 'foo', lambda ctx: None) assert (str(exc.value) == 'there is already a lazy property for "foo" on the application scope')