diff --git a/odoo/addons/base/tests/test_translate.py b/odoo/addons/base/tests/test_translate.py index 0a9fdc4bee8..efb36b3a2fd 100644 --- a/odoo/addons/base/tests/test_translate.py +++ b/odoo/addons/base/tests/test_translate.py @@ -9,7 +9,7 @@ from psycopg2.extras import Json import io from odoo.exceptions import AccessError, ValidationError -from odoo.tools import trans_load_data +from odoo.tools import trans_load_data, sql from odoo.tools.translate import quote, unquote, xml_translate, html_translate from odoo.tests.common import TransactionCase, BaseCase, new_test_user, tagged @@ -854,3 +854,37 @@ class TestLanguageInstallPerformance(TransactionCase): fr_BE.toggle_active() t1 = time.time() _stats_logger.info("installed language fr_BE in %.3fs", t1 - t0) + + +class TestTranslationTrigramIndexPatterns(BaseCase): + def test_value_conversion(self): + cases = [ + # pylint: disable=bad-whitespace + ( 'abc', '%abc%', 'simple text is not escaped correctly'), + ( 'a"bc', r'%a\\"bc%', '" is not escaped correctly'), + (r'a\bc', r'%a\\\\bc%', r'\ is not escaped correctly'), + ( 'a\nbc', r'%a\\nbc%', r'\n is not escaped correctly'), + ( 'a_bc', r'%a\_bc%', '_ is not escaped correctly'), + ( 'a%bc', r'%a\%bc%', '% is not escaped correctly'), + ( 'a_', '%', 'values with less than 3 characters should be dropped'), + ] + for value, expected, message in cases: + self.assertEqual(sql.value_to_translated_trigram_pattern(value), expected, message) + + def test_pattern_conversion(self): + cases = [ + # pylint: disable=bad-whitespace + ( 'abc', '%abc%', 'simple pattern is not escaped correctly'), + ( 'a"bc', r'%a\\"bc%', '" is not escaped correctly'), + (r'a\\bc', r'%a\\\\bc%', r'\ is not escaped correctly'), + ( 'a\nbc', r'%a\\nbc%', r'\n is not escaped correctly'), + (r'a\_bc', r'%a\_bc%', r"\_ shouldn't be escaped"), + (r'a\%bc', r'%a\%bc%', r"\% shouldn't be escaped"), + ( 'abc_def', '%abc%def%', 'wildcard character _ should be changed to %'), + ( 'abc%def', '%abc%def%', "wildcard character % shouldn't be escaped"), + (r'a\bc', '%abc%', r'redundant \ for pattern should be removed'), + ( 'abc_de', '%abc%', 'sub patterns less than 3 characters should be dropped'), + ( 'ab', '%', 'patterns without trigram should be simplified'), + ] + for original_pattern, escaped_pattern, message in cases: + self.assertEqual(sql.pattern_to_translated_trigram_pattern(original_pattern), escaped_pattern, message) diff --git a/odoo/addons/test_new_api/models/test_new_api.py b/odoo/addons/test_new_api/models/test_new_api.py index 225e193b783..2330cd4eabd 100644 --- a/odoo/addons/test_new_api/models/test_new_api.py +++ b/odoo/addons/test_new_api/models/test_new_api.py @@ -1730,3 +1730,10 @@ class RelatedTranslation3(models.Model): parent_id = fields.Many2one('test_new_api.related_translation_2', string='Parent Model') name = fields.Char('Name Related', related='parent_id.name', readonly=False) html = fields.Html('HTML Related', related='parent_id.html', readonly=False) + + +class IndexedTranslation(models.Model): + _name = 'test_new_api.indexed_translation' + _description = 'A model to indexed translated fields' + + name = fields.Text('Name trigram', translate=True, index='trigram') diff --git a/odoo/addons/test_new_api/security/ir.model.access.csv b/odoo/addons/test_new_api/security/ir.model.access.csv index db6a005784e..137db627427 100644 --- a/odoo/addons/test_new_api/security/ir.model.access.csv +++ b/odoo/addons/test_new_api/security/ir.model.access.csv @@ -99,3 +99,4 @@ access_test_new_api_modified_line,access_test_new_api_modified_line,model_test_n access_test_new_api_related_translation_1,access_test_new_api_related_translation_1,model_test_new_api_related_translation_1,,1,1,1,1 access_test_new_api_related_translation_2,access_test_new_api_related_translation_2,model_test_new_api_related_translation_2,,1,1,1,1 access_test_new_api_related_translation_3,access_test_new_api_related_translation_3,model_test_new_api_related_translation_3,,1,1,1,1 +access_test_new_api_indexed_translation,access_test_new_api_indexed_translation,model_test_new_api_indexed_translation,,1,1,1,1 diff --git a/odoo/addons/test_new_api/tests/__init__.py b/odoo/addons/test_new_api/tests/__init__.py index c2a4b097bc9..db0523d2483 100644 --- a/odoo/addons/test_new_api/tests/__init__.py +++ b/odoo/addons/test_new_api/tests/__init__.py @@ -7,6 +7,7 @@ from . import test_one2many from . import test_many2many from . import test_properties from . import test_qweb_float +from . import test_indexed_translation from . import test_related_translation from . import test_ui from . import test_domain diff --git a/odoo/addons/test_new_api/tests/test_indexed_translation.py b/odoo/addons/test_new_api/tests/test_indexed_translation.py new file mode 100644 index 00000000000..90356750803 --- /dev/null +++ b/odoo/addons/test_new_api/tests/test_indexed_translation.py @@ -0,0 +1,40 @@ +# -*- coding: utf-8 -*- + +import odoo.tests + + +@odoo.tests.tagged('post_install', '-at_install') +class TestIndexedTranslation(odoo.tests.TransactionCase): + + @classmethod + def setUpClass(cls): + super().setUpClass() + cls.env['res.lang']._activate_lang('fr_FR') + cls.record_en = cls.env['test_new_api.indexed_translation'].with_context(lang='en_US').create({}) + cls.record_fr = cls.record_en.with_context(lang='fr_FR') + cls.record_en.name = '