Launchpad automatic translations update.
[odoo/odoo.git] / openerp / addons / base / tests / test_search.py
1 import unittest2
2
3 import openerp.tests.common as common
4
5
6 class test_search(common.TransactionCase):
7
8     def test_00_search_order(self):
9
10         registry, cr, uid = self.registry, self.cr, self.uid
11
12         # Create 6 partners with a given name, and a given creation order to
13         # ensure the order of their ID. Some are set as unactive to verify they
14         # are by default excluded from the searches and to provide a second
15         # `order` argument.
16
17         partners = registry('res.partner')
18         c = partners.create(cr, uid, {'name': 'test_search_order_C'})
19         d = partners.create(cr, uid, {'name': 'test_search_order_D', 'active': False})
20         a = partners.create(cr, uid, {'name': 'test_search_order_A'})
21         b = partners.create(cr, uid, {'name': 'test_search_order_B'})
22         ab = partners.create(cr, uid, {'name': 'test_search_order_AB'})
23         e = partners.create(cr, uid, {'name': 'test_search_order_E', 'active': False})
24
25         # The tests.
26
27         # The basic searches should exclude records that have active = False.
28         # The order of the returned ids should be given by the `order`
29         # parameter of search().
30
31         name_asc = partners.search(cr, uid, [('name', 'like', 'test_search_order%')], order="name asc")
32         self.assertEqual([a, ab, b, c], name_asc, "Search with 'NAME ASC' order failed.")
33         name_desc = partners.search(cr, uid, [('name', 'like', 'test_search_order%')], order="name desc")
34         self.assertEqual([c, b, ab, a], name_desc, "Search with 'NAME DESC' order failed.")
35         id_asc = partners.search(cr, uid, [('name', 'like', 'test_search_order%')], order="id asc")
36         self.assertEqual([c, a, b, ab], id_asc, "Search with 'ID ASC' order failed.")
37         id_desc = partners.search(cr, uid, [('name', 'like', 'test_search_order%')], order="id desc")
38         self.assertEqual([ab, b, a, c], id_desc, "Search with 'ID DESC' order failed.")
39
40         # The inactive records shouldn't be excluded as soon as a condition on
41         # that field is present in the domain. The `order` parameter of
42         # search() should support any legal coma-separated values.
43
44         active_asc_id_asc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="active asc, id asc")
45         self.assertEqual([d, e, c, a, b, ab], active_asc_id_asc, "Search with 'ACTIVE ASC, ID ASC' order failed.")
46         active_desc_id_asc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="active desc, id asc")
47         self.assertEqual([c, a, b, ab, d, e], active_desc_id_asc, "Search with 'ACTIVE DESC, ID ASC' order failed.")
48         active_asc_id_desc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="active asc, id desc")
49         self.assertEqual([e, d, ab, b, a, c], active_asc_id_desc, "Search with 'ACTIVE ASC, ID DESC' order failed.")
50         active_desc_id_desc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="active desc, id desc")
51         self.assertEqual([ab, b, a, c, e, d], active_desc_id_desc, "Search with 'ACTIVE DESC, ID DESC' order failed.")
52         id_asc_active_asc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="id asc, active asc")
53         self.assertEqual([c, d, a, b, ab, e], id_asc_active_asc, "Search with 'ID ASC, ACTIVE ASC' order failed.")
54         id_asc_active_desc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="id asc, active desc")
55         self.assertEqual([c, d, a, b, ab, e], id_asc_active_desc, "Search with 'ID ASC, ACTIVE DESC' order failed.")
56         id_desc_active_asc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="id desc, active asc")
57         self.assertEqual([e, ab, b, a, d, c], id_desc_active_asc, "Search with 'ID DESC, ACTIVE ASC' order failed.")
58         id_desc_active_desc = partners.search(cr, uid, [('name', 'like', 'test_search_order%'), '|', ('active', '=', True), ('active', '=', False)], order="id desc, active desc")
59         self.assertEqual([e, ab, b, a, d, c], id_desc_active_desc, "Search with 'ID DESC, ACTIVE DESC' order failed.")
60
61     def test_10_inherits_m2order(self):
62         registry, cr, uid = self.registry, self.cr, self.uid
63         users_obj = registry('res.users')
64
65         # Find Employee group
66         group_employee_ref = self.registry('ir.model.data').get_object_reference(cr, uid, 'base', 'group_user')
67         group_employee_id = group_employee_ref and group_employee_ref[1] or False
68
69         # Get country/state data
70         country_us_id = registry('res.country').search(cr, uid, [('code', 'like', 'US')])[0]
71         state_ids = registry('res.country.state').search(cr, uid, [('country_id', '=', country_us_id)], limit=2)
72         country_be_id = registry('res.country').search(cr, uid, [('code', 'like', 'BE')])[0]
73
74         # Create test users
75         search_user = users_obj.create(cr, uid, {'name': '__search', 'login': '__search', 'groups_id': [(6, 0, [group_employee_id])]})
76         a = users_obj.create(cr, uid, {'name': '__test_A', 'login': '__test_A', 'country_id': country_be_id, 'state_id': country_be_id})
77         b = users_obj.create(cr, uid, {'name': '__test_B', 'login': '__a_test_B', 'country_id': country_us_id, 'state_id': state_ids[1]})
78         c = users_obj.create(cr, uid, {'name': '__test_B', 'login': '__z_test_B', 'country_id': country_us_id, 'state_id': state_ids[0]})
79
80         # Do: search on res.users, order on a field on res.partner to try inherits'd fields, then res.users
81         user_ids = users_obj.search(cr, search_user, [], order='name asc, login desc')
82         expected_ids = [search_user, a, c, b]
83         test_user_ids = filter(lambda x: x in expected_ids, user_ids)
84         self.assertEqual(test_user_ids, expected_ids, 'search on res_users did not provide expected ids or expected order')
85
86         # Do: order on many2one and inherits'd fields
87         user_ids = users_obj.search(cr, search_user, [], order='state_id asc, country_id desc, name asc, login desc')
88         expected_ids = [c, b, a, search_user]
89         test_user_ids = filter(lambda x: x in expected_ids, user_ids)
90         self.assertEqual(test_user_ids, expected_ids, 'search on res_users did not provide expected ids or expected order')
91
92         # Do: order on many2one and inherits'd fields
93         user_ids = users_obj.search(cr, search_user, [], order='country_id desc, state_id desc, name asc, login desc')
94         expected_ids = [search_user, b, c, a]
95         test_user_ids = filter(lambda x: x in expected_ids, user_ids)
96         self.assertEqual(test_user_ids, expected_ids, 'search on res_users did not provide expected ids or expected order')
97
98         # Do: order on many2one, but not by specifying in order parameter of search, but by overriding _order of res_users
99         old_order = users_obj._order
100         users_obj._order = 'country_id desc, name asc, login desc'
101         user_ids = users_obj.search(cr, search_user, [])
102         expected_ids = [search_user, c, b, a]
103         test_user_ids = filter(lambda x: x in expected_ids, user_ids)
104         self.assertEqual(test_user_ids, expected_ids, 'search on res_users did not provide expected ids or expected order')
105         users_obj._order = old_order
106
107
108 if __name__ == '__main__':
109     unittest2.main()