def test_subscribe_no_app():

    dispatcher = EventDispatcher()

    with pytest.raises(RuntimeError) as exc_info:
        dispatcher.subscribe("invalid type")(lambda _: _)  # noqa

    assert str(exc_info.value) == "Event dispatcher is not initialized."
def test_event_subscribe_and_dispatch(dispatcher: EventDispatcher):

    event_dispatched = DummyEvent()
    event_subscribed = None

    def handler(event: DummyEvent) -> None:
        nonlocal event_subscribed
        event_subscribed = event

    dispatcher.subscribe(DummyEvent, handler)
    dispatcher.dispatch(event_dispatched)

    assert event_subscribed == event_dispatched
def dispatcher():
    app = Flask("testapp")
    return EventDispatcher(app=app)
def test_unsubscribe_invalid_type(dispatcher: EventDispatcher):

    with pytest.raises(TypeError):
        dispatcher.unsubscribe("invalid type", lambda _: _)  # noqa
def test_dispatch_invalid_type(dispatcher: EventDispatcher):

    with pytest.raises(TypeError):
        dispatcher.dispatch("invalid type")  # noqa