From bb06758a7b737befcb28b8fe202d2c6da767146d Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Tue, 4 Jan 2022 11:53:42 +0100 Subject: [refactor] add type hints & remove Setting._post_init Previously the Setting classes used a horrible _post_init hack that prevented proper type checking. --- tests/unit/test_preferences.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/tests/unit/test_preferences.py b/tests/unit/test_preferences.py index 1ffed5c1a..18323940b 100644 --- a/tests/unit/test_preferences.py +++ b/tests/unit/test_preferences.py @@ -1,7 +1,6 @@ from searx.preferences import ( EnumStringSetting, MapSetting, - MissingArgumentException, SearchLanguageSetting, MultipleChoiceSetting, PluginsSetting, @@ -19,10 +18,6 @@ class PluginStub: class TestSettings(SearxTestCase): # map settings - def test_map_setting_invalid_initialization(self): - with self.assertRaises(MissingArgumentException): - MapSetting(3, wrong_argument={'0': 0}) - def test_map_setting_invalid_default_value(self): with self.assertRaises(ValidationException): MapSetting(3, map={'dog': 1, 'bat': 2}) @@ -43,9 +38,6 @@ class TestSettings(SearxTestCase): self.assertEqual(setting.get_value(), 2) # enum settings - def test_enum_setting_invalid_initialization(self): - with self.assertRaises(MissingArgumentException): - EnumStringSetting('cat', wrong_argument=[0, 1, 2]) def test_enum_setting_invalid_default_value(self): with self.assertRaises(ValidationException): @@ -67,9 +59,6 @@ class TestSettings(SearxTestCase): self.assertEqual(setting.get_value(), 2) # multiple choice settings - def test_multiple_setting_invalid_initialization(self): - with self.assertRaises(MissingArgumentException): - MultipleChoiceSetting(['2'], wrong_argument=['0', '1', '2']) def test_multiple_setting_invalid_default_value(self): with self.assertRaises(ValidationException): @@ -115,14 +104,14 @@ class TestSettings(SearxTestCase): def test_plugins_setting_all_default_enabled(self): plugin1 = PluginStub('plugin1', True) plugin2 = PluginStub('plugin2', True) - setting = PluginsSetting(['3'], choices=[plugin1, plugin2]) + setting = PluginsSetting(['3'], plugins=[plugin1, plugin2]) self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin2'])) def test_plugins_setting_few_default_enabled(self): plugin1 = PluginStub('plugin1', True) plugin2 = PluginStub('plugin2', False) plugin3 = PluginStub('plugin3', True) - setting = PluginsSetting('name', choices=[plugin1, plugin2, plugin3]) + setting = PluginsSetting('name', plugins=[plugin1, plugin2, plugin3]) self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin3'])) -- cgit v1.2.3 From 180d4d068b4c629ab99876b55046f98455b88149 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Thu, 6 Jan 2022 18:45:50 +0100 Subject: [refactor] refactor SwitchableSetting The previous implementation used two hash sets and a list. ... that's not necessary ... a single hash map suffices. And it's also less error prone ... because the previous data structure allowed a setting to be enabled and disabled at the same time. --- tests/unit/test_preferences.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/unit/test_preferences.py b/tests/unit/test_preferences.py index 18323940b..a69c45178 100644 --- a/tests/unit/test_preferences.py +++ b/tests/unit/test_preferences.py @@ -105,14 +105,14 @@ class TestSettings(SearxTestCase): plugin1 = PluginStub('plugin1', True) plugin2 = PluginStub('plugin2', True) setting = PluginsSetting(['3'], plugins=[plugin1, plugin2]) - self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin2'])) + self.assertEqual(set(setting.get_enabled()), set(['plugin1', 'plugin2'])) def test_plugins_setting_few_default_enabled(self): plugin1 = PluginStub('plugin1', True) plugin2 = PluginStub('plugin2', False) plugin3 = PluginStub('plugin3', True) setting = PluginsSetting('name', plugins=[plugin1, plugin2, plugin3]) - self.assertEqual(setting.get_enabled(), set(['plugin1', 'plugin3'])) + self.assertEqual(set(setting.get_enabled()), set(['plugin1', 'plugin3'])) class TestPreferences(SearxTestCase): -- cgit v1.2.3