Exemple #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)
Exemple #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)
Exemple #3
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_json(None, None, msg)
        self.assertEqual(mock_logger.error.call_count, 2)
Exemple #4
0
    def test_invalid_json(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_json(None, None, msg)
        self.assertEqual(mock_logger.error.call_count, 2)
Exemple #5
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)