Exemplo n.º 1
0
    def test_payload_nested(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

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

        payload_dict = {
            'nested01': {
                'inTemp': round(random.uniform(1, 100), 2),
                'outTemp': round(random.uniform(1, 100), 2)
            }
        }

        payload_dict['dateTime'] = time.time()
        payload_dict['usUnits'] = random.randint(1, 10)

        flattened_payload_dict = {
            'nested01_inTemp': payload_dict['nested01']['inTemp'],
            'nested01_outTemp': payload_dict['nested01']['outTemp']
        }
        flattened_payload_dict['dateTime'] = payload_dict['dateTime']
        flattened_payload_dict['usUnits'] = payload_dict['usUnits']

        if PY2:
            payload = json.dumps(payload_dict)
        else:
            payload = json.dumps(payload_dict).encode("utf-8")

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

        SUT._on_message_json(None, None, msg)

        mock_manager.append_data.assert_called_once_with(
            msg.topic, flattened_payload_dict)
Exemplo n.º 2
0
    def test_payload_good(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

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

        payload_dict = dict(self.payload_dict)
        payload_dict['dateTime'] = time.time()
        payload_dict['usUnits'] = random.randint(1, 10)

        if PY2:
            payload = json.dumps(payload_dict)
        else:
            payload = json.dumps(payload_dict).encode("utf-8")

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

        SUT._on_message_json(None, None, msg)

        mock_manager.append_data.assert_called_once_with(
            msg.topic, payload_dict)
        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)
Exemplo n.º 3
0
    def test_payload_good(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

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

        payload_dict = dict(self.payload_dict)
        payload_dict['dateTime'] = round(time.time(), 2)
        payload_dict['usUnits'] = random.randint(1, 10)

        payload_str = ""
        delim = ""
        for key in payload_dict:
            payload_str = "%s%s%s=%f" % (payload_str, delim, key,
                                         payload_dict[key])
            delim = ","

        payload_str = payload_str.encode('UTF-8')

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

        SUT._on_message_keyword(None, None, msg)
        mock_manager.append_data.assert_called_once_with(
            msg.topic, payload_dict)
Exemplo n.º 4
0
    def test_get_keyword_payload_type(self):
        message_handler_config = {}
        message_handler_config['type'] = 'keyword'

        SUT = MessageCallbackProvider(message_handler_config, None, None)

        callback = SUT.get_callback()
        self.assertEqual(callback, SUT._on_message_keyword)
Exemplo n.º 5
0
    def test_payload_bad_data(self):
        mock_logger = mock.Mock(spec=Logger)
        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      None)

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

        SUT._on_message_keyword(None, None, msg)
        self.assertEqual(mock_logger.error.call_count, 2)
Exemplo n.º 6
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)
Exemplo n.º 7
0
    def test_payload_empty(self):
        mock_logger = mock.Mock(spec=Logger)
        SUT = MessageCallbackProvider(self.message_handler_config, mock_logger,
                                      None)

        payload = ''
        payload = payload.encode('UTF-8')

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

        SUT._on_message_keyword(None, None, msg)
        self.assertEqual(mock_logger.error.call_count, 3)
Exemplo n.º 8
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})
Exemplo n.º 9
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})
Exemplo n.º 10
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})
Exemplo n.º 11
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})
Exemplo n.º 12
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)
Exemplo n.º 13
0
    def test_get_unknown_payload_type(self):
        message_handler_config = {}
        payload_type = 'foobar'
        message_handler_config['type'] = payload_type

        with self.assertRaises(ValueError) as context:
            MessageCallbackProvider(message_handler_config, None, None)

        self.assertEqual(str(context.exception),
                         "Invalid type configured: %s" % payload_type)
Exemplo n.º 14
0
    def test_missing_units(self):
        mock_manager = mock.Mock(spec=TopicManager)
        mock_logger = mock.Mock(spec=Logger)

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

        payload_dict = dict(self.payload_dict)
        payload_dict['dateTime'] = time.time()

        if PY2:
            payload = json.dumps(payload_dict)
        else:
            payload = json.dumps(payload_dict).encode("utf-8")

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

        SUT._on_message_json(None, None, msg)

        mock_manager.append_data.assert_called_once_with(
            msg.topic, payload_dict)
Exemplo n.º 15
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)