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_packet_is_in_past(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_queue_tests(start_ts, end_period_ts) self.final_packet_data.update(self.target_data) new_loop_packet_event = test_weewx_stubs.Event( test_weewx_stubs.NEW_LOOP_PACKET, packet=self.packet_data) with mock.patch('user.MQTTSubscribe.MQTTSubscribe') as mock_manager: with mock.patch('user.MQTTSubscribe.Logger'): # pylint: disable=no-member 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 = end_period_ts + 10 SUT.new_loop_packet(new_loop_packet_event) SUT.logger.error.assert_called_once() SUT.shutDown()
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_queue_tests(start_ts, end_period_ts) self.final_packet_data.update(self.target_data) new_loop_packet_event = test_weewx_stubs.Event( test_weewx_stubs.NEW_LOOP_PACKET, packet=self.packet_data) 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_loop_packet(new_loop_packet_event) self.assertDictEqual(new_loop_packet_event.packet, self.final_packet_data) SUT.shutDown()
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()
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)
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']) testruns = test_data['testruns'] cdict = config_dict['MQTTSubscribeService'] if not 'message_callback' in config_dict['MQTTSubscribeService']: config_dict['MQTTSubscribeService']['message_callback'] = {} config_dict['MQTTSubscribeService']['message_callback'][ 'type'] = payload unit_system_name = cdict['topics'].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] min_config_dict = { 'Station': { 'altitude': [0, 'foot'], 'latitude': 0, 'station_type': 'Simulator', 'longitude': 0 }, 'Simulator': { 'driver': 'weewx.drivers.simulator', }, 'Engine': { 'Services': {} } } engine = StdEngine(min_config_dict) service = MQTTSubscribeService(engine, config_dict) host = 'localhost' port = 1883 keepalive = 60 userdata = { 'topics': [], 'connected_flag': False, } client = mqtt.Client(userdata=userdata) client.on_connect = utils.on_connect client.connect(host, port, keepalive) client.loop_start() max_connect_wait = 1 # ToDo - configure i = 1 while not userdata['connected_flag']: if i > max_connect_wait: self.fail("Timed out waiting for connections.") time.sleep(1) i += 1 userdata2 = { 'topics': cdict['topics'].sections, 'connected_flag': False, 'msg': False, 'max_msg_wait': 1 #ToDo - configure } client2 = mqtt.Client(userdata=userdata2) client2.on_connect = utils.on_connect client2.on_message = utils.on_message client2.connect(host, port, keepalive) client2.loop_start() max_connect2_wait = 1 # ToDo - configure i = 1 while not userdata2['connected_flag']: if i > max_connect2_wait: self.fail("Timed out waiting for connection 2.") time.sleep(1) i += 1 max_waits = 10 for testrun in testruns: for topics in testrun['messages']: for topic in topics: topic_info = topics[topic] msg_count = utils.send_msg(utils.send_mqtt_msg, payload, client.publish, topic, topic_info, userdata2, self) utils.wait_on_queue(service, msg_count, max_waits, 1) record = {} interval = 300 current_time = int(time.time() + 0.5) end_period_ts = (int(current_time / interval) + 1) * interval record['dateTime'] = end_period_ts record['usUnits'] = unit_system new_loop_packet_event = weewx.Event(weewx.NEW_LOOP_PACKET, packet=record) service.new_loop_packet(new_loop_packet_event) records = [record] if check_results: results = testrun['results'] result = {} found = False for result in results: if 'service' 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) service.shutDown() client.disconnect() client2.disconnect()