logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

test_preferences.py (5318B)


  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('en', choices=['de', 'en'])
  72. setting.parse('de')
  73. self.assertEquals(setting.get_value(), 'de')
  74. def test_lang_setting_invalid_choice(self):
  75. setting = SearchLanguageSetting('en', choices=['de', 'en'])
  76. setting.parse('xx')
  77. self.assertEquals(setting.get_value(), 'en')
  78. def test_lang_setting_old_cookie_choice(self):
  79. setting = SearchLanguageSetting('en', choices=['en', '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('en', choices=['en', 'es', 'es-ES'])
  84. setting.parse('es_ES')
  85. self.assertEquals(setting.get_value(), 'es-ES')
  86. def test_lang_setting_old_default(self):
  87. setting = SearchLanguageSetting('en', choices=['en', 'es', 'de'])
  88. setting.parse('all')
  89. self.assertEquals(setting.get_value(), 'en')
  90. # plugins settings
  91. def test_plugins_setting_all_default_enabled(self):
  92. plugin1 = PluginStub('plugin1', True)
  93. plugin2 = PluginStub('plugin2', True)
  94. setting = PluginsSetting(['3'], choices=[plugin1, plugin2])
  95. self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin2']))
  96. def test_plugins_setting_few_default_enabled(self):
  97. plugin1 = PluginStub('plugin1', True)
  98. plugin2 = PluginStub('plugin2', False)
  99. plugin3 = PluginStub('plugin3', True)
  100. setting = PluginsSetting('name', choices=[plugin1, plugin2, plugin3])
  101. self.assertEquals(setting.get_enabled(), set(['plugin1', 'plugin3']))