summaryrefslogtreecommitdiff
path: root/tests/unit/test_utils.py
diff options
context:
space:
mode:
authorMarc Abonce Seguin <marc-abonce@mailbox.org>2018-02-28 22:30:48 -0600
committerMarc Abonce Seguin <marc-abonce@mailbox.org>2018-03-27 00:08:03 -0600
commit772c048d01c7585fd60afca1ce30a1914e6e5b4a (patch)
tree96a5662897df2bcf0ab53456e0a67ace998f2169 /tests/unit/test_utils.py
parentd1eae9359f8c5920632a730744ea2208070f06da (diff)
downloadsearxng-772c048d01c7585fd60afca1ce30a1914e6e5b4a.tar.gz
searxng-772c048d01c7585fd60afca1ce30a1914e6e5b4a.zip
refactor engine's search language handling
Add match_language function in utils to match any user given language code with a list of engine's supported languages. Also add language_aliases dict on each engine to translate standard language codes into the custom codes used by the engine.
Diffstat (limited to 'tests/unit/test_utils.py')
-rw-r--r--tests/unit/test_utils.py25
1 files changed, 25 insertions, 0 deletions
diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py
index eb40e62e2..4854636c7 100644
--- a/tests/unit/test_utils.py
+++ b/tests/unit/test_utils.py
@@ -65,6 +65,31 @@ class TestUtils(SearxTestCase):
for test_url, expected in data:
self.assertEqual(utils.prettify_url(test_url, max_length=32), expected)
+ def test_match_language(self):
+ self.assertEqual(utils.match_language('es', ['es']), 'es')
+ self.assertEqual(utils.match_language('es', [], fallback='fallback'), 'fallback')
+ self.assertEqual(utils.match_language('ja', ['jp'], {'ja': 'jp'}), 'jp')
+
+ aliases = {'en-GB': 'en-UK', 'he': 'iw'}
+
+ # guess country
+ self.assertEqual(utils.match_language('de-DE', ['de']), 'de')
+ self.assertEqual(utils.match_language('de', ['de-DE']), 'de-DE')
+ self.assertEqual(utils.match_language('es-CO', ['es-AR', 'es-ES', 'es-MX']), 'es-ES')
+ self.assertEqual(utils.match_language('es-CO', ['es-MX']), 'es-MX')
+ self.assertEqual(utils.match_language('en-UK', ['en-AU', 'en-GB', 'en-US']), 'en-GB')
+ self.assertEqual(utils.match_language('en-GB', ['en-AU', 'en-UK', 'en-US'], aliases), 'en-UK')
+
+ # language aliases
+ self.assertEqual(utils.match_language('iw', ['he']), 'he')
+ self.assertEqual(utils.match_language('he', ['iw'], aliases), 'iw')
+ self.assertEqual(utils.match_language('iw-IL', ['he']), 'he')
+ self.assertEqual(utils.match_language('he-IL', ['iw'], aliases), 'iw')
+ self.assertEqual(utils.match_language('iw', ['he-IL']), 'he-IL')
+ self.assertEqual(utils.match_language('he', ['iw-IL'], aliases), 'iw-IL')
+ self.assertEqual(utils.match_language('iw-IL', ['he-IL']), 'he-IL')
+ self.assertEqual(utils.match_language('he-IL', ['iw-IL'], aliases), 'iw-IL')
+
class TestHTMLTextExtractor(SearxTestCase):