test_locales.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. # -*- coding: utf-8 -*-
  2. # SPDX-License-Identifier: AGPL-3.0-or-later
  3. # lint: pylint
  4. """Test some code from module :py:obj:`searx.locales`"""
  5. from searx import locales
  6. from searx.sxng_locales import sxng_locales
  7. from tests import SearxTestCase
  8. class TestLocales(SearxTestCase):
  9. """Implemented tests:
  10. - :py:obj:`searx.locales.match_locale`
  11. """
  12. def test_match_locale(self):
  13. locale_tag_list = [x[0] for x in sxng_locales]
  14. # Test SearXNG search languages
  15. self.assertEqual(locales.match_locale('de', locale_tag_list), 'de')
  16. self.assertEqual(locales.match_locale('fr', locale_tag_list), 'fr')
  17. self.assertEqual(locales.match_locale('zh', locale_tag_list), 'zh')
  18. # Test SearXNG search regions
  19. self.assertEqual(locales.match_locale('ca-es', locale_tag_list), 'ca-ES')
  20. self.assertEqual(locales.match_locale('de-at', locale_tag_list), 'de-AT')
  21. self.assertEqual(locales.match_locale('de-de', locale_tag_list), 'de-DE')
  22. self.assertEqual(locales.match_locale('en-UK', locale_tag_list), 'en-GB')
  23. self.assertEqual(locales.match_locale('fr-be', locale_tag_list), 'fr-BE')
  24. self.assertEqual(locales.match_locale('fr-be', locale_tag_list), 'fr-BE')
  25. self.assertEqual(locales.match_locale('fr-ca', locale_tag_list), 'fr-CA')
  26. self.assertEqual(locales.match_locale('fr-ch', locale_tag_list), 'fr-CH')
  27. self.assertEqual(locales.match_locale('zh-cn', locale_tag_list), 'zh-CN')
  28. self.assertEqual(locales.match_locale('zh-tw', locale_tag_list), 'zh-TW')
  29. self.assertEqual(locales.match_locale('zh-hk', locale_tag_list), 'zh-HK')
  30. # Test language script code
  31. self.assertEqual(locales.match_locale('zh-hans', locale_tag_list), 'zh-CN')
  32. self.assertEqual(locales.match_locale('zh-hans-cn', locale_tag_list), 'zh-CN')
  33. self.assertEqual(locales.match_locale('zh-hant', locale_tag_list), 'zh-TW')
  34. self.assertEqual(locales.match_locale('zh-hant-tw', locale_tag_list), 'zh-TW')
  35. # Test individual locale lists
  36. self.assertEqual(locales.match_locale('es', [], fallback='fallback'), 'fallback')
  37. self.assertEqual(locales.match_locale('de', ['de-CH', 'de-DE']), 'de-DE')
  38. self.assertEqual(locales.match_locale('de', ['de-CH', 'de-DE']), 'de-DE')
  39. self.assertEqual(locales.match_locale('es', ['ES']), 'ES')
  40. self.assertEqual(locales.match_locale('es', ['es-AR', 'es-ES', 'es-MX']), 'es-ES')
  41. self.assertEqual(locales.match_locale('es-AR', ['es-AR', 'es-ES', 'es-MX']), 'es-AR')
  42. self.assertEqual(locales.match_locale('es-CO', ['es-AR', 'es-ES']), 'es-ES')
  43. self.assertEqual(locales.match_locale('es-CO', ['es-AR']), 'es-AR')
  44. # Tests from the commit message of 9ae409a05a
  45. # Assumption:
  46. # A. When a user selects a language the results should be optimized according to
  47. # the selected language.
  48. #
  49. # B. When user selects a language and a territory the results should be
  50. # optimized with first priority on territory and second on language.
  51. # Assume we have an engine that supports the following locales:
  52. locale_tag_list = ['zh-CN', 'zh-HK', 'nl-BE', 'fr-CA']
  53. # Examples (Assumption A.)
  54. # ------------------------
  55. # A user selects region 'zh-TW' which should end in zh_HK.
  56. # hint: CN is 'Hans' and HK ('Hant') fits better to TW ('Hant')
  57. self.assertEqual(locales.match_locale('zh-TW', locale_tag_list), 'zh-HK')
  58. # A user selects only the language 'zh' which should end in CN
  59. self.assertEqual(locales.match_locale('zh', locale_tag_list), 'zh-CN')
  60. # A user selects only the language 'fr' which should end in fr_CA
  61. self.assertEqual(locales.match_locale('fr', locale_tag_list), 'fr-CA')
  62. # The difference in priority on the territory is best shown with a
  63. # engine that supports the following locales:
  64. locale_tag_list = ['fr-FR', 'fr-CA', 'en-GB', 'nl-BE']
  65. # A user selects only a language
  66. self.assertEqual(locales.match_locale('en', locale_tag_list), 'en-GB')
  67. # hint: the engine supports fr_FR and fr_CA since no territory is given,
  68. # fr_FR takes priority ..
  69. self.assertEqual(locales.match_locale('fr', locale_tag_list), 'fr-FR')
  70. # Examples (Assumption B.)
  71. # ------------------------
  72. # A user selects region 'fr-BE' which should end in nl-BE
  73. self.assertEqual(locales.match_locale('fr-BE', locale_tag_list), 'nl-BE')
  74. # If the user selects a language and there are two locales like the
  75. # following:
  76. locale_tag_list = ['fr-BE', 'fr-CH']
  77. # The get_engine_locale selects the locale by looking at the "population
  78. # percent" and this percentage has an higher amount in BE (68.%)
  79. # compared to CH (21%)
  80. self.assertEqual(locales.match_locale('fr', locale_tag_list), 'fr-BE')