# HG changeset patch # User Vincent Hatakeyama <vincent.hatakeyama@xcg-consulting.fr> # Date 1668069000 -3600 # Thu Nov 10 09:30:00 2022 +0100 # Branch 15.0 # Node ID b7015f320d56c5fde2fc7878b35d859b1ccdcfcb # Parent fe6cbb34aa296df54105e5f133aa84e684e2e076 🚑 Fix tests when installing various languages in the test database diff --git a/NEWS.rst b/NEWS.rst --- a/NEWS.rst +++ b/NEWS.rst @@ -1,6 +1,11 @@ Changelog ========= +15.0.1.0.1 +---------- + +Fix tests when installing various languages in the test database. + 15.0.1.0.0 ---------- diff --git a/__manifest__.py b/__manifest__.py --- a/__manifest__.py +++ b/__manifest__.py @@ -21,7 +21,7 @@ { "name": "ICU Format", "summary": "Format message using ICU", - "version": "15.0.1.0.0", + "version": "15.0.1.0.1", "category": "Technical", "author": "XCG Consulting", "website": "https://odoo.consulting/", diff --git a/icu_format.py b/icu_format.py --- a/icu_format.py +++ b/icu_format.py @@ -19,7 +19,7 @@ ############################################################################## import datetime import logging -from typing import Dict, Mapping, Optional, Union +from typing import Callable, Dict, Mapping, Optional, ParamSpec, TypeVar, Union # Disable the warning as the library name is pyicu # pylint: disable=missing-manifest-dependency @@ -27,18 +27,23 @@ _logger = logging.getLogger(__name__) +# Disable needed as the pylint version does not contain the fix for type var from +# https://github.com/PyCQA/pylint/pull/5894 +T = TypeVar("T") # pylint: disable=invalid-name +P = ParamSpec("P") + class Cache: """Cache of factory produced object. No maximum capacity.""" - def __init__(self, factory): + def __init__(self, factory: Callable[P, T]): """factory to create items to cache""" self.__data = {} """Cached data""" self.__factory = factory """Factory to create items""" - def get(self, *args, **kwargs): + def get(self, *args: P.args, **kwargs: P.kwargs) -> T: """Return an item, eventually created with the factory""" # create a tuple out of args and kwargs key = tuple( @@ -53,7 +58,7 @@ _locale_cache = Cache(Locale) -"""A Locale cache""" +"""A icu.Locale cache""" class ICUFormatter: diff --git a/tests/test_icuformat.py b/tests/test_icuformat.py --- a/tests/test_icuformat.py +++ b/tests/test_icuformat.py @@ -106,14 +106,21 @@ def test_record_icu_format_and_translate(self): user = self.env["res.users"].search([], limit=1) - # TRANSLATORS: This is a test message, no need for translation + # TRANSLATORS: This is a test message, do not translate self.assertEqual("A message", user.icu_format_translate("A message", {})) def test_list_format(self): """Test the list formatter provided on BaseModel.""" self.assertEqual(icu_list_format("fr", ["a", "b", "c"]), "a, b et c") self.assertEqual(icu_list_format("en_GB", ["1", "2", "5"]), "1, 2 and 5") + self.assertEqual(icu_list_format("en_US", ["1", "2", "5"]), "1, 2, and 5") user = self.env["res.users"].search([], limit=1) + + # Force language on the user and activate it to avoid depending on the language + # installed in the test database. + self.env["res.lang"]._activate_lang("en_US") + self.env.user.lang = "en_US" + # Note that this uses the current user lang, not user lang self.assertEqual(user.icu_list_format(["1", "2", "5"]), "1, 2, and 5") def test_format_currency(self): @@ -154,10 +161,20 @@ ) self.assertEqual(result, "Your total is 1,25 €") + # Force no language on the user + self.env.user.lang = None # Test with handy method on currency result = self.env.ref("base.EUR").icu_format( "Your total is {total, number, currency}", dict(total=1.25) ) + self.assertEqual(result, "Your total is € 1.25") + # Force language on the user and activate it to avoid depending on the language + # installed in the test database. + self.env["res.lang"]._activate_lang("en_US") + self.env.user.lang = "en_US" + result = self.env.ref("base.EUR").icu_format( + "Your total is {total, number, currency}", dict(total=1.25) + ) self.assertEqual(result, "Your total is €1.25") def test_formatter(self):