示例#1
0
    def test_empty_payload(self):
        mock_logger = mock.Mock(spec=Logger)
        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      None)

        msg = Msg(self.topic, '', 0, 0)

        SUT._on_message_individual(None, None, msg)
        self.assertEqual(mock_logger.error.call_count, 2)
示例#2
0
    def test_single_topic(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      mock_manager)

        payload = round(random.uniform(1, 100), 2)
        msg = Msg(self.single_topic, str(payload), 0, 0)

        SUT._on_message_individual(None, None, msg)
        mock_manager.append_data.assert_called_once_with(
            msg.topic, {self.fieldname: payload})
示例#3
0
    def test_None_payload(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      mock_manager)

        payload = None
        msg = Msg(self.topic, payload, 0, 0)

        SUT._on_message_individual(None, None, msg)

        mock_manager.append_data.assert_called_once_with(
            msg.topic, {self.fieldname: None})
示例#4
0
    def test_None_payload(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

        message_handler_config = dict(self.message_handler_config)
        message_handler_config['full_topic_fieldname'] = True

        SUT = MessageCallbackProvider(message_handler_config, mock_logger,
                                      mock_manager)

        msg = Msg(self.topic, None, 0, 0)

        SUT._on_message_individual(None, None, msg)
        mock_manager.append_data.assert_called_once_with(
            msg.topic, {self.topic: None})
示例#5
0
    def test_multiple_topics(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

        message_handler_config = dict(self.message_handler_config)
        message_handler_config['full_topic_fieldname'] = True

        SUT = MessageCallbackProvider(message_handler_config, mock_logger,
                                      mock_manager)

        payload = round(random.uniform(1, 100), 2)
        msg = Msg(self.multi_topic, str(payload), 0, 0)

        SUT._on_message_individual(None, None, msg)
        mock_manager.append_data.assert_called_once_with(
            msg.topic, {self.multi_topic: payload})
示例#6
0
    def test_bad_payload(self):
        mock_logger = mock.Mock(spec=Logger)
        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      None)

        msg = Msg(
            self.topic,
            ''.join([
                random.choice(string.ascii_letters + string.digits)
                for n in range(32)
            ]),  # pylint: disable=unused-variable
            0,
            0)

        SUT._on_message_individual(None, None, msg)
        self.assertEqual(mock_logger.error.call_count, 2)
示例#7
0
    def test_unicode_topic(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)
        if PY2:
            topic = unicode(self.topic)  # (never called under python 3) pylint: disable=undefined-variable
        else:
            topic = self.topic

        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      mock_manager)

        payload = round(random.uniform(1, 100), 2)
        msg = Msg(topic, str(payload), 0, 0)

        SUT._on_message_individual(None, None, msg)
        mock_manager.append_data.assert_called_once_with(
            msg.topic, {self.fieldname: payload})

        call_args_list = mock_manager.append_data.call_args_list
        second_arg = call_args_list[0].args[1]
        for key in second_arg:
            self.assertIsInstance(key, str)