From 0e95e2e3cc5d82c5d7c20f173bb09c7544eb5e30 Mon Sep 17 00:00:00 2001 From: Tang Vu Date: Sun, 22 Mar 2026 00:06:54 +0700 Subject: [PATCH] refactor: missing tests for settings cascade and override logic (#2287) The `Settings.load()` method iterates through multiple configuration file paths and updates the internal `__dict__`, intending to override earlier default settings with later user-specific ones. This cascading logic is a core configuration feature but lacks explicit tests to guarantee that dictionary merging and overriding behave exactly as documented (e.g., ensuring a setting in `~/.maigret/settings.json` correctly overrides `resources/settings.json` without wiping out other keys). Affected files: test_settings.py --- tests/test_settings.py | 53 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 tests/test_settings.py diff --git a/tests/test_settings.py b/tests/test_settings.py new file mode 100644 index 0000000..7e0fb9d --- /dev/null +++ b/tests/test_settings.py @@ -0,0 +1,53 @@ +import unittest +from unittest.mock import patch, mock_open + +from maigret.settings import Settings + + +class TestSettings(unittest.TestCase): + @patch('json.load') + @patch('builtins.open', new_callable=mock_open) + def test_settings_cascade_and_override(self, mock_file, mock_json_load): + file1_data = {"timeout": 10, "retries_count": 3, "proxy_url": "http://proxy1"} + file2_data = {"timeout": 20, "recursive_search": True} + file3_data = {"proxy_url": "http://proxy3", "print_not_found": False} + + mock_json_load.side_effect = [file1_data, file2_data, file3_data] + + settings = Settings() + paths = ['file1.json', 'file2.json', 'file3.json'] + + was_inited, msg = settings.load(paths) + + self.assertTrue(was_inited) + self.assertEqual(settings.retries_count, 3) + self.assertEqual(settings.timeout, 20) + self.assertTrue(settings.recursive_search) + self.assertEqual(settings.proxy_url, "http://proxy3") + self.assertFalse(settings.print_not_found) + + @patch('builtins.open') + def test_settings_file_not_found(self, mock_open_func): + mock_open_func.side_effect = FileNotFoundError() + + settings = Settings() + paths = ['nonexistent.json'] + + was_inited, msg = settings.load(paths) + + self.assertFalse(was_inited) + self.assertIn('None of the default settings files found', msg) + + @patch('json.load') + @patch('builtins.open', new_callable=mock_open) + def test_settings_invalid_json(self, mock_file, mock_json_load): + mock_json_load.side_effect = ValueError("Expecting value") + + settings = Settings() + paths = ['invalid.json'] + + was_inited, msg = settings.load(paths) + + self.assertFalse(was_inited) + self.assertIsInstance(msg, ValueError) + self.assertIn('Problem with parsing json contents', str(msg))