[IMP] orm: fix and generalize method resolve_o2m_commands_to_record_dicts (now named...
[odoo/odoo.git] / openerp / tests / test_orm.py
1 import os
2 import unittest2
3
4 import openerp
5 import common
6
7 UID = common.ADMIN_USER_ID
8 DB = common.DB
9
10 CREATE = lambda values: (0, False, values)
11 UPDATE = lambda id, values: (1, id, values)
12 DELETE = lambda id: (2, id, False)
13 FORGET = lambda id: (3, id, False)
14 LINK_TO = lambda id: (4, id, False)
15 DELETE_ALL = lambda: (5, False, False)
16 REPLACE_WITH = lambda ids: (6, False, ids)
17
18 class TestO2MSerialization(common.TransactionCase):
19
20     def setUp(self):
21         super(TestO2MSerialization, self).setUp()
22         self.partner = self.registry('res.partner')
23
24     def test_no_command(self):
25         " empty list of commands yields an empty list of records "
26         results = self.partner.resolve_2many_commands(
27             self.cr, UID, 'address', [])
28
29         self.assertEqual(results, [])
30
31     def test_CREATE_commands(self):
32         " returns the VALUES dict as-is "
33         results = self.partner.resolve_2many_commands(
34             self.cr, UID, 'address',
35             map(CREATE, [{'foo': 'bar'}, {'foo': 'baz'}, {'foo': 'baq'}]))
36         self.assertEqual(results, [
37             {'foo': 'bar'},
38             {'foo': 'baz'},
39             {'foo': 'baq'}
40         ])
41
42     def test_LINK_TO_command(self):
43         " reads the records from the database, records are returned with their ids. "
44         ids = [
45             self.partner.create(self.cr, UID, {'name': 'foo'}),
46             self.partner.create(self.cr, UID, {'name': 'bar'}),
47             self.partner.create(self.cr, UID, {'name': 'baz'})
48         ]
49         commands = map(LINK_TO, ids)
50
51         results = self.partner.resolve_2many_commands(
52             self.cr, UID, 'address', commands, ['name'])
53
54         self.assertEqual(results, [
55             {'id': ids[0], 'name': 'foo'},
56             {'id': ids[1], 'name': 'bar'},
57             {'id': ids[2], 'name': 'baz'}
58         ])
59
60     def test_bare_ids_command(self):
61         " same as the equivalent LINK_TO commands "
62         ids = [
63             self.partner.create(self.cr, UID, {'name': 'foo'}),
64             self.partner.create(self.cr, UID, {'name': 'bar'}),
65             self.partner.create(self.cr, UID, {'name': 'baz'})
66         ]
67
68         results = self.partner.resolve_2many_commands(
69             self.cr, UID, 'address', ids, ['name'])
70
71         self.assertEqual(results, [
72             {'id': ids[0], 'name': 'foo'},
73             {'id': ids[1], 'name': 'bar'},
74             {'id': ids[2], 'name': 'baz'}
75         ])
76
77     def test_UPDATE_command(self):
78         " take the in-db records and merge the provided information in "
79         id_foo = self.partner.create(self.cr, UID, {'name': 'foo'})
80         id_bar = self.partner.create(self.cr, UID, {'name': 'bar'})
81         id_baz = self.partner.create(self.cr, UID, {'name': 'baz', 'city': 'tag'})
82
83         results = self.partner.resolve_2many_commands(
84             self.cr, UID, 'address', [
85                 LINK_TO(id_foo),
86                 UPDATE(id_bar, {'name': 'qux', 'city': 'tagtag'}),
87                 UPDATE(id_baz, {'name': 'quux'})
88             ], ['name', 'city'])
89
90         self.assertEqual(results, [
91             {'id': id_foo, 'name': 'foo', 'city': False},
92             {'id': id_bar, 'name': 'qux', 'city': 'tagtag'},
93             {'id': id_baz, 'name': 'quux', 'city': 'tag'}
94         ])
95
96     def test_mixed_commands(self):
97         ids = [
98             self.partner.create(self.cr, UID, {'name': name})
99             for name in ['NObar', 'baz', 'qux', 'NOquux', 'NOcorge', 'garply']
100         ]
101
102         results = self.partner.resolve_2many_commands(
103             self.cr, UID, 'address', [
104                 CREATE({'name': 'foo'}),
105                 UPDATE(ids[0], {'name': 'bar'}),
106                 LINK_TO(ids[1]),
107                 LINK_TO(ids[2]),
108                 UPDATE(ids[3], {'name': 'quux',}),
109                 UPDATE(ids[4], {'name': 'corge'}),
110                 CREATE({'name': 'grault'}),
111                 LINK_TO(ids[5])
112             ], ['name'])
113
114         self.assertEqual(results, [
115             {'name': 'foo'},
116             {'id': ids[0], 'name': 'bar'},
117             {'id': ids[1], 'name': 'baz'},
118             {'id': ids[2], 'name': 'qux'},
119             {'id': ids[3], 'name': 'quux'},
120             {'id': ids[4], 'name': 'corge'},
121             {'name': 'grault'},
122             {'id': ids[5], 'name': 'garply'}
123         ])
124
125     def test_LINK_TO_pairs(self):
126         "LINK_TO commands can be written as pairs, instead of triplets"
127         ids = [
128             self.partner.create(self.cr, UID, {'name': 'foo'}),
129             self.partner.create(self.cr, UID, {'name': 'bar'}),
130             self.partner.create(self.cr, UID, {'name': 'baz'})
131         ]
132         commands = map(lambda id: (4, id), ids)
133
134         results = self.partner.resolve_2many_commands(
135             self.cr, UID, 'address', commands, ['name'])
136
137         self.assertEqual(results, [
138             {'id': ids[0], 'name': 'foo'},
139             {'id': ids[1], 'name': 'bar'},
140             {'id': ids[2], 'name': 'baz'}
141         ])
142
143     def test_singleton_commands(self):
144         "DELETE_ALL can appear as a singleton"
145
146         try:
147             self.partner.resolve_2many_commands(
148                 self.cr, UID, 'address', [(5,)], ['name'])
149         except AssertionError:
150             # 5 should fail with an assert error, but not e.g. a ValueError
151             pass
152
153     def test_invalid_commands(self):
154         "Commands with uncertain semantics in this context should be forbidden"
155
156         with self.assertRaises(AssertionError):
157             self.partner.resolve_2many_commands(
158                 self.cr, UID, 'address', [DELETE(42)], ['name'])
159
160         with self.assertRaises(AssertionError):
161             self.partner.resolve_2many_commands(
162                 self.cr, UID, 'address', [FORGET(42)], ['name'])
163
164         with self.assertRaises(AssertionError):
165             self.partner.resolve_2many_commands(
166                 self.cr, UID, 'address', [DELETE_ALL()], ['name'])
167
168         with self.assertRaises(AssertionError):
169             self.partner.resolve_2many_commands(
170                 self.cr, UID, 'address', [REPLACE_WITH([42])], ['name'])
171
172
173 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: