You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.5 KiB

  1. # -*- coding: utf-8 -*-
  2. # © 2015 Grupo ESOC Ingeniería de Servicios, S.L.U. - Jairo Llopis
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp.tests.common import TransactionCase
  5. from .. import exceptions
  6. class BasicCase(TransactionCase):
  7. def setUp(self):
  8. super(BasicCase, self).setUp()
  9. self.langs = ("en_US", "es_ES", "it_IT", "pt_PT", "zh_CN")
  10. self.rl = self.env["res.lang"]
  11. for lang in self.langs:
  12. if not self.rl.search([("code", "=", lang)]):
  13. self.rl.load_lang(lang)
  14. def test_explicit(self):
  15. """When an explicit lang is used."""
  16. for lang in self.langs:
  17. self.assertEqual(self.rl.best_match(lang).code, lang)
  18. def test_record(self):
  19. """When called from a ``res.lang`` record."""
  20. rl = self.rl.with_context(lang="it_IT")
  21. rl.env.user.lang = "pt_PT"
  22. for lang in self.langs:
  23. self.assertEqual(
  24. rl.search([("code", "=", lang)]).best_match().code,
  25. lang)
  26. def test_context(self):
  27. """When called with a lang in context."""
  28. self.env.user.lang = "pt_PT"
  29. for lang in self.langs:
  30. self.assertEqual(
  31. self.rl.with_context(lang=lang).best_match().code,
  32. lang)
  33. def test_user(self):
  34. """When lang not specified in context."""
  35. for lang in self.langs:
  36. self.env.user.lang = lang
  37. # Lang is False in context
  38. self.assertEqual(
  39. self.rl.with_context(lang=False).best_match().code,
  40. lang)
  41. # Lang not found in context
  42. self.assertEqual(
  43. self.rl.with_context(dict()).best_match().code,
  44. lang)
  45. def test_first_installed(self):
  46. """When falling back to first installed language."""
  47. first = self.rl.search([("active", "=", True)], limit=1)
  48. self.env.user.lang = False
  49. self.assertEqual(
  50. self.rl.with_context(lang=False).best_match().code,
  51. first.code)
  52. def test_unavailable(self):
  53. """When matches to an unavailable language."""
  54. self.env.user.lang = False
  55. self.rl = self.rl.with_context(lang=False)
  56. first = self.rl.search([("active", "=", True)], limit=1)
  57. # Safe mode
  58. self.assertEqual(self.rl.best_match("fake_LANG").code, first.code)
  59. # Unsafe mode
  60. with self.assertRaises(exceptions.BestMatchedLanguageNotFoundError):
  61. self.rl.best_match("fake_LANG", failure_safe=False)