Files
odoo_source/odoo/addons/base/tests/test_func.py
T
Gorash b376a0deb5 [IMP] base: comparison between two lazy values is possible.
Before this commit, two lazy values could not always be compared. Indeed,
the comparison did indeed use the value for the self, but not the other.
A normally true comparison was returned as false. Inverting the values
causes the code to pass through the lazy functions of the objects. For
example, if we go through `__lt__` the fact of having reversed the
values means that we will necessarily go through the `__gt__` of the
other.

Part-of: odoo/odoo#88276
2022-06-03 16:40:38 +02:00

93 lines
3.3 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
import functools
from odoo.tests.common import BaseCase
from odoo.tools import frozendict, lazy
from odoo.tools.func import compose
from odoo import Command
class TestCompose(BaseCase):
def test_basic(self):
str_add = compose(str, lambda a, b: a + b)
self.assertEqual(str_add(1, 2), "3")
def test_decorator(self):
""" ensure compose() can be partially applied as a decorator
"""
@functools.partial(compose, str)
def mul(a, b):
return a * b
self.assertEqual(mul(5, 42), u"210")
class TestFrozendict(BaseCase):
def test_frozendict_immutable(self):
""" Ensure that a frozendict is immutable. """
vals = {'name': 'Joe', 'age': 42}
frozen_vals = frozendict(vals)
# check __setitem__, __delitem__
with self.assertRaises(Exception):
frozen_vals['surname'] = 'Jack'
with self.assertRaises(Exception):
frozen_vals['name'] = 'Jack'
with self.assertRaises(Exception):
del frozen_vals['name']
# check update, setdefault, pop, popitem, clear
with self.assertRaises(Exception):
frozen_vals.update({'surname': 'Jack'})
with self.assertRaises(Exception):
frozen_vals.update({'name': 'Jack'})
with self.assertRaises(Exception):
frozen_vals.setdefault('surname', 'Jack')
with self.assertRaises(Exception):
frozen_vals.pop('surname', 'Jack')
with self.assertRaises(Exception):
frozen_vals.pop('name', 'Jack')
with self.assertRaises(Exception):
frozen_vals.popitem()
with self.assertRaises(Exception):
frozen_vals.clear()
def test_frozendict_hash(self):
""" Ensure that a frozendict is hashable. """
# dict with simple values
hash(frozendict({'name': 'Joe', 'age': 42}))
# dict with tuples, lists, and embedded dicts
hash(frozendict({
'user_id': (42, 'Joe'),
'line_ids': [Command.create({'values': [42]})],
}))
class TestLazy(BaseCase):
def test_lazy_compare(self):
""" Ensure that a lazy can be compared with an other lazy. """
self.assertEqual(lazy(lambda: 1) <= lazy(lambda: 42), True)
self.assertEqual(lazy(lambda: 42) <= lazy(lambda: 1), False)
self.assertEqual(lazy(lambda: 42) == lazy(lambda: 42), True)
self.assertEqual(lazy(lambda: 1) == lazy(lambda: 42), False)
self.assertEqual(lazy(lambda: 42) != lazy(lambda: 42), False)
self.assertEqual(lazy(lambda: 1) != lazy(lambda: 42), True)
# Object like recordset implement __eq__
class Obj:
def __init__(self, num):
self.num = num
def __eq__(self, other):
if isinstance(other, Obj):
return self.num == other.num
raise ValueError('Object does not have the correct type')
self.assertEqual(lazy(lambda: Obj(42)) == lazy(lambda: Obj(42)), True)
self.assertEqual(lazy(lambda: Obj(1)) == lazy(lambda: Obj(42)), False)
self.assertEqual(lazy(lambda: Obj(42)) != lazy(lambda: Obj(42)), False)
self.assertEqual(lazy(lambda: Obj(1)) != lazy(lambda: Obj(42)), True)