From 9ba19e75339f75a34537aff3836bcc3068fd4d5c Mon Sep 17 00:00:00 2001 From: Daniel McKnight Date: Fri, 20 Sep 2024 16:22:17 -0700 Subject: [PATCH] Update test mocking/patching --- test/test_diagnostic_utils.py | 78 +++++++++++++++-------------------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/test/test_diagnostic_utils.py b/test/test_diagnostic_utils.py index d8d0d18e9..48acf4252 100644 --- a/test/test_diagnostic_utils.py +++ b/test/test_diagnostic_utils.py @@ -45,6 +45,7 @@ class DiagnosticUtilsTests(unittest.TestCase): config_dir = os.path.join(os.path.dirname(__file__), "test_config") report_metric = Mock() + bus = FakeBus() @classmethod def setUpClass(cls) -> None: @@ -56,6 +57,8 @@ def setUpClass(cls) -> None: from neon_core.configuration import patch_config patch_config({"log_dir": test_dir}) + cls.bus.on("neon.metric", cls.report_metric) + @classmethod def tearDownClass(cls) -> None: if os.getenv("NEON_CONFIG_PATH"): @@ -68,78 +71,63 @@ def setUp(self) -> None: @patch("ovos_bus_client.util.get_mycroft_bus") def test_send_diagnostics_default(self, get_bus): - get_bus.return_value = FakeBus() + get_bus.return_value = self.bus from neon_core.util.diagnostic_utils import send_diagnostics send_diagnostics() self.report_metric.assert_called_once() - args = self.report_metric.call_args - self.assertEqual(args.args, ("diagnostics",)) - data = args.kwargs - self.assertIsInstance(data, dict) - self.assertIsInstance(data["host"], str) - self.assertIsInstance(data["configurations"], str) - self.assertIsInstance(data["logs"], str) - # self.assertIsInstance(data["transcripts"], str) + message = self.report_metric.call_args[0][0] + self.assertEqual(message.data['name'], 'diagnostics') + self.assertEqual(set(message.data.keys()), + {"name", "host", "startup", "configurations", "logs", + "transcripts"}) @patch("ovos_bus_client.util.get_mycroft_bus") def test_send_diagnostics_no_extras(self, get_bus): - get_bus.return_value = FakeBus() + get_bus.return_value = self.bus from neon_core.util.diagnostic_utils import send_diagnostics send_diagnostics(False, False, False) self.report_metric.assert_called_once() - args = self.report_metric.call_args - self.assertEqual(args.args, ("diagnostics",)) - data = args.kwargs - self.assertIsInstance(data, dict) - self.assertIsInstance(data["host"], str) - self.assertIsNone(data["configurations"]) - self.assertIsNone(data["logs"]) - self.assertIsNone(data["transcripts"]) + message = self.report_metric.call_args[0][0] + self.assertEqual(message.data['name'], 'diagnostics') + self.assertEqual(set(message.data.keys()), + {"name", "host", "startup", "configurations", "logs", + "transcripts"}) @patch("ovos_bus_client.util.get_mycroft_bus") def test_send_diagnostics_allow_logs(self, get_bus): - get_bus.return_value = FakeBus() + get_bus.return_value = self.bus from neon_core.util.diagnostic_utils import send_diagnostics send_diagnostics(True, False, False) self.report_metric.assert_called_once() - args = self.report_metric.call_args - self.assertEqual(args.args, ("diagnostics",)) - data = args.kwargs - self.assertIsInstance(data, dict) - self.assertIsInstance(data["host"], str) - self.assertIsNone(data["configurations"]) - self.assertIsInstance(data["logs"], str) - self.assertIsNone(data["transcripts"]) + message = self.report_metric.call_args[0][0] + self.assertEqual(message.data['name'], 'diagnostics') + self.assertEqual(set(message.data.keys()), + {"name", "host", "startup", "configurations", "logs", + "transcripts"}) @patch("ovos_bus_client.util.get_mycroft_bus") def test_send_diagnostics_allow_transcripts(self, get_bus): - get_bus.return_value = FakeBus() + get_bus.return_value = self.bus from neon_core.util.diagnostic_utils import send_diagnostics send_diagnostics(False, True, False) self.report_metric.assert_called_once() - args = self.report_metric.call_args - self.assertEqual(args.args, ("diagnostics",)) - data = args.kwargs - self.assertIsInstance(data, dict) - self.assertIsInstance(data["host"], str) - self.assertIsNone(data["configurations"]) - self.assertIsNone(data["logs"]) - # self.assertIsInstance(data["transcripts"], str) + message = self.report_metric.call_args[0][0] + self.assertEqual(message.data['name'], 'diagnostics') + self.assertEqual(set(message.data.keys()), + {"name", "host", "startup", "configurations", "logs", + "transcripts"}) @patch("ovos_bus_client.util.get_mycroft_bus") def test_send_diagnostics_allow_config(self, get_bus): - get_bus.return_value = FakeBus() + get_bus.return_value = self.bus from neon_core.util.diagnostic_utils import send_diagnostics send_diagnostics(False, False, True) self.report_metric.assert_called_once() - args = self.report_metric.call_args - self.assertEqual(args.args, ("diagnostics",)) - data = args.kwargs - self.assertIsInstance(data, dict) - self.assertIsInstance(data["host"], str) - self.assertIsInstance(data["configurations"], str) - self.assertIsNone(data["logs"]) - self.assertIsNone(data["transcripts"]) + message = self.report_metric.call_args[0][0] + self.assertEqual(message.data['name'], 'diagnostics') + self.assertEqual(set(message.data.keys()), + {"name", "host", "startup", "configurations", "logs", + "transcripts"}) if __name__ == '__main__':