def test_export_trackers(tmp_path: Path, monkeypatch: MonkeyPatch): events, namespace = prepare_namespace_and_mocked_tracker_store_with_events( tmp_path, monkeypatch) # mock event broker so we can check its `publish` method is called event_broker = Mock() event_broker.publish = Mock() monkeypatch.setattr(export, "_get_event_broker", lambda _: event_broker) # run the export function export.export_trackers(namespace) # check that only events 1, 2, 3, and 4 have been published # event 6 was sent by `id-3` which was not requested, and event 5 # lies outside the requested time range calls = event_broker.publish.mock_calls # only four events were published (i.e. `publish()` method was called four times) assert len(calls) == 4 # call objects are tuples of (name, pos. args, kwargs) # args itself is a tuple, and we want to access the first one, hence `call[1][0]` # check that events 1-4 were published assert all( any(call[1][0]["text"] == event.text for call in calls) for event in events[:4])
def test_export_trackers_publishing_exceptions(tmp_path: Path, monkeypatch: MonkeyPatch, exception: Exception): events, namespace = prepare_namespace_and_mocked_tracker_store_with_events( tmp_path, monkeypatch) # mock event broker so we can check its `publish` method is called event_broker = Mock() event_broker.publish.side_effect = exception monkeypatch.setattr(export, "_get_event_broker", lambda _: event_broker) with pytest.raises(SystemExit): export.export_trackers(namespace)
def test_export_trackers_publishing_exceptions(tmp_path: Path, monkeypatch: MonkeyPatch, exception: Exception): events, namespace = prepare_namespace_and_mocked_tracker_store_with_events( tmp_path, monkeypatch) # mock event broker so we can check its `publish` method is called event_broker = Mock() event_broker.publish.side_effect = exception async def _get_event_broker( _: rasa_core_utils.AvailableEndpoints) -> EventBroker: return event_broker monkeypatch.setattr(export, "_get_event_broker", _get_event_broker) with pytest.raises(SystemExit): export.export_trackers(namespace)