test_locales.py 4.6 KB

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