def test_minimum_configuration(self):
        mock_StdEngine = mock.Mock()
        server_url = random_string()

        config_dict = {
            'StdRESTful': {
                'MQTTPublish': {
                    'server_url': server_url,
                }
            }
        }
        config = configobj.ConfigObj(config_dict)

        manager_dict = {random_string(): random_string()}

        topics = {
            'weather/loop': self.create_topic(),
            'weather': self.create_topic(payload_type='individual')
        }

        site_dict = copy.deepcopy(config_dict['StdRESTful']['MQTTPublish'])
        site_config = configobj.ConfigObj(site_dict)

        site_dict_final = {
            'server_url': server_url,
            'topics': topics,
            'manager_dict': manager_dict
        }
        site_config_final = configobj.ConfigObj(site_dict_final)

        with mock.patch('weewx.restx') as mock_restx:
            with mock.patch('weewx.manager') as mock_manager:
                with mock.patch('weewx.manager.open_manager'):
                    with mock.patch(
                            'user.mqttpublish.MQTTPublish.bind') as mock_bind:
                        with mock.patch('user.mqttpublish.loginf'):
                            with mock.patch(
                                    'user.mqttpublish.MQTTPublishThread'
                            ) as mock_MQTTThread:
                                mock_restx.get_site_dict.return_value = site_config
                                mock_manager.get_manager_dict_from_config.return_value = manager_dict

                                SUT = MQTTPublish(mock_StdEngine, config)

                                mock_manager.get_manager_dict_from_config.assert_called_once_with(
                                    config, 'wx_binding')
                                mock_manager.open_manager.assert_called_once_with(
                                    manager_dict)

                                call_args_list = mock_bind.call_args_list
                                self.assertEqual(len(call_args_list), 1)
                                self.assertEqual(call_args_list[0].args[0],
                                                 NEW_ARCHIVE_RECORD)
                                self.assertEqual(call_args_list[0].args[1],
                                                 SUT.new_archive_record)

                                mock_MQTTThread.assert_called_once_with(
                                    'MQTTPublish', SUT.archive_queue,
                                    **site_config_final)
    def test_topicsinputs(self):
        mock_StdEngine = mock.Mock()
        server_url = random_string()
        topic = random_string()

        inputs = {random_string(): random_string()}

        config_dict = {
            'StdRESTful': {
                'MQTTPublish': {
                    'server_url': server_url,
                    'topics': {
                        topic: {
                            'inputs': inputs
                        }
                    }
                }
            }
        }
        config = configobj.ConfigObj(config_dict)

        manager_dict = {random_string(): random_string()}

        topics = {topic: self.create_topic(inputs=inputs)}

        site_dict = copy.deepcopy(config_dict['StdRESTful']['MQTTPublish'])
        site_config = configobj.ConfigObj(site_dict)

        site_dict_final = {
            'server_url': server_url,
            'topics': topics,
            'manager_dict': manager_dict
        }
        site_config_final = configobj.ConfigObj(site_dict_final)

        with mock.patch('weewx.restx') as mock_restx:
            with mock.patch('weewx.manager') as mock_manager:
                with mock.patch('weewx.manager.open_manager'):
                    with mock.patch('user.mqttpublish.MQTTPublish.bind'):
                        with mock.patch('user.mqttpublish.loginf'):
                            with mock.patch(
                                    'user.mqttpublish.MQTTPublishThread'
                            ) as mock_MQTTThread:
                                mock_restx.get_site_dict.return_value = site_config
                                mock_manager.get_manager_dict_from_config.return_value = manager_dict

                                SUT = MQTTPublish(mock_StdEngine, config)

                                mock_MQTTThread.assert_called_once_with(
                                    'MQTTPublish', SUT.archive_queue,
                                    **site_config_final)
    def test_append_units_label(self):
        mock_StdEngine = mock.Mock()
        server_url = random_string()

        config_dict = {
            'StdRESTful': {
                'MQTTPublish': {
                    'server_url': server_url,
                    'append_units_label': False
                }
            }
        }
        config = configobj.ConfigObj(config_dict)

        manager_dict = {random_string(): random_string()}

        topics = {
            'weather/loop':
            self.create_topic(append_units_label=False),
            'weather':
            self.create_topic(payload_type='individual',
                              append_units_label=False)
        }

        site_dict = copy.deepcopy(config_dict['StdRESTful']['MQTTPublish'])
        site_config = configobj.ConfigObj(site_dict)

        site_dict_final = {
            'server_url': server_url,
            'topics': topics,
            'manager_dict': manager_dict
        }
        site_config_final = configobj.ConfigObj(site_dict_final)

        with mock.patch('weewx.restx') as mock_restx:
            with mock.patch('weewx.manager') as mock_manager:
                with mock.patch('weewx.manager.open_manager'):
                    with mock.patch('user.mqttpublish.MQTTPublish.bind'):
                        with mock.patch('user.mqttpublish.loginf'):
                            with mock.patch(
                                    'user.mqttpublish.MQTTPublishThread'
                            ) as mock_MQTTThread:
                                mock_restx.get_site_dict.return_value = site_config
                                mock_manager.get_manager_dict_from_config.return_value = manager_dict

                                SUT = MQTTPublish(mock_StdEngine, config)

                                mock_MQTTThread.assert_called_once_with(
                                    'MQTTPublish', SUT.archive_queue,
                                    **site_config_final)
    def test_topicsaugment_record(self):
        mock_StdEngine = mock.Mock()
        server_url = random_string()
        topic = random_string()

        config_dict = {
            'StdRESTful': {
                'MQTTPublish': {
                    'server_url': server_url,
                    'topics': {
                        topic: {
                            'augment_record': False
                        }
                    }
                }
            }
        }
        config = configobj.ConfigObj(config_dict)

        topics = {topic: self.create_topic(augment_record=False)}

        site_dict = copy.deepcopy(config_dict['StdRESTful']['MQTTPublish'])
        site_config = configobj.ConfigObj(site_dict)

        site_dict_final = {'server_url': server_url, 'topics': topics}
        site_config_final = configobj.ConfigObj(site_dict_final)

        with mock.patch('weewx.restx') as mock_restx:
            with mock.patch('weewx.manager') as mock_manager:
                with mock.patch('weewx.manager.open_manager'):
                    with mock.patch('user.mqttpublish.MQTTPublish.bind'):
                        with mock.patch('user.mqttpublish.loginf'):
                            with mock.patch(
                                    'user.mqttpublish.MQTTPublishThread'
                            ) as mock_MQTTThread:
                                mock_restx.get_site_dict.return_value = site_config

                                SUT = MQTTPublish(mock_StdEngine, config)

                                mock_manager.get_manager_dict_from_config.assert_not_called(
                                )
                                mock_manager.open_manager.assert_not_called()

                                mock_MQTTThread.assert_called_once_with(
                                    'MQTTPublish', SUT.archive_queue,
                                    **site_config_final)
    def test_topics_binding(self):
        mock_StdEngine = mock.Mock()
        server_url = random_string()
        topic = random_string()

        config_dict = {
            'StdRESTful': {
                'MQTTPublish': {
                    'server_url': server_url,
                    'topics': {
                        topic: {
                            'binding': 'archive, loop'
                        }
                    }
                }
            }
        }
        config = configobj.ConfigObj(config_dict)

        manager_dict = {random_string(): random_string()}

        topics = {topic: self.create_topic(binding='archive, loop')}

        site_dict = copy.deepcopy(config_dict['StdRESTful']['MQTTPublish'])
        site_config = configobj.ConfigObj(site_dict)

        site_dict_final = {
            'server_url': server_url,
            'topics': topics,
            'manager_dict': manager_dict
        }
        site_config_final = configobj.ConfigObj(site_dict_final)

        with mock.patch('weewx.restx') as mock_restx:
            with mock.patch('weewx.manager') as mock_manager:
                with mock.patch('weewx.manager.open_manager'):
                    with mock.patch(
                            'user.mqttpublish.MQTTPublish.bind') as mock_bind:
                        with mock.patch('user.mqttpublish.loginf'):
                            with mock.patch(
                                    'user.mqttpublish.MQTTPublishThread'
                            ) as mock_MQTTThread:
                                mock_restx.get_site_dict.return_value = site_config
                                mock_manager.get_manager_dict_from_config.return_value = manager_dict

                                SUT = MQTTPublish(mock_StdEngine, config)

                                call_args_list = mock_bind.call_args_list
                                self.assertEqual(len(call_args_list), 2)
                                self.assertEqual(call_args_list[0].args[0],
                                                 NEW_ARCHIVE_RECORD)
                                self.assertEqual(call_args_list[0].args[1],
                                                 SUT.new_archive_record)
                                self.assertEqual(call_args_list[1].args[0],
                                                 NEW_LOOP_PACKET)
                                self.assertEqual(call_args_list[1].args[1],
                                                 SUT.new_loop_packet)

                                mock_MQTTThread.assert_called_once_with(
                                    'MQTTPublish', SUT.archive_queue,
                                    **site_config_final)