diff options
| author | Raphael Collet <rco@openerp.com> | 2016-09-02 15:58:39 +0200 |
|---|---|---|
| committer | Raphael Collet <rco@openerp.com> | 2016-09-02 17:28:12 +0200 |
| commit | 9e64f9f9514194d74e166b032add8511182f2677 (patch) | |
| tree | 5086710c04c6b923be18a6d1d97b3f0f3a2a9c4a /odoo/addons/base/tests/test_func.py | |
| parent | 11812b0b9e82b2b1b9041d8250ce01e7b4a5f7ee (diff) | |
[REF] openerp: move `openerp` to `odoo`
Diffstat (limited to 'odoo/addons/base/tests/test_func.py')
| -rw-r--r-- | odoo/addons/base/tests/test_func.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/odoo/addons/base/tests/test_func.py b/odoo/addons/base/tests/test_func.py new file mode 100644 index 00000000000..ac394cb9b79 --- /dev/null +++ b/odoo/addons/base/tests/test_func.py @@ -0,0 +1,65 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +import functools +import unittest + +from odoo.tools import frozendict +from odoo.tools.func import compose + + +class TestCompose(unittest.TestCase): + 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, unicode) + def mul(a, b): + return a * b + + self.assertEqual(mul(5, 42), u"210") + + +class TestFrozendict(unittest.TestCase): + 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': [(0, 0, {'values': [42]})], + })) |
