def test_fetch_events_within_time_range_tracker_contains_no_events(): # create mock tracker store that returns `None` on `retrieve()` tracker_store = Mock() tracker_store.retrieve.return_value = DialogueStateTracker.from_events( "a great ID", []) tracker_store.keys.return_value = ["a great ID"] exporter = MockExporter(tracker_store) # no events means `NoEventsInTimeRangeError` with pytest.raises(NoEventsInTimeRangeError): # noinspection PyProtectedMember exporter._fetch_events_within_time_range()
def test_fetch_events_within_time_range(): conversation_ids = ["some-id", "another-id"] # prepare events from different senders and different timestamps event_1 = random_user_uttered_event(3) event_2 = random_user_uttered_event(2) event_3 = random_user_uttered_event(1) events = { conversation_ids[0]: [event_1, event_2], conversation_ids[1]: [event_3] } def _get_tracker(conversation_id: Text) -> DialogueStateTracker: return DialogueStateTracker.from_events(conversation_id, events[conversation_id]) # create mock tracker store tracker_store = Mock() tracker_store.retrieve.side_effect = _get_tracker tracker_store.keys.return_value = conversation_ids exporter = MockExporter(tracker_store) exporter.requested_conversation_ids = conversation_ids # noinspection PyProtectedMember fetched_events = exporter._fetch_events_within_time_range() # events should come back for all requested conversation IDs assert all( any(_id in event["sender_id"] for event in fetched_events) for _id in conversation_ids) # events are sorted by timestamp despite the initially different order assert fetched_events == list( sorted(fetched_events, key=lambda e: e["timestamp"]))
async def test_closing_broker(): exporter = MockExporter(event_broker=SQLEventBroker()) # noinspection PyProtectedMember exporter._fetch_events_within_time_range = Mock(return_value=[]) # run the export function with pytest.warns(None) as warnings: await exporter.publish_events() assert len(warnings) == 0
async def test_closing_broker_sync(): class TestBroker(SQLEventBroker): def close(self) -> None: pass exporter = MockExporter(event_broker=TestBroker()) # noinspection PyProtectedMember exporter._fetch_events_within_time_range = Mock(return_value=[]) # run the export function with pytest.warns(FutureWarning): await exporter.publish_events()
async def test_publishing_error(): # mock event broker so it raises on `publish()` event_broker = Mock() event_broker.publish.side_effect = ValueError() exporter = MockExporter(event_broker=event_broker) user_event = random_user_uttered_event(1).as_dict() user_event["sender_id"] = uuid.uuid4().hex # noinspection PyProtectedMember exporter._fetch_events_within_time_range = Mock(return_value=[user_event]) # run the export function with pytest.raises(PublishingError): await exporter.publish_events()
def test_fetch_events_within_time_range_with_session_events(): conversation_id = "test_fetch_events_within_time_range_with_sessions" tracker_store = SQLTrackerStore( dialect="sqlite", db=f"{uuid.uuid4().hex}.db", domain=Domain.empty() ) events = [ random_user_uttered_event(1), SessionStarted(2), ActionExecuted(timestamp=3, action_name=ACTION_SESSION_START_NAME), random_user_uttered_event(4), ] tracker = DialogueStateTracker.from_events(conversation_id, evts=events) tracker_store.save(tracker) exporter = MockExporter(tracker_store=tracker_store) # noinspection PyProtectedMember fetched_events = exporter._fetch_events_within_time_range() assert len(fetched_events) == len(events)