class Kernel(object): _tree_processor = None _command_handler = None @logger.class_construct def __init__(self): self._tree_processor = TreeProcessor() self._command_handler = CommandProcessor() # @log_func def talk(self, message, from_user): """ Main function what send requests to the processors :param message: message from user :param from_user: is message from private chat with user :return: list of answer for user and filename or answer only :rtype: list """ self._tree_processor.create_message_tree(message) self._tree_processor.parse_message_tree() (msg_command, msg_args) = self._tree_processor.get_commands() if msg_command == "": msg_tags = self._tree_processor.get_tags() msg_rating = self._tree_processor.get_rating() result = self._command_handler.execute( msg_command, [msg_tags, msg_rating, from_user]) else: result = self._command_handler.execute(msg_command, msg_args) return result
def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer)
class CommandProcessorFixture(unittest.TestCase): """ Command Processor tests """ def setUp(self): self._subscriber_registry = Registry() self._commandProcessor = CommandProcessor(registry=self._subscriber_registry) def test_handle_command(self): """ given that we have a handler registered for a command, when we send a command, it should call the handler""" self._handler = MyCommandHandler() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) self._commandProcessor.send(self._request) self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message") def test_handle_event(self): """ Given that we have many handlers registered of an event, when we raise an event, it should call all the handlers""" self._handler = MyEventHandler() self._other_handler = MyEventHandler() self._request = MyEvent() self._subscriber_registry.register(MyEvent, lambda: self._handler) self._subscriber_registry.register(MyEvent, lambda: self._other_handler) self._commandProcessor.publish(self._request) self.assertTrue(self._handler.called, "The first handler should be called with the message") self.assertTrue((self._other_handler, "The second handler should also be called with the message")) def test_missing_command_handler_registration(self): """Given that we are missing a handler for a command, when we send a command, it should throw an exception""" self._handler = MyCommandHandler() self._request = MyCommand() exception_thrown = False try: self._commandProcessor.send(self._request) except: exception_thrown = True self.assertTrue(exception_thrown, "Expected an exception to be thrown when no handler is registered for a command") def test_missing_event_handler_registration(self): """Given that we have no handlers register for an event, when we raise an event, it should not error """ self._handler = MyEventHandler() self._other_handler = MyEventHandler() self._request = MyEvent() exception_thrown = False try: self._commandProcessor.publish(self._request) except: exception_thrown = True self.assertFalse(exception_thrown, "Did not expect an exception to be thrown where there are no handlers for an event")
class PostTests(unittest.TestCase): def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer) def test_handle_command(self): """ given that we have a message mapper and producer registered for a commnd, when we post a command, it should send via the producer""" self._request = MyCommand() self._commandProcessor.post(self._request) self.assertTrue(self._message_store.message_was_added, "Expected a message to be added") self.assertTrue(self._message_store.get_message(self._request.id), "Expected the command to be converted into a message") self.assertTrue(self._producer.was_sent_message, "Expected a message to be sent via the producer")
def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer, )
def test_missing_message_producer(self): """given that we have no me message producer configured for the commandprocessor when we post a command it should raise a confiugration error """ self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=None) was_exception_thrown = False try: self._request = MyCommand() self._commandProcessor.post(self._request) except ConfigurationException: was_exception_thrown = True self.assertTrue(was_exception_thrown)
def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._messageMapperRegistry.register(MyCommand, lambda: map_to_message) self._messageMapperRegistry self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer)
class PostTests(unittest.TestCase): def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._messageMapperRegistry.register(MyCommand, map_to_message) self._messageMapperRegistry self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer) def test_handle_command(self): """ given that we have a message mapper and producer registered for a commnd, when we post a command, it should send via the producer""" self._request = MyCommand() self._commandProcessor.post(self._request) self.assertTrue(self._message_store.message_was_added, "Expected a message to be added") self.assertTrue(self._message_store.get_message(self._request.id), "Expected the command to be converted into a message") self.assertTrue(self._producer.was_sent_message, "Expected a message to be sent via the producer")
class PostTests(unittest.TestCase): def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._messageMapperRegistry.register(MyCommand, map_to_message) self._messageMapperRegistry self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer) def test_handle_command(self): """ given that we have a message mapper and producer registered for a commnd, when we post a command, it should send via the producer """ self._request = MyCommand() self._commandProcessor.post(self._request) self.assertTrue(self._message_store.message_was_added, "Expected a message to be added") self.assertTrue(self._message_store.get_message(self._request.id), "Expected the command to be converted into a message") self.assertTrue(self._producer.was_sent_message, "Expected a message to be sent via the producer") self.assertTrue(str(self._message_store.get_message(self._request.id).body), "") def test_missing_message_mapper(self): """given that we have no message mapper registered for a command when we post a command it should raise an error """ self._request = MyOtherCommand() was_exception_thrown = False try: self._commandProcessor.post(self._request) except ConfigurationException as ex: was_exception_thrown = True self.assertTrue(was_exception_thrown)
class LoggingAndMonitoringFixture(unittest.TestCase): def setUp(self): self._subscriber_registry = Registry() self._commandProcessor = CommandProcessor(registry=self._subscriber_registry) def test_logging_a_handler(self): """s Given that I have a handler decorated for logging When I call that handler Then I should receive logs indicating the call and return of the handler * N.b. This is an example of using decorators to extend the Brightside pipeline """ handler = MyCommandHandler() request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: handler) logger = logging.getLogger("tests.handlers_testdoubles") with patch.object(logger, 'log') as mock_log: self._commandProcessor.send(request) mock_log.assert_has_calls([call(logging.DEBUG, "Entering handle " + str(request)), call(logging.DEBUG, "Exiting handle " + str(request))])
def setUpClass(cls): print("Init tests...\n" + "=" * 50 + "\n") resource_path = '/'.join(('../snippets', 'input.json')) template = pkg_resources.resource_stream(__name__, resource_path) line = template.read().decode('utf-8') cls.input_strings = json.loads(line)['messages']['command_processor'] template.close() resource_path = '/'.join(('../snippets', 'model.json')) template = pkg_resources.resource_stream(__name__, resource_path) line = template.read().decode('utf-8') cls.noChoices = json.loads(line)['command_processor']['noChoices'] template.close() cls.sp = CommandProcessor() cls.sp.__init__()
def test_missing_message_producer(self): """given that we have no me message producer configured for the commandprocessor when we post a command it should raise a confiugration error """ self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=None) was_exception_thrown = False try: request = MyCommand() self._commandProcessor.post(request) except ConfigurationException: was_exception_thrown = True self.assertTrue(was_exception_thrown)
def setUp(self): self._subscriber_registry = Registry() self._commandProcessor = CommandProcessor( registry=self._subscriber_registry)
class PipelineTests(unittest.TestCase): """Tests of decorators on handlers. It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable to Pythoh via its decorator syntax This test suite is really about proving we can provide equivalent functionality We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter """ def setUp(self): self._subscriber_registry = Registry() self._commandProcessor = CommandProcessor( registry=self._subscriber_registry) def test_handle_retry_on_command(self): """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """ self._handler = MyHandlerSupportingRetry() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) self._commandProcessor.send(self._request) self.assertTrue( self._handler.called, "Expected the handle method on the handler to be called with the message" ) self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline") def test_exceed_retry_on_command(self): """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries""" self._handler = MyHandlerBreakingAfterRetry() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) exception_raised = False try: self._commandProcessor.send(self._request) except RuntimeError: exception_raised = True self.assertTrue( exception_raised, "Exepcted the exception to bubble out, when we run out of retries") self.assertFalse( self._handler.called, "Did not expect the handle method on the handler to be called with the message" ) self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline") def test_handle_circuit_breaker_on_command(self): """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries""" self._handler = MyHandlerBreakingCircuitAfterThreeFailures() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) exception_raised = False try: self._commandProcessor.send(self._request) except RuntimeError: exception_raised = True # Now see if the circuit is broken following the failed call circuit_broken = False try: self._commandProcessor.send(self._request) except CircuitBrokenError: circuit_broken = True self.assertTrue( exception_raised, "Exepcted an exception to be raised, when we run out of retries") self.assertTrue(circuit_broken, "Expected the circuit to be broken to further calls") self.assertFalse( self._handler.called, "Did not expect the handle method on the handler to be called with the message" ) self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")
def setUp(self): self._subscriber_registry = Registry() self._commandProcessor = CommandProcessor(registry=self._subscriber_registry)
def __init__(self): self._tree_processor = TreeProcessor() self._command_handler = CommandProcessor()
class PipelineTests(unittest.TestCase): """Tests of decorators on handlers. It is worth noting that whereas Brighter has to create a mechanism to allow decorators on the target handler, Python already has a decorator syntax. As this maps well to the attributes used by Brighter to signal the decorators used to meet orthogonal concerns it is natural to just use Python's own decorator syntax in Brightside, over rolling up a bespoke mechanism. Indeed, the use of attributes and decorator classes in Brighter mirrors functionality avaailable to Pythoh via its decorator syntax This test suite is really about proving we can provide equivalent functionality We use Benjamin Hodgoson's Poll library, which is itself inspired by Polly, the library used by Brighter """ def setUp(self): self._subscriber_registry = Registry() self._commandProcessor = CommandProcessor(registry=self._subscriber_registry) def test_handle_retry_on_command(self): """ given that we have a retry plocy for a command, when we raise an exception, then we should retry n times or until succeeds """ self._handler = MyHandlerSupportingRetry() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) self._commandProcessor.send(self._request) self.assertTrue(self._handler.called, "Expected the handle method on the handler to be called with the message") self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline") def test_exceed_retry_on_command(self): """ given that we have a retry policy for a command, when we raise an exception, then we should bubble the exception out after n retries""" self._handler = MyHandlerBreakingAfterRetry() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) exception_raised = False try: self._commandProcessor.send(self._request) except RuntimeError: exception_raised = True self.assertTrue(exception_raised, "Exepcted the exception to bubble out, when we run out of retries") self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message") self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline") def test_handle_circuit_breaker_on_command(self): """ given that we have a circuit breaker policy for a command, when we raise an exception, then we should break the circuit after n retries""" self._handler = MyHandlerBreakingCircuitAfterThreeFailures() self._request = MyCommand() self._subscriber_registry.register(MyCommand, lambda: self._handler) exception_raised = False try: self._commandProcessor.send(self._request) except RuntimeError: exception_raised = True # Now see if the circuit is broken following the failed call circuit_broken = False try: self._commandProcessor.send(self._request) except CircuitBrokenError: circuit_broken = True self.assertTrue(exception_raised, "Exepcted an exception to be raised, when we run out of retries") self.assertTrue(circuit_broken, "Expected the circuit to be broken to further calls") self.assertFalse(self._handler.called, "Did not expect the handle method on the handler to be called with the message") self.assertTrue(self._handler.call_count == 3, "Expected two retries of the pipeline")
class PostTests(unittest.TestCase): def setUp(self): self._messageMapperRegistry = MessageMapperRegistry() self._messageMapperRegistry.register(MyCommand, map_to_message) self._message_store = FakeMessageStore() self._producer = FakeProducer() self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=self._producer) def test_handle_command(self): """ given that we have a message mapper and producer registered for a commnd, when we post a command, it should send via the producer """ self._request = MyCommand() self._commandProcessor.post(self._request) self.assertTrue(self._message_store.message_was_added, "Expected a message to be added") self.assertTrue(self._message_store.get_message(self._request.id), "Expected the command to be converted into a message") self.assertTrue(self._producer.was_sent_message, "Expected a message to be sent via the producer") self.assertTrue( str(self._message_store.get_message(self._request.id).body), "") def test_missing_message_mapper(self): """given that we have no message mapper registered for a command when we post a command it should raise an error """ self._request = MyOtherCommand() was_exception_thrown = False try: self._commandProcessor.post(self._request) except ConfigurationException: was_exception_thrown = True # it looks as though we should use self_assertRaises... self.assertTrue(was_exception_thrown, "") def test_missing_message_producer(self): """given that we have no me message producer configured for the commandprocessor when we post a command it should raise a confiugration error """ self._commandProcessor = CommandProcessor( message_mapper_registry=self._messageMapperRegistry, message_store=self._message_store, producer=None) was_exception_thrown = False try: self._request = MyCommand() self._commandProcessor.post(self._request) except ConfigurationException: was_exception_thrown = True self.assertTrue(was_exception_thrown) def test_missing_message_mapper_registry(self): """ given that we have no message mapper registry for the commandprocessor when we post a command it should raise a configuration error """ self._commandProcessor = CommandProcessor( message_mapper_registry=None, message_store=self._message_store, producer=self._producer) was_exception_thrown = False try: self._request = MyCommand() self._commandProcessor.post(self._request) except ConfigurationException: was_exception_thrown = True self.assertTrue(was_exception_thrown)