def test_simulate_loop(): data = {} data['dateTime'] = 0 options = argparse.Namespace() options.type = 'driver' options.binding = 'loop' options.record_count = len(data) options.units = 'US' options.interval = random.randint(1, 99) options.delay = random.randint(1, 99) options.callback = random_string() options.topics = random_string() options.host = random_string() options.console = random_string() options.config_file = random_string() options.verbose = random_string() mock_driver = mock.Mock() data = {} data['dateTime'] = 0 mock_driver.genArchiveRecords.return_value = [data] mock_driver.genLoopPackets.return_value = [data] driver_loader_stub = DriverLoaderStub(mock_driver) with mock.patch('user.MQTTSubscribe.argparse') as mock_argparse: with mock.patch('user.MQTTSubscribe.print'): with mock.patch('user.MQTTSubscribe.weeutil'): with mock.patch('user.MQTTSubscribe.to_sorted_string'): with mock.patch('%s.%s' % (builtins_module_name, '__import__')): with mock.patch.dict( sys.modules, {'user.MQTTSubscribe': driver_loader_stub}): mock_parser = mock.Mock() mock_parser.parse_args.return_value = options mock_argparse.ArgumentParser.return_value = mock_parser SUT = user.MQTTSubscribe.Simulator() SUT.run() mock_driver.genLoopPackets.assert_called_once()
class Teston_connect(unittest.TestCase): unit_system_name = 'US' unit_system = test_weewx_stubs.UNITS_CONSTANTS[unit_system_name] config_dict = { 'console': False, 'keyword_delimiter': None, 'keyword_separator': None, 'host': 'host', 'keepalive': random.randint(1, 10), 'port': random.randint(1, 10), 'username': random_string(), 'password': random_string(), 'archive_topic': None } def test_multiple_topics(self): topic1 = random_string() topic2 = random_string() config_dict = dict(self.config_dict) config_dict['unit_system'] = self.unit_system_name config_dict['topics'] = {} config_dict['topics'][topic1] = {} config_dict['topics'][topic1]['subscribe'] = True config_dict['topics'][topic2] = {} config_dict['topics'][topic2]['subscribe'] = True config_dict['message_callback'] = {} config = configobj.ConfigObj(config_dict) subscribed_topics = dict(config_dict['topics']) qos = 0 mock_logger = mock.Mock(spec=Logger) with mock.patch('paho.mqtt.client.Client', spec=paho.mqtt.client.Client) as mock_client: with mock.patch('user.MQTTSubscribe.MessageCallbackProvider'): with mock.patch( 'user.MQTTSubscribe.TopicManager') as mock_manager: type(mock_manager.return_value ).subscribed_topics = mock.PropertyMock( return_value=subscribed_topics) type(mock_manager.return_value).get_qos = mock.Mock( return_value=qos) mock_client.subscribe.return_value = [1, 0] SUT = MQTTSubscriber(config, mock_logger) rc = random.randint(1, 10) userdata = {} userdata['connect'] = False SUT._on_connect( mock_client, userdata, None, rc, ) # pylint: disable=protected-access self.assertEqual(mock_client.subscribe.call_count, 2) mock_client.subscribe.assert_any_call(topic1, qos) mock_client.subscribe.assert_any_call(topic2, qos)
class TestGetWindQueueData(unittest.TestCase): topic = random_string() config_dict = {} config_dict['collect_wind_across_loops'] = False config_dict[topic] = {} config = configobj.ConfigObj(config_dict) fieldname = 'windSpeed' def create_queue_data(self): return { self.fieldname: random.uniform(1, 100), 'usUnits': 1, 'dateTime': time.time() } def test_queue_datetime_in_future(self): mock_logger = mock.Mock(spec=Logger) with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).get_data = mock.Mock( return_value={}) SUT = TopicManager(None, self.config, mock_logger) SUT.append_data(self.topic, self.create_queue_data(), fieldname=self.fieldname) gen = SUT.get_data(SUT.subscribed_topics[self.topic]['queue'], 0) data = next(gen, None) self.assertIsNone(data) def test_add_to_collector_returns_data(self): mock_logger = mock.Mock(spec=Logger) collected_data = self.create_queue_data() with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).get_data = mock.Mock( return_value={}) type(mock_CollectData.return_value).add_data = mock.Mock( return_value=collected_data) SUT = TopicManager(None, self.config, mock_logger) SUT.append_data(self.topic, self.create_queue_data(), fieldname=self.fieldname) # ToDo - need to get the topic a better way # perhaps find it by searching on subscribed topic 'type' gen = SUT.get_data( SUT.subscribed_topics[SUT.collected_topic]['queue']) data = next(gen, None) self.assertEqual(data, collected_data) data = next(gen, None) self.assertIsNone(data) def test_get_from_collector_returns_data(self): mock_logger = mock.Mock(spec=Logger) collected_data = self.create_queue_data() with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).get_data = mock.Mock( return_value=collected_data) SUT = TopicManager(None, self.config, mock_logger) gen = SUT.get_data(SUT.subscribed_topics[self.topic]['queue'], 0) data = next(gen, None) self.assertEqual(data, collected_data) data = next(gen, None) self.assertIsNone(data) @staticmethod def test_get_from_collector_not_called(): mock_logger = mock.Mock(spec=Logger) topic = random_string() config_dict = {} config_dict[topic] = {} config = configobj.ConfigObj(config_dict) with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: SUT = TopicManager(None, config, mock_logger) gen = SUT.get_data(SUT.subscribed_topics[topic]['queue'], 0) next(gen, None) mock_CollectData.get_data.assert_not_called()
class TestGetQueueData(unittest.TestCase): topic = random_string() config_dict = {} config_dict[topic] = {} config = configobj.ConfigObj(config_dict) @staticmethod def create_queue_data(): return { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, 'dateTime': time.time() } def test_queue_empty(self): mock_logger = mock.Mock(spec=Logger) with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).get_data = mock.Mock( return_value={}) SUT = TopicManager(None, self.config, mock_logger) gen = SUT.get_data(SUT.subscribed_topics[self.topic]['queue']) data = next(gen, None) self.assertIsNone(data) def test_queue_datetime_in_future(self): mock_logger = mock.Mock(spec=Logger) with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).get_data = mock.Mock( return_value={}) SUT = TopicManager(None, self.config, mock_logger) SUT.append_data(self.topic, self.create_queue_data()) gen = SUT.get_data(SUT.subscribed_topics[self.topic]['queue'], 0) data = next(gen, None) self.assertIsNone(data) def test_queue_good(self): mock_logger = mock.Mock(spec=Logger) with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).get_data = mock.Mock( return_value={}) SUT = TopicManager(None, self.config, mock_logger) elem_one = self.create_queue_data() elem_two = self.create_queue_data() elem_three = self.create_queue_data() SUT.append_data(self.topic, elem_one) SUT.append_data(self.topic, elem_two) SUT.append_data(self.topic, elem_three) elements = [] for data in SUT.get_data( SUT.subscribed_topics[self.topic]['queue']): elements.append(data) self.assertEqual(len(elements), 3) self.assertDictEqual(elements[0], elem_one) self.assertDictEqual(elements[1], elem_two) self.assertDictEqual(elements[2], elem_three) def test_wind_queue_good(self): mock_logger = mock.Mock(spec=Logger) with mock.patch('user.MQTTSubscribe.CollectData') as mock_CollectData: type(mock_CollectData.return_value).add_data = mock.Mock( return_value={}) type(mock_CollectData.return_value).get_data = mock.Mock( return_value={}) SUT = TopicManager(None, self.config, mock_logger) collector_topic = "" for topic in SUT.subscribed_topics: if SUT.subscribed_topics[topic]['queue'][ 'type'] == 'collector': collector_topic = topic break fieldname = 'windSpeed' data = { fieldname: random.uniform(1, 100), 'usUnits': 1, 'dateTime': time.time() } SUT.append_data(collector_topic, data, fieldname) elements = [] for data in SUT.get_data( SUT.subscribed_topics[collector_topic]['queue']): elements.append(data) self.assertEqual(len(elements), 0)
class TestAppendData(unittest.TestCase): topic = random_string() config_dict = {} config_dict[topic] = {} config = configobj.ConfigObj(config_dict) def test_append_good_data(self): queue_data = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, 'dateTime': time.time() } mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, self.config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertDictEqual(data, queue_data) def test_append_good_data_use_server_datetime(self): queue_data_subset = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, } queue_data = copy.deepcopy(queue_data_subset) queue_data['dateTime'] = time.time() config = copy.deepcopy(self.config) config['use_server_datetime'] = True mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertGreaterEqual(data.items(), queue_data_subset.items()) self.assertIn('dateTime', data) self.assertNotEqual(queue_data['dateTime'], data['dateTime']) def test_missing_datetime(self): queue_data = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1 } mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, self.config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertGreaterEqual(data.items(), queue_data.items()) self.assertIn('dateTime', data) def test_missing_units(self): queue_data = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'dateTime': time.time() } mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, self.config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertGreaterEqual(data.items(), queue_data.items()) self.assertIn('usUnits', data) def test_dateteime_format_add_offset(self): # pylint: disable=too-many-locals queue_data_subset = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, } queue_data = copy.deepcopy(queue_data_subset) datetime_format = "%Y-%m-%d %H:%M:%S" offset_format = "hhmm" current_epoch = int(time.time()) offset_hour = random.randint(1, 23) offset_minute = random.randint(1, 59) offset_hour_str = str(offset_hour).rjust(2, '0') offset_minute_str = str(offset_minute).rjust(2, '0') offset_str = "%s%s" % (offset_hour_str, offset_minute_str) current_datetime = datetime.datetime.fromtimestamp( current_epoch).strftime(datetime_format) queue_data['dateTime'] = "%s+%s" % (current_datetime, offset_str) adjusted_epoch = current_epoch + (offset_hour * 60 + offset_minute) * 60 config = copy.deepcopy(self.config) config['datetime_format'] = datetime_format config['offset_format'] = offset_format mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertGreaterEqual(data.items(), queue_data_subset.items()) self.assertIn('dateTime', data) self.assertEqual(adjusted_epoch, data['dateTime']) def test_dateteime_format_subtract_offset(self): # pylint: disable=too-many-locals queue_data_subset = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, } queue_data = copy.deepcopy(queue_data_subset) datetime_format = "%B %d %Y %H:%M:%S" offset_format = "hh:mm" current_epoch = int(time.time()) offset_hour = random.randint(1, 23) offset_minute = random.randint(1, 59) offset_hour_str = str(offset_hour).rjust(2, '0') offset_minute_str = str(offset_minute).rjust(2, '0') offset_str = "%s:%s" % (offset_hour_str, offset_minute_str) current_datetime = datetime.datetime.fromtimestamp( current_epoch).strftime(datetime_format) queue_data['dateTime'] = "%s -%s" % (current_datetime, offset_str) adjusted_epoch = current_epoch - (offset_hour * 60 + offset_minute) * 60 config = copy.deepcopy(self.config) config['datetime_format'] = datetime_format config['offset_format'] = offset_format mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertGreaterEqual(data.items(), queue_data_subset.items()) self.assertIn('dateTime', data) self.assertEqual(adjusted_epoch, data['dateTime']) def test_dateteime_format_no_offset(self): queue_data_subset = { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, } queue_data = copy.deepcopy(queue_data_subset) datetime_format = "%Y-%m-%d %H:%M:%S" current_epoch = int(time.time()) current_datetime = datetime.datetime.fromtimestamp( current_epoch).strftime(datetime_format) queue_data['dateTime'] = current_datetime adjusted_epoch = current_epoch config = copy.deepcopy(self.config) config['datetime_format'] = datetime_format mock_logger = mock.Mock(spec=Logger) SUT = TopicManager(None, config, mock_logger) SUT.append_data(self.topic, queue_data) queue = SUT._get_queue(self.topic)['data'] # pylint: disable=protected-access self.assertEqual(len(queue), 1) queue_element = queue.popleft() data = queue_element['data'] self.assertGreaterEqual(data.items(), queue_data_subset.items()) self.assertIn('dateTime', data) self.assertEqual(adjusted_epoch, data['dateTime'])
def test_no_default_setting(self): mock_logger = mock.Mock(spec=Logger) topic = random_string() config_key = random_string(10) config_dict = {} config_dict['message'] = {} config_dict['message']['type'] = random_string(10) config_dict['message']['flatten_delimiter'] = random_string(5) config_dict['message']['keyword_delimiter'] = random_string(5) config_dict['message']['keyword_separator'] = random_string(5) config_dict[topic] = {} config_dict[topic]['message'] = {} config_dict[topic]['message']['type'] = random_string(10) config_dict[topic]['message']['flatten_delimiter'] = random_string(5) config_dict[topic]['message']['keyword_delimiter'] = random_string(5) config_dict[topic]['message']['keyword_separator'] = random_string(5) config_dict[topic]['message'][config_key] = random_string() config = configobj.ConfigObj(config_dict) SUT = TopicManager(None, config, mock_logger) self.assertIn(SUT.message_config_name, SUT.subscribed_topics[topic]) self.assertEqual( SUT.subscribed_topics[topic][SUT.message_config_name]['type'], config_dict[topic]['message']['type']) self.assertEqual( SUT.subscribed_topics[topic][ SUT.message_config_name]['flatten_delimiter'], config_dict[topic]['message']['flatten_delimiter']) self.assertEqual( SUT.subscribed_topics[topic][ SUT.message_config_name]['keyword_delimiter'], config_dict[topic]['message']['keyword_delimiter']) self.assertEqual( SUT.subscribed_topics[topic][ SUT.message_config_name]['keyword_separator'], config_dict[topic]['message']['keyword_separator']) self.assertEqual( SUT.subscribed_topics[topic][SUT.message_config_name][config_key], config_dict[topic]['message'][config_key])
class TestAccumulatedData(unittest.TestCase): topic = random_string() config_dict = {} config_dict[topic] = {} config = configobj.ConfigObj(config_dict) @staticmethod def create_queue_data(): return { 'inTemp': random.uniform(1, 100), 'outTemp': random.uniform(1, 100), 'usUnits': 1, 'dateTime': time.time() } def test_ignore_start_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() } start_ts = time.time() end_ts = time.time() adjust_start_time = 1 config = copy.deepcopy(self.config) config['ignore_start_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': start_ts}) accumulated_data = SUT.get_accumulated_data( SUT.subscribed_topics[self.topic]['queue'], 0, end_ts, 0) mock_Accum.assert_called_once_with( test_weewx_stubs.weeutil.weeutil.TimeSpan( start_ts - adjust_start_time, end_ts)) self.assertDictEqual(accumulated_data, final_record_data) def test_ignore_start_set_and_adjusted(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() } start_ts = time.time() end_ts = time.time() adjust_start_time = random.randint(2, 9) config = copy.deepcopy(self.config) config['ignore_start_time'] = True config['adjust_start_time'] = adjust_start_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, config, mock_logger) SUT.append_data(self.topic, {'dateTime': start_ts}) accumulated_data = SUT.get_accumulated_data( SUT.subscribed_topics[self.topic]['queue'], 0, end_ts, 0) mock_Accum.assert_called_once_with( test_weewx_stubs.weeutil.weeutil.TimeSpan( start_ts - adjust_start_time, end_ts)) self.assertDictEqual(accumulated_data, final_record_data) 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 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(None, self.config, mock_logger) SUT.append_data(self.topic, queue_data) mock_logger.reset_mock() accumulated_data = SUT.get_accumulated_data( SUT.subscribed_topics[self.topic]['queue'], 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() 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(None, self.config, mock_logger) accumulated_data = SUT.get_accumulated_data( SUT.subscribed_topics[self.topic]['queue'], 0, time.time(), 0) self.assertDictEqual(accumulated_data, {}) mock_Accum.assert_not_called() mock_Accum.addRecord.assert_not_called() mock_Accum.getRecord.assert_not_called() mock_to_std_system.assert_not_called() 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)
def test_init_console(self): config_dict = {} binding = 'loop' MQTTSubscribeService_binding_config = { 'MQTTSubscribeService': { 'binding': binding } } MQTTSubscribeService_console_config = { 'MQTTSubscribeService': { 'console': True } } MQTTSubscribeDriver_console_config = { 'MQTTSubscribeDriver': { 'console': True } } options = argparse.Namespace() options.binding = binding options.callback = None options.topics = None options.host = None options.console = random_string() options.type = None options.record_count = random.randint(1, 99) options.interval = random.randint(1, 99) options.delay = random.randint(1, 99) options.units = None options.config_file = random_string() options.verbose = random_string() with mock.patch('user.MQTTSubscribe.argparse') as mock_argparse: with mock.patch('user.MQTTSubscribe.print'): with mock.patch('user.MQTTSubscribe.os'): with mock.patch( 'user.MQTTSubscribe.configobj') as mock_configobj: with mock.patch('user.MQTTSubscribe.weeutil'): with mock.patch('user.MQTTSubscribe.merge_config' ) as mock_merge_config: mock_parser = mock.Mock() mock_parser.parse_args.return_value = options mock_argparse.ArgumentParser.return_value = mock_parser mock_configobj.ConfigObj.return_value = config_dict SUT = user.MQTTSubscribe.Simulator() SUT.init_configuration() call_args_list = mock_merge_config.call_args_list self.assertEqual(len(call_args_list), 3) self.assertEqual(call_args_list[0].args[0], config_dict) self.assertEqual( call_args_list[0].args[1], MQTTSubscribeService_binding_config) self.assertEqual(call_args_list[1].args[0], config_dict) self.assertEqual( call_args_list[1].args[1], MQTTSubscribeService_console_config) self.assertEqual(call_args_list[2].args[0], config_dict) self.assertEqual( call_args_list[2].args[1], MQTTSubscribeDriver_console_config)
def test_init_topics(self): # pylint: disable=too-many-locals config_dict = { 'MQTTSubscribeDriver': { 'topics': {} }, 'MQTTSubscribeService': { 'topics': {} } } binding = 'loop' topic_count = random.randint(1, 5) MQTTSubscribeService_binding_config = { 'MQTTSubscribeService': { 'binding': binding } } topics = [] i = 0 while i < topic_count: topics.append(random_string()) i += 1 topics_string = ','.join(topics) options = argparse.Namespace() options.binding = binding options.callback = None options.topics = topics_string options.host = None options.console = None options.type = None options.record_count = random.randint(1, 99) options.interval = random.randint(1, 99) options.delay = random.randint(1, 99) options.units = None options.config_file = random_string() options.verbose = random_string() with mock.patch('user.MQTTSubscribe.argparse') as mock_argparse: with mock.patch('user.MQTTSubscribe.print'): with mock.patch('user.MQTTSubscribe.os'): with mock.patch( 'user.MQTTSubscribe.configobj') as mock_configobj: with mock.patch('user.MQTTSubscribe.weeutil'): with mock.patch('user.MQTTSubscribe.merge_config' ) as mock_merge_config: mock_parser = mock.Mock() mock_parser.parse_args.return_value = options mock_argparse.ArgumentParser.return_value = mock_parser mock_configobj.ConfigObj.return_value = config_dict SUT = user.MQTTSubscribe.Simulator() SUT.init_configuration() call_args_list = mock_merge_config.call_args_list self.assertEqual(len(call_args_list), 1 + (2 * topic_count)) self.assertEqual(call_args_list[0].args[0], config_dict) self.assertEqual( call_args_list[0].args[1], MQTTSubscribeService_binding_config) i = 0 while i < topic_count: MQTTSubscribeService_topic_config = { 'MQTTSubscribeService': { 'topics': { topics[i]: {} } } } MQTTSubscribeDriver_topic_config = { 'MQTTSubscribeDriver': { 'topics': { topics[i]: {} } } } self.assertEqual( call_args_list[2 * i + 1].args[0], config_dict) self.assertEqual( call_args_list[2 * i + 1].args[1], MQTTSubscribeService_topic_config) self.assertEqual( call_args_list[2 * i + 2].args[0], config_dict) self.assertEqual( call_args_list[2 * i + 2].args[1], MQTTSubscribeDriver_topic_config) i += 1
def test_key_not_in_cache(self): SUT = RecordCache() value = SUT.get_value(random_string(), 0, None) self.assertIsNone(value)