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