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.

76 lines
2.6 KiB

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