[IMP] base_domain_inverse_function: black, isort, prettier
parent
411c3af759
commit
eb5cb322ec
|
@ -24,10 +24,7 @@ def inverse_combine(domain, operator):
|
|||
"""
|
||||
if operator not in DOMAIN_OPERATORS:
|
||||
raise Exception("Unsupported operator parameter: %s" % operator)
|
||||
operator_func = {
|
||||
AND_OPERATOR: AND,
|
||||
OR_OPERATOR: OR
|
||||
}
|
||||
operator_func = {AND_OPERATOR: AND, OR_OPERATOR: OR}
|
||||
other_operator = OR_OPERATOR if operator == AND_OPERATOR else AND_OPERATOR
|
||||
result = []
|
||||
operator_elements_stack = []
|
||||
|
@ -39,7 +36,9 @@ def inverse_combine(domain, operator):
|
|||
# 1. Loop over the domain in reverse order
|
||||
for element in reversed(domain):
|
||||
if element == NOT_OPERATOR:
|
||||
raise Exception("Inversing domains including NOT operator ('!') is not supported")
|
||||
raise Exception(
|
||||
"Inversing domains including NOT operator ('!') is not supported"
|
||||
)
|
||||
if element in DOMAIN_OPERATORS:
|
||||
# 3. When we reach an operator:
|
||||
# - pop the last item from the element stack to the corresponding operator stack
|
||||
|
@ -48,7 +47,10 @@ def inverse_combine(domain, operator):
|
|||
if element != operator:
|
||||
if len(elements_stack) > 0:
|
||||
other_elements_stack.append([elements_stack.pop()])
|
||||
if len(other_elements_stack) == 1 and last_element not in DOMAIN_OPERATORS:
|
||||
if (
|
||||
len(other_elements_stack) == 1
|
||||
and last_element not in DOMAIN_OPERATORS
|
||||
):
|
||||
other_elements_stack.append([elements_stack.pop()])
|
||||
else:
|
||||
if len(elements_stack) > 0:
|
||||
|
|
|
@ -7,16 +7,21 @@ from ..inverse_expression import inverse_AND, inverse_OR
|
|||
|
||||
|
||||
class TestInverseFunctions(SavepointCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls.basic_domain_and = ["&", "&", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)]
|
||||
cls.basic_domain_or = ["|", "|", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)]
|
||||
cls.complex_domain_and_and_or = ["&"] + cls.basic_domain_and + cls.basic_domain_or
|
||||
cls.complex_domain_or_or_and = ["|"] + cls.basic_domain_or + cls.basic_domain_and
|
||||
cls.complex_domain_and_and_or = (
|
||||
["&"] + cls.basic_domain_and + cls.basic_domain_or
|
||||
)
|
||||
cls.complex_domain_or_or_and = (
|
||||
["|"] + cls.basic_domain_or + cls.basic_domain_and
|
||||
)
|
||||
cls.complex_domain_and_or_or = ["&"] + cls.basic_domain_or + cls.basic_domain_or
|
||||
cls.complex_domain_or_and_and = ["|"] + cls.basic_domain_and + cls.basic_domain_and
|
||||
cls.complex_domain_or_and_and = (
|
||||
["|"] + cls.basic_domain_and + cls.basic_domain_and
|
||||
)
|
||||
|
||||
def test_neutral_basic_and(self):
|
||||
result = AND(inverse_AND(self.basic_domain_and))
|
||||
|
@ -48,10 +53,7 @@ class TestInverseFunctions(SavepointCase):
|
|||
[("b", "=", 2)],
|
||||
[("c", "=", 3)],
|
||||
]
|
||||
self.assertEqual(
|
||||
inverse_AND(self.basic_domain_and),
|
||||
result
|
||||
)
|
||||
self.assertEqual(inverse_AND(self.basic_domain_and), result)
|
||||
|
||||
def test_inverse_basic_or(self):
|
||||
result = [
|
||||
|
@ -59,51 +61,36 @@ class TestInverseFunctions(SavepointCase):
|
|||
[("b", "=", 2)],
|
||||
[("c", "=", 3)],
|
||||
]
|
||||
self.assertEqual(
|
||||
inverse_OR(self.basic_domain_or),
|
||||
result
|
||||
)
|
||||
self.assertEqual(inverse_OR(self.basic_domain_or), result)
|
||||
|
||||
def test_inverse_complex_and_and_or(self):
|
||||
result = [
|
||||
[("a", "=", 1)],
|
||||
[("b", "=", 2)],
|
||||
[("c", "=", 3)],
|
||||
["|", "|", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)]
|
||||
["|", "|", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)],
|
||||
]
|
||||
self.assertEqual(
|
||||
inverse_AND(self.complex_domain_and_and_or),
|
||||
result
|
||||
)
|
||||
self.assertEqual(inverse_AND(self.complex_domain_and_and_or), result)
|
||||
|
||||
def test_inverse_complex_or_or_and(self):
|
||||
result = [
|
||||
[("a", "=", 1)],
|
||||
[("b", "=", 2)],
|
||||
[("c", "=", 3)],
|
||||
["&", "&", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)]
|
||||
["&", "&", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)],
|
||||
]
|
||||
self.assertEqual(
|
||||
inverse_OR(self.complex_domain_or_or_and),
|
||||
result
|
||||
)
|
||||
self.assertEqual(inverse_OR(self.complex_domain_or_or_and), result)
|
||||
|
||||
def test_inverse_complex_and_or_or(self):
|
||||
result = [
|
||||
["|", "|", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)],
|
||||
["|", "|", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)],
|
||||
]
|
||||
self.assertEqual(
|
||||
inverse_AND(self.complex_domain_and_or_or),
|
||||
result
|
||||
)
|
||||
self.assertEqual(inverse_AND(self.complex_domain_and_or_or), result)
|
||||
|
||||
def test_inverse_complex_or_and_and(self):
|
||||
result = [
|
||||
["&", "&", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)],
|
||||
["&", "&", ("a", "=", 1), ("b", "=", 2), ("c", "=", 3)],
|
||||
]
|
||||
self.assertEqual(
|
||||
inverse_OR(self.complex_domain_or_and_and),
|
||||
result
|
||||
)
|
||||
self.assertEqual(inverse_OR(self.complex_domain_or_and_and), result)
|
||||
|
|
|
@ -14,7 +14,7 @@ class TestPartnerDomains(SavepointCase):
|
|||
cls.partner_domains = [
|
||||
[("name", "ilike", "Deco")],
|
||||
[("email", "ilike", "example.com")],
|
||||
[("country_id", "=", cls.env.ref("base.us").id)]
|
||||
[("country_id", "=", cls.env.ref("base.us").id)],
|
||||
]
|
||||
|
||||
def test_inverse_partner_domain_and(self):
|
||||
|
@ -25,7 +25,7 @@ class TestPartnerDomains(SavepointCase):
|
|||
# Ensure result is same after inverse
|
||||
self.assertEqual(
|
||||
self.partner_model.search(and_domains),
|
||||
self.partner_model.search(AND(partner_domains))
|
||||
self.partner_model.search(AND(partner_domains)),
|
||||
)
|
||||
|
||||
def test_inverse_partner_domain_or(self):
|
||||
|
@ -36,14 +36,14 @@ class TestPartnerDomains(SavepointCase):
|
|||
# Ensure result is same after inverse
|
||||
self.assertEqual(
|
||||
self.partner_model.search(or_domains),
|
||||
self.partner_model.search(OR(partner_domains))
|
||||
self.partner_model.search(OR(partner_domains)),
|
||||
)
|
||||
|
||||
def test_inverse_partner_domain_or_subdomain_and(self):
|
||||
partner_domains_2 = [
|
||||
[("name", "ilike", "Gemini")],
|
||||
[("email", "ilike", "example.com")],
|
||||
[("country_id", "=", self.env.ref("base.us").id)]
|
||||
[("country_id", "=", self.env.ref("base.us").id)],
|
||||
]
|
||||
composed_domain = OR([AND(self.partner_domains), AND(partner_domains_2)])
|
||||
decomposed_or_domains = inverse_OR(composed_domain)
|
||||
|
@ -56,14 +56,14 @@ class TestPartnerDomains(SavepointCase):
|
|||
self.partner_model.search(composed_domain),
|
||||
self.partner_model.search(
|
||||
OR([AND(decomposed_and_domains_1), AND(decomposed_and_domains_2)])
|
||||
)
|
||||
),
|
||||
)
|
||||
|
||||
def test_inverse_partner_domain_and_subdomain_or(self):
|
||||
partner_domains_2 = [
|
||||
[("name", "ilike", "Gemini")],
|
||||
[("email", "ilike", "example.com")],
|
||||
[("country_id", "=", self.env.ref("base.us").id)]
|
||||
[("country_id", "=", self.env.ref("base.us").id)],
|
||||
]
|
||||
composed_domain = AND([OR(self.partner_domains), OR(partner_domains_2)])
|
||||
decomposed_and_domains = inverse_AND(composed_domain)
|
||||
|
@ -76,5 +76,5 @@ class TestPartnerDomains(SavepointCase):
|
|||
self.partner_model.search(composed_domain),
|
||||
self.partner_model.search(
|
||||
OR([AND(decomposed_or_domains_1), AND(decomposed_or_domains_2)])
|
||||
)
|
||||
),
|
||||
)
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
../../../../base_domain_inverse_function
|
|
@ -0,0 +1,6 @@
|
|||
import setuptools
|
||||
|
||||
setuptools.setup(
|
||||
setup_requires=['setuptools-odoo'],
|
||||
odoo_addon=True,
|
||||
)
|
Loading…
Reference in New Issue