test_preferences.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException,
  2. MultipleChoiceSetting, PluginsSetting, ValidationException)
  3. from searx.testing import SearxTestCase
  4. class PluginStub(object):
  5. def __init__(self, id, default_on):
  6. self.id = id
  7. self.default_on = default_on
  8. class TestSettings(SearxTestCase):
  9. # map settings
  10. def test_map_setting_invalid_initialization(self):
  11. with self.assertRaises(MissingArgumentException):
  12. setting = MapSetting(3, wrong_argument={'0': 0})
  13. def test_map_setting_invalid_default_value(self):
  14. with self.assertRaises(ValidationException):
  15. setting = MapSetting(3, map={'dog': 1, 'bat': 2})
  16. def test_map_setting_invalid_choice(self):
  17. setting = MapSetting(2, map={'dog': 1, 'bat': 2})
  18. with self.assertRaises(ValidationException):
  19. setting.parse('cat')
  20. def test_map_setting_valid_default(self):
  21. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  22. self.assertEquals(setting.get_value(), 3)
  23. def test_map_setting_valid_choice(self):
  24. setting = MapSetting(3, map={'dog': 1, 'bat': 2, 'cat': 3})
  25. self.assertEquals(setting.get_value(), 3)
  26. setting.parse('bat')
  27. self.assertEquals(setting.get_value(), 2)
  28. def test_enum_setting_invalid_initialization(self):
  29. with self.assertRaises(MissingArgumentException):
  30. setting = EnumStringSetting('cat', wrong_argument=[0, 1, 2])
  31. # enum settings
  32. def test_enum_setting_invalid_initialization(self):
  33. with self.assertRaises(MissingArgumentException):
  34. setting = EnumStringSetting('cat', wrong_argument=[0, 1, 2])
  35. def test_enum_setting_invalid_default_value(self):
  36. with self.assertRaises(ValidationException):
  37. setting = EnumStringSetting(3, choices=[0, 1, 2])
  38. def test_enum_setting_invalid_choice(self):
  39. setting = EnumStringSetting(0, choices=[0, 1, 2])
  40. with self.assertRaises(ValidationException):
  41. setting.parse(3)
  42. def test_enum_setting_valid_default(self):
  43. setting = EnumStringSetting(3, choices=[1, 2, 3])
  44. self.assertEquals(setting.get_value(), 3)
  45. def test_enum_setting_valid_choice(self):
  46. setting = EnumStringSetting(3, choices=[1, 2, 3])
  47. self.assertEquals(setting.get_value(), 3)
  48. setting.parse(2)
  49. self.assertEquals(setting.get_value(), 2)
  50. # multiple choice settings
  51. def test_multiple_setting_invalid_initialization(self):
  52. with self.assertRaises(MissingArgumentException):
  53. setting = MultipleChoiceSetting(['2'], wrong_argument=['0', '1', '2'])
  54. def test_multiple_setting_invalid_default_value(self):
  55. with self.assertRaises(ValidationException):
  56. setting = MultipleChoiceSetting(['3', '4'], choices=['0', '1', '2'])
  57. def test_multiple_setting_invalid_choice(self):
  58. setting = MultipleChoiceSetting(['1', '2'], choices=['0', '1', '2'])
  59. with self.assertRaises(ValidationException):
  60. setting.parse('4, 3')
  61. def test_multiple_setting_valid_default(self):
  62. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  63. self.assertEquals(setting.get_value(), ['3'])
  64. def test_multiple_setting_valid_choice(self):
  65. setting = MultipleChoiceSetting(['3'], choices=['1', '2', '3'])
  66. self.assertEquals(setting.get_value(), ['3'])
  67. setting.parse('2')
  68. self.assertEquals(setting.get_value(), ['2'])
  69. # plugins settings
  70. def test_plugins_setting_all_default_enabled(self):
  71. plugin1 = PluginStub('plugin1', True)
  72. plugin2 = PluginStub('plugin2', True)
  73. setting = PluginsSetting(['3'], choices=[plugin1, plugin2])
  74. self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin2']))
  75. def test_plugins_setting_few_default_enabled(self):
  76. plugin1 = PluginStub('plugin1', True)
  77. plugin2 = PluginStub('plugin2', False)
  78. plugin3 = PluginStub('plugin3', True)
  79. setting = PluginsSetting('name', choices=[plugin1, plugin2, plugin3])
  80. self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin3']))