test_preferences.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException, SearchLanguageSetting,
  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. # search language settings
  70. def test_lang_setting_valid_choice(self):
  71. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  72. setting.parse('de')
  73. self.assertEquals(setting.get_value(), 'de')
  74. def test_lang_setting_invalid_choice(self):
  75. setting = SearchLanguageSetting('all', choices=['all', 'de', 'en'])
  76. setting.parse('xx')
  77. self.assertEquals(setting.get_value(), 'all')
  78. def test_lang_setting_old_cookie_choice(self):
  79. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  80. setting.parse('es_XA')
  81. self.assertEquals(setting.get_value(), 'es')
  82. def test_lang_setting_old_cookie_format(self):
  83. setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES'])
  84. setting.parse('es_ES')
  85. self.assertEquals(setting.get_value(), 'es-ES')
  86. # plugins settings
  87. def test_plugins_setting_all_default_enabled(self):
  88. plugin1 = PluginStub('plugin1', True)
  89. plugin2 = PluginStub('plugin2', True)
  90. setting = PluginsSetting(['3'], choices=[plugin1, plugin2])
  91. self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin2']))
  92. def test_plugins_setting_few_default_enabled(self):
  93. plugin1 = PluginStub('plugin1', True)
  94. plugin2 = PluginStub('plugin2', False)
  95. plugin3 = PluginStub('plugin3', True)
  96. setting = PluginsSetting('name', choices=[plugin1, plugin2, plugin3])
  97. self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin3']))