Example #1
0
    def test_queue_valid(self):
        mock_logger = mock.Mock(spec=Logger)

        final_record_data = {
            'inTemp': random.uniform(1, 100),
            'outTemp': random.uniform(1, 100),
            'usUnits': random.uniform(0, 2),
            'interval': 5,
            'dateTime': time.time()
        }

        with mock.patch('user.MQTTSubscribe.weewx.accum.Accum') as mock_Accum:
            with mock.patch('user.MQTTSubscribe.weewx.units.to_std_system'
                            ) as mock_to_std_system:
                type(mock_Accum.return_value).isEmpty = mock.PropertyMock(
                    return_value=False)
                mock_to_std_system.return_value = final_record_data

                SUT = TopicManager(None, self.config, mock_logger)
                SUT.append_data(self.topic, {})

                accumulated_data = SUT.get_accumulated_data(
                    SUT.subscribed_topics[self.topic]['queue'], 0, time.time(),
                    0)

                self.assertDictEqual(accumulated_data, final_record_data)
Example #2
0
    def test_ignore_end_set(self):
        mock_logger = mock.Mock(spec=Logger)

        final_record_data = {
            'inTemp': random.uniform(1, 100),
            'outTemp': random.uniform(1, 100),
            'usUnits': random.uniform(0, 2),
            'interval': 5,
            'dateTime': time.time()
        }

        end_ts = time.time()

        config = copy.deepcopy(self.config)
        config['ignore_end_time'] = True

        with mock.patch('user.MQTTSubscribe.weewx.accum.Accum') as mock_Accum:
            with mock.patch('user.MQTTSubscribe.weewx.units.to_std_system'
                            ) as mock_to_std_system:
                type(mock_Accum.return_value).isEmpty = mock.PropertyMock(
                    return_value=False)
                mock_to_std_system.return_value = final_record_data

                SUT = TopicManager(None, config, mock_logger)
                SUT.append_data(self.topic, {'dateTime': end_ts})

                accumulated_data = SUT.get_accumulated_data(
                    SUT.subscribed_topics[self.topic]['queue'], 0, 0, 0)

                mock_Accum.assert_called_once_with(
                    test_weewx_stubs.weeutil.weeutil.TimeSpan(0, end_ts))
                self.assertDictEqual(accumulated_data, final_record_data)
    def runit(self, payload, file_pointer, check_results=True):
        test_data = json.load(file_pointer, object_hook=utils.byteify)
        config_dict = configobj.ConfigObj(
            test_data['config'])['MQTTSubscribeService']
        testruns = test_data['testruns']

        logger = Logger('IntegTest')
        topics_dict = config_dict.get('topics', {})
        manager = TopicManager(topics_dict, logger)

        unit_system_name = topics_dict.get('unit_system', 'US').strip().upper()
        if unit_system_name not in weewx.units.unit_constants:
            raise ValueError("MQTTSubscribe: Unknown unit system: %s" %
                             unit_system_name)
        unit_system = weewx.units.unit_constants[unit_system_name]

        on_message = utils.get_callback(payload, config_dict, manager, logger)
        for testrun in testruns:
            start_ts = time.time()
            for topics in testrun['messages']:
                for topic in topics:
                    topic_info = topics[topic]
                    utils.send_msg(utils.send_direct_msg, payload, on_message,
                                   topic, topic_info)

            end_ts = time.time()
            records = []
            for topic in sorted(
                    manager.subscribed_topics
            ):  # todo - dependent on topic names - not great
                data = manager.get_accumulated_data(topic, start_ts, end_ts,
                                                    unit_system)
                records.append(data)

            if check_results:
                results = testrun['results']
                result = {}
                found = False
                for result in results:
                    if 'accumulate' in result['test']:
                        if payload in result['payloads']:
                            found = True
                            break
                self.assertTrue(found, "No results for %s" % payload)

                utils.check(self, payload, records, result['records'])
            else:
                for record in records:
                    print(record)
    def test_queue_empty(self):
        mock_logger = mock.Mock(spec=Logger)

        with mock.patch('user.MQTTSubscribe.weewx.accum.Accum') as mock_Accum:
            with mock.patch('user.MQTTSubscribe.weewx.units.to_std_system'
                            ) as mock_to_std_system:
                type(mock_Accum.return_value).isEmpty = mock.PropertyMock(
                    return_value=True)

                SUT = TopicManager(self.config, mock_logger)

                accumulated_data = SUT.get_accumulated_data(
                    self.topic, 0, time.time(), 0)

                self.assertDictEqual(accumulated_data, {})
                mock_Accum.addRecord.assert_not_called()
                mock_Accum.getRecord.assert_not_called()
                mock_to_std_system.assert_not_called()
Example #5
0
    def test_queue_element_before_start(self):
        mock_logger = mock.Mock(spec=Logger)
        queue_data = self.create_queue_data()

        with mock.patch('user.MQTTSubscribe.weewx.accum.Accum') as mock_Accum:
            with mock.patch('user.MQTTSubscribe.weewx.units.to_std_system'
                            ) as mock_to_std_system:
                type(mock_Accum.return_value).addRecord = \
                    mock.Mock(side_effect=test_weewx_stubs.weewx.accum.OutOfSpan("Attempt to add out-of-interval record"))

                SUT = TopicManager(self.config, mock_logger)
                SUT.append_data(self.topic, queue_data)

                mock_logger.reset_mock()
                accumulated_data = SUT.get_accumulated_data(
                    self.topic, 0, time.time(), 0)

                self.assertDictEqual(accumulated_data, {})
                mock_logger.info.assert_called_once()
                mock_Accum.getRecord.assert_not_called()
                mock_to_std_system.assert_not_called()
Example #6
0
    def runit(self, payload, file_pointer):
        test_data = json.load(file_pointer, object_hook=utils.byteify)
        config_dict = configobj.ConfigObj(
            test_data['config'])['MQTTSubscribeService']
        testruns = test_data['testruns']

        logger = Logger()
        topics_dict = config_dict.get('topics', {})
        manager = TopicManager(topics_dict, logger)

        on_message = utils.get_callback(payload, config_dict, manager, logger)
        for testrun in testruns:
            start_ts = time.time()
            for topics in testrun['messages']:
                for topic in topics:
                    topic_info = topics[topic]
                    utils.send_msg(utils.send_direct_msg, payload, on_message,
                                   topic, topic_info)

            end_ts = time.time()
            results = testrun['results']
            result = {}
            found = False
            for result in results:
                if 'accumulate' in result['test']:
                    if payload in result['payloads']:
                        found = True
                        break

            self.assertTrue(found, "No results for %s" % payload)

            records = []
            for topic in sorted(
                    manager.subscribed_topics
            ):  # todo - dependent on topic names - not great
                data = manager.get_accumulated_data(topic, start_ts, end_ts,
                                                    result['units'])
                records.append(data)

            utils.check(self, payload, records, result['records'])