diff --git a/tryton/common/domain_parser.py b/tryton/common/domain_parser.py index a7a0a382e0..7c79e2a05d 100644 --- a/tryton/common/domain_parser.py +++ b/tryton/common/domain_parser.py @@ -98,12 +98,12 @@ def group_operator(tokens): yield cur -def likify(value): +def likify(value, escape='\\'): "Add % if needed" if not value: return '%' - escaped = value.replace('%%', '__') - if '%' in escaped: + escaped = value.replace(escape + '%', '').replace(escape + '_', '') + if '%' in escaped or '_' in escaped: return value else: return '%' + value + '%' diff --git a/tryton/tests/test_common_domain_parser.py b/tryton/tests/test_common_domain_parser.py index 4d0ca0c197..8ff14f9dd4 100644 --- a/tryton/tests/test_common_domain_parser.py +++ b/tryton/tests/test_common_domain_parser.py @@ -7,7 +7,8 @@ from unittest import TestCase from tryton.common import untimezoned_date from tryton.common.domain_parser import ( group_operator, quote, split_target_value, convert_value, format_value, - complete_value, parenthesize, rlist, operatorize, DomainParser, udlex) + complete_value, parenthesize, rlist, operatorize, DomainParser, udlex, + likify) class DomainParserTestCase(TestCase): @@ -26,6 +27,19 @@ class DomainParserTestCase(TestCase): self.assertEqual( list(group_operator(iter(['a', '>', '=', '=']))), ['a', '>=', '=']) + def test_likify(self): + "Test likify" + for value, result in [ + ('', '%'), + ('foo', '%foo%'), + ('foo%', 'foo%'), + ('foo_bar', 'foo_bar'), + ('foo\\%', '%foo\\%%'), + ('foo\\_bar', '%foo\\_bar%'), + ]: + with self.subTest(value=value): + self.assertEqual(likify(value), result) + def test_quote(self): "Test quote" self.assertEqual(quote('test'), 'test')