Skip to content

Commit

Permalink
Update test mocking/patching
Browse files Browse the repository at this point in the history
  • Loading branch information
NeonDaniel committed Sep 20, 2024
1 parent 7cb12c2 commit 9ba19e7
Showing 1 changed file with 33 additions and 45 deletions.
78 changes: 33 additions & 45 deletions test/test_diagnostic_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"):
Expand All @@ -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__':
Expand Down

0 comments on commit 9ba19e7

Please sign in to comment.