From 772c048d01c7585fd60afca1ce30a1914e6e5b4a Mon Sep 17 00:00:00 2001 From: Marc Abonce Seguin Date: Wed, 28 Feb 2018 22:30:48 -0600 Subject: 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. --- tests/unit/test_utils.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'tests/unit/test_utils.py') 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): -- cgit v1.2.3-54-g00ecf