def test_handle_events(self): test_events = [dict(message_id="t000-0001"), dict(message_id="t000-0002"), dict(message_id="t000-0003")] new_events = [dict(message_id="t000-0004")] conf = [dict(name='test_thing', params={}), dict(name='other_thing', params={}), dict(name='some_thing', params={})] handler_class1 = mock.MagicMock(name='handler1') handler_class2 = mock.MagicMock(name='handler2') handler_class3 = mock.MagicMock(name='handler3') handler_class3.return_value.handle_events.return_value = ( test_events + new_events) handler_map = {'test_thing': handler_class1, 'other_thing': handler_class2, 'some_thing': handler_class3} p = pipeline_manager.Pipeline("test_pipeline", conf, handler_map) p.commit = mock.MagicMock(name='commit') p.rollback = mock.MagicMock(name='rollback') ret = p.handle_events(test_events, self.fake_stream, self.debugger) handler_class1.return_value.handle_events.assert_called_once_with( test_events, p.env) events1 = handler_class1.return_value.handle_events.return_value handler_class2.return_value.handle_events.assert_called_once_with( events1, p.env) events2 = handler_class2.return_value.handle_events.return_value handler_class3.return_value.handle_events.assert_called_once_with( events2, p.env) p.commit.assert_called_once_with(self.debugger) self.assertFalse(p.rollback.called) self.assertEqual(ret, new_events)
def test_handle_events_error(self): test_events = [dict(message_id="t000-0001"), dict(message_id="t000-0002"), dict(message_id="t000-0003")] conf = [dict(name='test_thing', params={}), dict(name='other_thing', params={}), dict(name='some_thing', params={})] handler_class1 = mock.MagicMock(name='handler1') handler_class2 = mock.MagicMock(name='handler2') handler_class3 = mock.MagicMock(name='handler3') class WhackyError(Exception): pass handler_class2.return_value.handle_events.side_effect = WhackyError( "whoa!") handler_map = {'test_thing': handler_class1, 'other_thing': handler_class2, 'some_thing': handler_class3} p = pipeline_manager.Pipeline("test_pipeline", conf, handler_map) p.commit = mock.MagicMock(name='commit') p.rollback = mock.MagicMock(name='rollback') with self.assertRaises(pipeline_manager.PipelineExecutionError): p.handle_events(test_events, self.fake_stream, self.debugger) p.rollback.assert_called_once_with(self.debugger) self.assertFalse(p.commit.called)
def test_rollback_with_error(self): conf = [ dict(name='test_thing', params={}), dict(name='other_thing', params={}), dict(name='some_thing', params={}) ] handler_class1 = mock.MagicMock(name='handler1') handler_class2 = mock.MagicMock(name='handler2') handler_class3 = mock.MagicMock(name='handler3') class WhackyError(Exception): pass handler_class2.return_value.rollback.side_effect = WhackyError("whoa!") handler_map = { 'test_thing': handler_class1, 'other_thing': handler_class2, 'some_thing': handler_class3 } p = pipeline_manager.Pipeline("test_pipeline", conf, handler_map) p.rollback(self.debugger) handler_class1.return_value.rollback.assert_called_once_with() handler_class2.return_value.rollback.assert_called_once_with() handler_class3.return_value.rollback.assert_called_once_with()
def test_init(self): conf = [dict(name='test_thing', params={'book': 42})] handler_class = mock.MagicMock() handler_map = {'test_thing': handler_class} p = pipeline_manager.Pipeline("test_pipeline", conf, handler_map) self.assertEqual(p.name, "test_pipeline") self.assertEqual(len(p.handlers), 1) self.assertIs(handler_class.return_value, p.handlers[0]) handler_class.assert_called_once_with(book=42)
def test_rollback(self): conf = [dict(name='test_thing', params={}), dict(name='other_thing', params={}), dict(name='some_thing', params={})] handler_class1 = mock.MagicMock(name='handler1') handler_class2 = mock.MagicMock(name='handler2') handler_class3 = mock.MagicMock(name='handler3') handler_map = {'test_thing': handler_class1, 'other_thing': handler_class2, 'some_thing': handler_class3} p = pipeline_manager.Pipeline("test_pipeline", conf, handler_map) p.rollback(self.debugger) handler_class1.return_value.rollback.assert_called_once_with() handler_class2.return_value.rollback.assert_called_once_with() handler_class3.return_value.rollback.assert_called_once_with()