From e0c270bd72f7b2a40222e3ed264e25d36cb0fc30 Mon Sep 17 00:00:00 2001 From: marc Date: Tue, 13 Dec 2016 23:51:15 -0600 Subject: [PATCH] tests for language support in engines --- searx/engines/.yandex.py.swp | Bin 0 -> 12288 bytes searx/engines/duckduckgo.py | 6 ++++-- searx/engines/subtitleseeker.py | 2 +- searx/preferences.py | 2 +- tests/unit/engines/test_duckduckgo.py | 11 +++++++++++ tests/unit/engines/test_gigablast.py | 6 ++++++ tests/unit/engines/test_subtitleseeker.py | 5 +++++ tests/unit/engines/test_wikipedia.py | 4 ++++ tests/unit/test_preferences.py | 23 +++++++++++++++++++++- 9 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 searx/engines/.yandex.py.swp diff --git a/searx/engines/.yandex.py.swp b/searx/engines/.yandex.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..ff2a8f64848312a9beca190fb55cf54fcee8ca14 GIT binary patch literal 12288 zcmeHNJ&zkj7@kldJ`z9#QKcAs60;WP-MdQwVaddZD?mz=NL(Pnvdnlrw_a|)n4LXr z%t4VN4bVeHMHPvH4+#)IfsS8*6zBj!K@%aKcRzf0Ap|0-NHfy2>v_NSeV=(}8qav` zhCe7A2PM&)5mZtAeK<~Ux11;lN znlMegTuD8vpDGE$MCIOGCKEMtv-vHfS*2ybGH`ng9AhUp*4M~qrS%v;{_us{bGO!( z0n318z%pPNunbrRECZGS%fKDYfXR-qcTvGzbrt>kedLCBJ+&{(fMvikU>UFsSOzQu zmI2FvWxz6E8L$jk2JS!xGhbzNm(mf{#Yvt?oh$BZnNLNVHRhFFcR6q5cngZJ5+k}? z2>10gOehEB=*}`}rcSLMm9utx@1`+KXd?Ab5lUs_zk0|YU*bK!5QUE5edxOo)jor@ z;l7vA^Qw&PRM!qXO!y>?>wK#qdF|V+xU7Zv)?ipB0DDZPCXU=~oyNJ*VG>Z`nQPR{ zwAn;$Q}`@Ra#hOPUADplWtLJ-j-2kW`qPe9Zxkvw=n{@muQk>}#(60r>3^%`rMB&itk4qvYr zE&Y0YEOT|x%S4l2B$J?k!zh-Si@F^Wv^iJ@H4^o6bX^=$onZ{EL-8_pzRLn3BO9kJ*nwWGKQZ0#ndFi%!fW3s&KHCLb);CIgx=(@Vc5bW^DKn*2~ zrhVt5|MBbZyHo7!kbdVavA>6!Mik?6&yMFe(-o5Z@LXlhf>J9W(S^_j>F`{oHEo)K zYCs;Se(8`GS(a)8&6X|{=PP{c+~zrk;<4RoDf6XKL8`;@EEov%sM0bLLsVYf3P;D$ z*@#1m4#Vv*z!%E%4-x8v5DohLG>j(8bFlKn5y+O?bmmq>@-Qxws$465G>fA~PNza8 z1>;dTZseF%o073stHpR94Ue<78c?qCvb$Ex6C26GA~$jz)h0>Gofo#wp5eYsxIe|7 zQP@8H=4hfYrP?WSBB#!kGL;b-mg0`gLKr3~eO{-#;Y5|9)0n@h#yJ|W%;$z)(uIfC Qxg@74l6Xap+hqlR0ly4Q)Bpeg literal 0 HcmV?d00001 diff --git a/searx/engines/duckduckgo.py b/searx/engines/duckduckgo.py index d37d2778b..9cf5fb339 100644 --- a/searx/engines/duckduckgo.py +++ b/searx/engines/duckduckgo.py @@ -53,14 +53,16 @@ def request(query, params): locale = None elif params['language'][:2] == 'ja': locale = 'jp-jp' + elif params['language'][:2] == 'sl': + locale = 'sl-sl' elif params['language'] == 'zh-TW': locale = 'tw-tzh' elif params['language'] == 'zh-HK': locale = 'hk-tzh' elif params['language'][-2:] == 'SA': - locale = 'xa' + params['language'].split('-')[0] + locale = 'xa-' + params['language'].split('-')[0] elif params['language'][-2:] == 'GB': - locale = 'uk' + params['language'].split('-')[0] + locale = 'uk-' + params['language'].split('-')[0] else: locale = params['language'].split('-') if len(locale) == 2: diff --git a/searx/engines/subtitleseeker.py b/searx/engines/subtitleseeker.py index f979d0141..77b010c3f 100644 --- a/searx/engines/subtitleseeker.py +++ b/searx/engines/subtitleseeker.py @@ -46,7 +46,7 @@ def response(resp): # dirty fix for languages named differenly in their site if resp.search_params['language'][:2] == 'fa': search_lang = 'Farsi' - elif resp.search_params['language'] == 'pt_BR': + elif resp.search_params['language'] == 'pt-BR': search_lang = 'Brazilian' elif resp.search_params['language'] != 'all': search_lang = [lc[3] diff --git a/searx/preferences.py b/searx/preferences.py index 7dc0e3172..3aeb87e9f 100644 --- a/searx/preferences.py +++ b/searx/preferences.py @@ -110,7 +110,7 @@ class SearchLanguageSetting(EnumStringSetting): elif data == 'ar-XA': data = 'ar-SA' else: - data = 'all' + data = self.value self.value = data diff --git a/tests/unit/engines/test_duckduckgo.py b/tests/unit/engines/test_duckduckgo.py index b5a4fd4f0..2aeaa1880 100644 --- a/tests/unit/engines/test_duckduckgo.py +++ b/tests/unit/engines/test_duckduckgo.py @@ -19,6 +19,17 @@ class TestDuckduckgoEngine(SearxTestCase): self.assertIn('duckduckgo.com', params['url']) self.assertIn('ch-de', params['url']) + # when ddg uses non standard code + dicto['language'] = 'en-GB' + params = duckduckgo.request(query, dicto) + self.assertIn('uk-en', params['url']) + + # no country given + duckduckgo.supported_languages = ['de-CH', 'en-US'] + dicto['language'] = 'de' + params = duckduckgo.request(query, dicto) + self.assertIn('ch-de', params['url']) + def test_no_url_in_request_year_time_range(self): dicto = defaultdict(dict) query = 'test_query' diff --git a/tests/unit/engines/test_gigablast.py b/tests/unit/engines/test_gigablast.py index cb96f3cd1..0723b064c 100644 --- a/tests/unit/engines/test_gigablast.py +++ b/tests/unit/engines/test_gigablast.py @@ -15,6 +15,12 @@ class TestGigablastEngine(SearxTestCase): self.assertTrue('url' in params) self.assertTrue(query in params['url']) self.assertTrue('gigablast.com' in params['url']) + self.assertTrue('xx' in params['url']) + + dicto['language'] = 'en-US' + params = gigablast.request(query, dicto) + self.assertTrue('en' in params['url']) + self.assertFalse('en-US' in params['url']) def test_response(self): self.assertRaises(AttributeError, gigablast.response, None) diff --git a/tests/unit/engines/test_subtitleseeker.py b/tests/unit/engines/test_subtitleseeker.py index e499cd2d6..a22ee74b9 100644 --- a/tests/unit/engines/test_subtitleseeker.py +++ b/tests/unit/engines/test_subtitleseeker.py @@ -10,6 +10,7 @@ class TestSubtitleseekerEngine(SearxTestCase): query = 'test_query' dicto = defaultdict(dict) dicto['pageno'] = 1 + dicto['language'] = 'fr-FR' params = subtitleseeker.request(query, dicto) self.assertTrue('url' in params) self.assertTrue(query in params['url']) @@ -68,6 +69,10 @@ class TestSubtitleseekerEngine(SearxTestCase): self.assertIn('1039 Subs', results[0]['content']) self.assertIn('Alternative Title', results[0]['content']) + dicto['language'] = 'pt-BR' + results = subtitleseeker.response(response) + self.assertEqual(results[0]['url'], 'http://this.is.the.url/Brazilian/') + html = """
diff --git a/tests/unit/engines/test_wikipedia.py b/tests/unit/engines/test_wikipedia.py index 3e2f47ea9..0057277c5 100644 --- a/tests/unit/engines/test_wikipedia.py +++ b/tests/unit/engines/test_wikipedia.py @@ -29,6 +29,10 @@ class TestWikipediaEngine(SearxTestCase): params = wikipedia.request(query, dicto) self.assertIn('en', params['url']) + dicto['language'] = 'xx' + params = wikipedia.request(query, dicto) + self.assertIn('en', params['url']) + def test_response(self): dicto = defaultdict(dict) dicto['language'] = 'fr' diff --git a/tests/unit/test_preferences.py b/tests/unit/test_preferences.py index c17350809..885c515e2 100644 --- a/tests/unit/test_preferences.py +++ b/tests/unit/test_preferences.py @@ -1,4 +1,4 @@ -from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException, +from searx.preferences import (EnumStringSetting, MapSetting, MissingArgumentException, SearchLanguageSetting, MultipleChoiceSetting, PluginsSetting, ValidationException) from searx.testing import SearxTestCase @@ -88,6 +88,27 @@ class TestSettings(SearxTestCase): setting.parse('2') self.assertEquals(setting.get_value(), ['2']) + # search language settings + def test_lang_setting_valid_choice(self): + setting = SearchLanguageSetting('all', choices=['all', 'de', 'en']) + setting.parse('de') + self.assertEquals(setting.get_value(), 'de') + + def test_lang_setting_invalid_choice(self): + setting = SearchLanguageSetting('all', choices=['all', 'de', 'en']) + setting.parse('xx') + self.assertEquals(setting.get_value(), 'all') + + def test_lang_setting_old_cookie_choice(self): + setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES']) + setting.parse('es_XA') + self.assertEquals(setting.get_value(), 'es') + + def test_lang_setting_old_cookie_format(self): + setting = SearchLanguageSetting('all', choices=['all', 'es', 'es-ES']) + setting.parse('es_ES') + self.assertEquals(setting.get_value(), 'es-ES') + # plugins settings def test_plugins_setting_all_default_enabled(self): plugin1 = PluginStub('plugin1', True)