def test_set_initial_state(plugin): dt = datetime.datetime.now() events = [ journal.Event(dt, 'test1', {}, '{}'), journal.Event(dt, 'test2', {}, '{}'), journal.Event(dt, 'test3', {}, '{}'), ] reader_mock = mock.MagicMock() reader_mock.get_latest_file_events.return_value = events plugin.journal_reader = reader_mock mutation1 = mock.MagicMock() mutation2 = mock.MagicMock() mutation3 = mock.MagicMock() with mock.patch.dict(gamestate.mutation_registry._callbacks, { 'test1': [mutation1], 'test2': [mutation2], 'test3': [mutation3] }): with mock.patch( 'edp.contrib.gamestate.game_state_set_signal') as signal_mock: plugin.set_initial_state() signal_mock.emit.assert_called_once() mutation1.assert_called_once() mutation2.assert_called_once() mutation3.assert_called_once()
def test_on_journal_event_state_not_changed(plugin): event = journal.Event(datetime.datetime.now(), 'test', {}, '{}') with mock.patch( 'edp.contrib.gamestate.game_state_changed_signal') as signal_mock: plugin.on_journal_event(event) signal_mock.emit.assert_not_called()
def test_update_state_mutation_run(plugin): event = journal.Event(datetime.datetime.now(), 'test', {}, '{}') mutation = mock.MagicMock() with mock.patch.dict(gamestate.mutation_registry._callbacks, {'test': [mutation]}): plugin.update_state(event) mutation.assert_called_once_with(event=event, state=plugin._state)
def test_journal_event_discarded(discarded_events, event_name, is_discarded, plugin, mock_api): event = journal.Event(datetime.datetime.now(), event_name, {}, '') mock_api.discarded_events.return_value = discarded_events plugin.on_journal_event(event) if is_discarded: assert len(plugin._events_buffer) == 0 else: assert len(plugin._events_buffer) == 1
def test_event_filtered(eddn_plugin, event_name, filtered): event = journal.Event(timestamp=datetime.datetime.now(), name=event_name, data={}, raw='{}') eddn_plugin.on_journal_event(event) if filtered: assert len(eddn_plugin._events_buffer) == 0 else: assert len(eddn_plugin._events_buffer) == 1
def test_push_events_event_patched_with_state(mock_api, plugin, state): events = [ journal.Event(datetime.datetime.now(), 'test1', {}, '{}'), journal.Event(datetime.datetime.now(), 'test2', {'foo': 'bar'}, '{"foo": "bar"}'), journal.Event(datetime.datetime.now(), 'test3', {'event': 'test3'}, '{"event": "test3"}'), ] plugin.gamestate = mock.Mock() plugin.gamestate.state = state plugin.process_buffered_events(events) mock_api.journal_event.assert_called_once() patched_events = mock_api.journal_event.call_args[0] assert len(patched_events) == len(events) for patched_event in patched_events: assert {'_systemAddress', '_systemName', '_systemCoordinates', '_marketId', '_stationName', '_shipId'} \ .issubset(set(patched_event.keys()))
def make_event(_name, timestamp=None, **kwargs) -> journal.Event: """ Build Event """ dt = timestamp or datetime.datetime.now() data = { 'event': _name, 'timestamp': utils.to_ed_timestamp(dt), **kwargs } data_str = json.dumps(data) return journal.Event(dt, _name, data, data_str)
def test_on_journal_event_changed_state(plugin): event = journal.Event(datetime.datetime.now(), 'test', {}, '{}') def mutation(event, state: gamestate.GameStateData): state.commander.name = 'test' with mock.patch.dict(gamestate.mutation_registry._callbacks, {'test': [mutation]}): with mock.patch('edp.contrib.gamestate.game_state_changed_signal' ) as signal_mock: plugin.on_journal_event(event) signal_mock.emit.assert_called_once()
def event(): return journal.Event(datetime.datetime.now(), 'test', {}, '{}')
def test_update_state_mutation_not_registered(plugin): event = journal.Event(datetime.datetime.now(), 'test', {}, '{}') assert not plugin.update_state(event)