diff options
| author | Xavier Morel <xmo@openerp.com> | 2011-10-10 15:51:57 +0200 |
|---|---|---|
| committer | Xavier Morel <xmo@openerp.com> | 2011-10-10 15:51:57 +0200 |
| commit | d5accc0faea908edf758a4da22bcd4bc01485ca2 (patch) | |
| tree | 1c268d07befa1bd0000b586c1216db66339f57b5 /tests | |
| parent | 8a5314b15e6010f6f9718facab88989c48c11be5 (diff) | |
[ADD] method to correctly serialize o2m commands into a sequence of record-dicts for e.g. onchanges
also alters res.partner.bank _default_value to use it with res.partner addresses, and adds a few tests to ensure behavior is correct
bzr revid: xmo@openerp.com-20111010135157-dafgcwwcni5gkeom
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_orm.py | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/test_orm.py b/tests/test_orm.py new file mode 100644 index 00000000000..67b97b894d6 --- /dev/null +++ b/tests/test_orm.py @@ -0,0 +1,121 @@ +import os +import unittest2 +import openerp + +UID = 1 +DB = os.environ['OPENERP_DATABASE'] + +CREATE = lambda values: (0, False, values) +UPDATE = lambda id, values: (1, id, values) +LINK_TO = lambda id: (4, id, False) + + +def setUpModule(): + openerp.tools.config['addons_path'] = os.environ['OPENERP_ADDONS_PATH'] + +class TestO2MSerialization(unittest2.TestCase): + def setUp(self): + self.cr = openerp.modules.registry.RegistryManager.get(DB).db.cursor() + self.partner = openerp.modules.registry.RegistryManager.get(DB)['res.partner'] + self.address = openerp.modules.registry.RegistryManager.get(DB)['res.partner.address'] + def tearDown(self): + self.cr.rollback() + self.cr.close() + + def test_no_command(self): + " empty list of commands yields an empty list of records " + results = list(self.partner.serialize_o2m_commands(self.cr, UID, 'address', [])) + + self.assertEqual(results, []) + + def test_CREATE_commands(self): + " returns the VALUES dict as-is " + results = list(self.partner.serialize_o2m_commands( + self.cr, UID, 'address', + map(CREATE, [{'foo': 'bar'}, {'foo': 'baz'}, {'foo': 'baq'}]))) + self.assertEqual(results, [ + {'foo': 'bar'}, + {'foo': 'baz'}, + {'foo': 'baq'} + ]) + + def test_LINK_TO_command(self): + " reads the records from the database, records are returned with their ids. " + ids = [ + self.address.create(self.cr, UID, {'name': 'foo'}), + self.address.create(self.cr, UID, {'name': 'bar'}), + self.address.create(self.cr, UID, {'name': 'baz'}) + ] + commands = map(LINK_TO, ids) + + results = list(self.partner.serialize_o2m_commands(self.cr, UID, 'address', commands, ['name'])) + + self.assertEqual(results, [ + {'id': ids[0], 'name': 'foo'}, + {'id': ids[1], 'name': 'bar'}, + {'id': ids[2], 'name': 'baz'} + ]) + + def test_bare_ids_command(self): + " same as the equivalent LINK_TO commands " + ids = [ + self.address.create(self.cr, UID, {'name': 'foo'}), + self.address.create(self.cr, UID, {'name': 'bar'}), + self.address.create(self.cr, UID, {'name': 'baz'}) + ] + + results = list(self.partner.serialize_o2m_commands(self.cr, UID, 'address', ids, ['name'])) + + self.assertEqual(results, [ + {'id': ids[0], 'name': 'foo'}, + {'id': ids[1], 'name': 'bar'}, + {'id': ids[2], 'name': 'baz'} + ]) + + def test_UPDATE_command(self): + " take the in-db records and merge the provided information in " + id_foo = self.address.create(self.cr, UID, {'name': 'foo'}) + id_bar = self.address.create(self.cr, UID, {'name': 'bar'}) + id_baz = self.address.create(self.cr, UID, {'name': 'baz', 'city': 'tag'}) + + results = list(self.partner.serialize_o2m_commands( + self.cr, UID, 'address', [ + LINK_TO(id_foo), + UPDATE(id_bar, {'name': 'qux', 'city': 'tagtag'}), + UPDATE(id_baz, {'name': 'quux'}) + ], ['name', 'city'])) + + self.assertEqual(results, [ + {'id': id_foo, 'name': 'foo', 'city': False}, + {'id': id_bar, 'name': 'qux', 'city': 'tagtag'}, + {'id': id_baz, 'name': 'quux', 'city': 'tag'} + ]) + + def test_mixed_commands(self): + ids = [ + self.address.create(self.cr, UID, {'name': name}) + for name in ['NObar', 'baz', 'qux', 'NOquux', 'NOcorge', 'garply'] + ] + + results = list(self.partner.serialize_o2m_commands( + self.cr, UID, 'address', [ + CREATE({'name': 'foo'}), + UPDATE(ids[0], {'name': 'bar'}), + LINK_TO(ids[1]), + LINK_TO(ids[2]), + UPDATE(ids[3], {'name': 'quux',}), + UPDATE(ids[4], {'name': 'corge'}), + CREATE({'name': 'grault'}), + LINK_TO(ids[5]) + ], ['name'])) + + self.assertEqual(results, [ + {'name': 'foo'}, + {'id': ids[0], 'name': 'bar'}, + {'id': ids[1], 'name': 'baz'}, + {'id': ids[2], 'name': 'qux'}, + {'id': ids[3], 'name': 'quux'}, + {'id': ids[4], 'name': 'corge'}, + {'name': 'grault'}, + {'id': ids[5], 'name': 'garply'} + ]) |
