def test_layabout_can_register_handler(): """ Test that layabout can register Slack API event handlers normally. """ layabout = Layabout() def handle_hello(slack, event): pass layabout.handle(type='hello')(fn=handle_hello) assert len(layabout._handlers) == 1
def test_layabout_can_register_handler_with_kwargs(): """ Test that layabout can register Slack API event handlers with keyword arguments normally. """ layabout = Layabout() kwargs = dict(spam='🍳') def handle_hello(slack, event): pass layabout.handle(type='hello', kwargs=kwargs)(fn=handle_hello) assert len(layabout._handlers) == 1
def test_layabout_raises_type_error_with_invalid_handler(): """ Test that layabout raises a TypeError if an event handler that doesn't meet the minimum criteria to be called correctly is registered. """ layabout = Layabout() def invalid_handler(): pass with pytest.raises(TypeError) as exc: layabout.handle(type='hello')(fn=invalid_handler) expected = ("invalid_handler() missing 2 required positional arguments: " "'slack' and 'event'") assert str(exc.value) == expected
def test_layabout_can_handle_an_event(events, run_once, monkeypatch): """ Test that layabout can handle an event. """ layabout = Layabout() SlackClient, slack = mock_slack(connections=(True, ), reads=(events, [])) handle_hello = MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('hello')(handle_hello) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) handle_hello.assert_called_once_with(slack, events[0])
def test_layabout_can_handle_all_events_with_a_splat_handler( events, run_once, monkeypatch): """ Test that layabout can handle all events with a splat handler. """ layabout = Layabout() SlackClient, slack = mock_slack(connections=(True, ), reads=(events, [])) splat = MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('*')(splat) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) splat.assert_has_calls([call(slack, e) for e in events])
def test_layabout_can_only_handle_events_that_happen(events, run_once, monkeypatch): """ Test that layabout only handles events that actually happen. """ layabout = Layabout() SlackClient, _ = mock_slack(connections=(True, ), reads=(events, [])) aint_happenin = MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('this will never happen')(aint_happenin) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) aint_happenin.assert_not_called()
def test_layabout_can_handle_multiple_events(events, run_once, monkeypatch): """ Test that layabout calls all handlers for their respective events. """ layabout = Layabout() SlackClient, slack = mock_slack(connections=(True, ), reads=(events, [])) handle_hello, handle_goodbye = MagicMock(), MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('hello')(handle_hello) layabout.handle('goodbye')(handle_goodbye) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) handle_hello.assert_called_once_with(slack, events[0]) handle_goodbye.assert_called_once_with(slack, events[1])
def test_layabout_can_handle_an_event_with_kwargs(events, run_once, monkeypatch): """ Test that layabout can call an event handler that requires kwargs. """ layabout = Layabout() SlackClient, slack = mock_slack(connections=(True, ), reads=(events, [])) kwargs = dict(caerbannog='🐰') handle_hello = MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('hello', kwargs=kwargs)(handle_hello) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) handle_hello.assert_called_once_with(slack, events[0], **kwargs)
def test_layabout_can_handle_one_event_multiple_times(events, run_once, monkeypatch): """ Test that layabout calls all handlers for a given event. """ layabout = Layabout() SlackClient, slack = mock_slack(connections=(True, ), reads=(events, [])) handle_hello1, handle_hello2 = MagicMock(), MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('hello')(handle_hello1) layabout.handle('hello')(handle_hello2) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) handle_hello1.assert_called_once_with(slack, events[0]) handle_hello2.assert_called_once_with(slack, events[0])
def test_layabout_can_handle_events_with_normal_and_splat_handlers( events, run_once, monkeypatch): """ Test that layabout can handle an event with normal handlers as well as a splat handler. """ layabout = Layabout() SlackClient, slack = mock_slack(connections=(True, ), reads=(events, [])) handle_hello, splat = MagicMock(), MagicMock() monkeypatch.setattr('layabout.SlackClient', SlackClient) layabout.handle('hello')(handle_hello) layabout.handle('*')(splat) layabout.run(connector=TOKEN, interval=0, retries=0, backoff=lambda r: 0, until=run_once) handle_hello.assert_called_once_with(slack, events[0]) splat.assert_has_calls([call(slack, e) for e in events])