test_locales.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # pylint: disable=missing-module-docstring
  3. """Test some code from module :py:obj:`searx.locales`"""
  4. from __future__ import annotations
  5. from parameterized import parameterized
  6. from searx import locales
  7. from searx.sxng_locales import sxng_locales
  8. from tests import SearxTestCase
  9. class TestLocales(SearxTestCase):
  10. """Implemented tests:
  11. - :py:obj:`searx.locales.match_locale`
  12. """
  13. @classmethod
  14. def setUpClass(cls):
  15. cls.locale_tag_list = [x[0] for x in sxng_locales]
  16. @parameterized.expand(
  17. [
  18. 'de',
  19. 'fr',
  20. 'zh',
  21. ]
  22. )
  23. def test_locale_languages(self, locale: str):
  24. # Test SearXNG search languages
  25. self.assertEqual(locales.match_locale(locale, self.locale_tag_list), locale)
  26. @parameterized.expand(
  27. [
  28. ('ca-es', 'ca-ES'),
  29. ('de-at', 'de-AT'),
  30. ('de-de', 'de-DE'),
  31. ('en-UK', 'en-GB'),
  32. ('fr-be', 'fr-BE'),
  33. ('fr-be', 'fr-BE'),
  34. ('fr-ca', 'fr-CA'),
  35. ('fr-ch', 'fr-CH'),
  36. ('zh-cn', 'zh-CN'),
  37. ('zh-tw', 'zh-TW'),
  38. ('zh-hk', 'zh-HK'),
  39. ]
  40. )
  41. def test_match_region(self, locale: str, expected_locale: str):
  42. # Test SearXNG search regions
  43. self.assertEqual(locales.match_locale(locale, self.locale_tag_list), expected_locale)
  44. @parameterized.expand(
  45. [
  46. ('zh-hans', 'zh-CN'),
  47. ('zh-hans-cn', 'zh-CN'),
  48. ('zh-hant', 'zh-TW'),
  49. ('zh-hant-tw', 'zh-TW'),
  50. ]
  51. )
  52. def test_match_lang_script_code(self, locale: str, expected_locale: str):
  53. # Test language script code
  54. self.assertEqual(locales.match_locale(locale, self.locale_tag_list), expected_locale)
  55. def test_locale_de(self):
  56. self.assertEqual(locales.match_locale('de', ['de-CH', 'de-DE']), 'de-DE')
  57. self.assertEqual(locales.match_locale('de', ['de-CH', 'de-DE']), 'de-DE')
  58. def test_locale_es(self):
  59. self.assertEqual(locales.match_locale('es', [], fallback='fallback'), 'fallback')
  60. self.assertEqual(locales.match_locale('es', ['ES']), 'ES')
  61. self.assertEqual(locales.match_locale('es', ['es-AR', 'es-ES', 'es-MX']), 'es-ES')
  62. self.assertEqual(locales.match_locale('es-AR', ['es-AR', 'es-ES', 'es-MX']), 'es-AR')
  63. self.assertEqual(locales.match_locale('es-CO', ['es-AR', 'es-ES']), 'es-ES')
  64. self.assertEqual(locales.match_locale('es-CO', ['es-AR']), 'es-AR')
  65. @parameterized.expand(
  66. [
  67. ('zh-TW', ['zh-HK'], 'zh-HK'), # A user selects region 'zh-TW' which should end in zh_HK.
  68. # hint: CN is 'Hans' and HK ('Hant') fits better to TW ('Hant')
  69. ('zh', ['zh-CN'], 'zh-CN'), # A user selects only the language 'zh' which should end in CN
  70. ('fr', ['fr-CA'], 'fr-CA'), # A user selects only the language 'fr' which should end in fr_CA
  71. ('nl', ['nl-BE'], 'nl-BE'), # A user selects only the language 'fr' which should end in fr_CA
  72. # Territory tests
  73. ('en', ['en-GB'], 'en-GB'), # A user selects only a language
  74. (
  75. 'fr',
  76. ['fr-FR', 'fr-CA'],
  77. 'fr-FR',
  78. ), # the engine supports fr_FR and fr_CA since no territory is given, fr_FR takes priority
  79. ]
  80. )
  81. def test_locale_optimized_selected(self, locale: str, locale_list: list[str], expected_locale: str):
  82. """
  83. Tests from the commit message of 9ae409a05a
  84. Assumption:
  85. A. When a user selects a language the results should be optimized according to
  86. the selected language.
  87. """
  88. self.assertEqual(locales.match_locale(locale, locale_list), expected_locale)
  89. @parameterized.expand(
  90. [
  91. ('fr-BE', ['fr-FR', 'fr-CA', 'nl-BE'], 'nl-BE'), # A user selects region 'fr-BE' which should end in nl-BE
  92. ('fr', ['fr-BE', 'fr-CH'], 'fr-BE'), # A user selects fr with 2 locales,
  93. # the get_engine_locale selects the locale by looking at the "population
  94. # percent" and this percentage has an higher amount in BE (68.%)
  95. # compared to CH (21%)
  96. ]
  97. )
  98. def test_locale_optimized_territory(self, locale: str, locale_list: list[str], expected_locale: str):
  99. """
  100. Tests from the commit message of 9ae409a05a
  101. B. When user selects a language and a territory the results should be
  102. optimized with first priority on territory and second on language.
  103. """
  104. self.assertEqual(locales.match_locale(locale, locale_list), expected_locale)