Exemplo n.º 1
0
    def test_field_exists(self):
        unit_system = random.randint(1, 10)
        fieldname = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        config_dict = {}
        config_dict['MQTTSubscribeService'] = {
            'archive_field_cache': {
                'fields': {
                    fieldname: {}
                }
            }
        }

        config = configobj.ConfigObj(config_dict)

        with mock.patch('user.MQTTSubscribe.MQTTSubscribe'):
            with mock.patch('user.MQTTSubscribe.RecordCache'):
                # pylint: disable=no-member
                SUT = MQTTSubscribeService(self.mock_StdEngine, config)

                record = {
                    'usUnits': unit_system,
                    'dateTime': time.time(),
                    fieldname: round(random.uniform(1, 100), 2)
                }

                event = test_weewx_stubs.Event(
                    test_weewx_stubs.NEW_ARCHIVE_RECORD, record=record)

                SUT.new_archive_record(event)
                SUT.cache.update_value.assert_called_once()
    def test_queue_valid(self):
        topic = 'foo/bar'
        current_time = int(time.time() + 0.5)
        end_period_ts = (int(current_time / 300) + 1) * 300
        start_ts = end_period_ts - 300
        self.setup_archive_queue_tests(start_ts, end_period_ts, topic)
        self.final_record_data.update(self.target_data)

        new_loop_record_event = test_weewx_stubs.Event(
            test_weewx_stubs.NEW_ARCHIVE_RECORD,
            record=self.record_data,
            origin='hardware')

        with mock.patch('user.MQTTSubscribe.MQTTSubscribe') as mock_manager:
            type(mock_manager.return_value
                 ).subscribed_topics = mock.PropertyMock(return_value=[topic])
            type(mock_manager.return_value).get_accumulated_data = mock.Mock(
                return_value=self.target_data)

            SUT = MQTTSubscribeService(self.mock_StdEngine, self.config_dict)
            SUT.end_ts = start_ts

            SUT.new_archive_record(new_loop_record_event)

            self.assertDictEqual(new_loop_record_event.record,
                                 self.final_record_data)

        SUT.shutDown()
Exemplo n.º 3
0
    def test_field_missing(self):
        unit_system = random.randint(1, 10)
        fieldname = ''.join([
            random.choice(string.ascii_letters + string.digits)
            for n in range(32)
        ])
        config_dict = {}
        config_dict['MQTTSubscribeService'] = {
            'archive_field_cache': {
                'fields': {
                    fieldname: {}
                }
            }
        }

        config = configobj.ConfigObj(config_dict)

        with mock.patch('user.MQTTSubscribe.MQTTSubscribe'):
            with mock.patch('user.MQTTSubscribe.RecordCache') as mock_cache:
                value = round(random.uniform(10, 100), 2)
                type(mock_cache.return_value).get_value = mock.Mock(
                    return_value=value)
                # pylint: disable=no-member
                SUT = MQTTSubscribeService(self.mock_StdEngine, config)

                record = {'usUnits': unit_system, 'dateTime': time.time()}

                event = test_weewx_stubs.Event(
                    test_weewx_stubs.NEW_ARCHIVE_RECORD, record=record)

                updated_record = copy.deepcopy(record)
                updated_record.update({fieldname: value})

                SUT.new_archive_record(event)
                SUT.cache.get_value.assert_called_once()
                self.assertEqual(record, updated_record)