From 166d9336c1091da1f291144bb10b58412cd4e9d2 Mon Sep 17 00:00:00 2001 From: mart-r Date: Wed, 17 Jul 2024 14:33:50 +0100 Subject: [PATCH] CU-8694wh3d5: Add relevant tests to automatic usage monitoring --- tests/utils/test_usage_monitoring.py | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/utils/test_usage_monitoring.py b/tests/utils/test_usage_monitoring.py index e552a561..9112e71e 100644 --- a/tests/utils/test_usage_monitoring.py +++ b/tests/utils/test_usage_monitoring.py @@ -6,6 +6,7 @@ import tempfile from unittest import TestCase +from unittest.mock import patch class UsageMonitorBaseTests(TestCase): @@ -86,3 +87,41 @@ def test_some_in_file(self): lines = self._get_saved_lines() self.assertTrue(lines) self.assertEqual(len(lines), self.expected_in_file) + + +class UMT(UsageMonitorBaseTests): + ENABLED_DICT = { + "MEDCAT_LOGS": "True", + "MEDCAT_LOGS_LOCATION": "." + } + DISABLED_DICT_1 = { + "MEDCAT_LOGS": "False", + "MEDCAT_LOGS_LOCATION": "FAIL" # should not change anything + } + DISABLED_DICT_2 = { + "MEDCAT_LOGS": "0", + "MEDCAT_LOGS_LOCATION": "." + } + + def setUp(self) -> None: + super().setUp() + self.config.enabled = "auto" + self.config.log_folder = self._temp_dir.name + self.monitor = usage_monitoring.UsageMonitor(self.MODEL_HASH, + self.config) + + @patch.dict(os.environ, ENABLED_DICT) + def test_listens_to_os_environ_enabled(self): + self.assertTrue(self.monitor._should_log()) + self.assertNotEqual(self.config.log_folder, self._temp_dir.name) + self.assertEqual(self.config.log_folder, self.ENABLED_DICT["MEDCAT_LOGS_LOCATION"]) + + @patch.dict(os.environ, DISABLED_DICT_1) + def test_listens_to_os_environ_disabled_1(self): + self.assertFalse(self.monitor._should_log()) + self.assertEqual(self.config.log_folder, self._temp_dir.name) + + @patch.dict(os.environ, DISABLED_DICT_2) + def test_listens_to_os_environ_disabled_2(self): + self.assertFalse(self.monitor._should_log()) + self.assertEqual(self.config.log_folder, self._temp_dir.name)