1 # -*- coding: utf-8 -*-
2 # Run with one of these commands:
3 # > OPENERP_ADDONS_PATH='../../addons/trunk' OPENERP_PORT=8069 \
4 # OPENERP_DATABASE=yy PYTHONPATH=. python tests/test_ir_sequence.py
5 # > OPENERP_ADDONS_PATH='../../addons/trunk' OPENERP_PORT=8069 \
6 # OPENERP_DATABASE=yy nosetests tests/test_ir_sequence.py
7 # > OPENERP_ADDONS_PATH='../../../addons/trunk' OPENERP_PORT=8069 \
8 # OPENERP_DATABASE=yy PYTHONPATH=../:. unit2 test_ir_sequence
9 # This assume an existing database.
17 ADMIN_USER_ID = common.ADMIN_USER_ID
20 common.create_xmlrpc_proxies()
22 tearDownModule = common.tearDownModule
25 return openerp.modules.registry.RegistryManager.get(DB)[model]
28 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
30 class test_ir_sequence_standard(unittest2.TestCase):
31 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """
33 def test_ir_sequence_create(self):
34 """ Try to create a sequence object. """
36 d = dict(code='test_sequence_type', name='Test sequence type')
37 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
39 d = dict(code='test_sequence_type', name='Test sequence')
40 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
45 def test_ir_sequence_search(self):
48 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})
53 def test_ir_sequence_draw(self):
54 """ Try to draw a number. """
56 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type', {})
61 def test_ir_sequence_draw_twice(self):
62 """ Try to draw a number from two transactions. """
65 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type', {})
67 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type', {})
74 class test_ir_sequence_no_gap(unittest2.TestCase):
75 """ Copy of the previous tests for a 'No gap' sequence. """
77 def test_ir_sequence_create_no_gap(self):
78 """ Try to create a sequence object. """
80 d = dict(code='test_sequence_type_2', name='Test sequence type')
81 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
83 d = dict(code='test_sequence_type_2', name='Test sequence',
84 implementation='no_gap')
85 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
90 def test_ir_sequence_draw_no_gap(self):
91 """ Try to draw a number. """
93 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})
98 def test_ir_sequence_draw_twice_no_gap(self):
99 """ Try to draw a number from two transactions.
100 This is expected to not work.
104 msg_re = '^could not obtain lock on row in relation "ir_sequence"$'
105 with self.assertRaisesRegexp(psycopg2.OperationalError, msg_re):
106 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type_2', {})
108 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type_2', {})
112 class test_ir_sequence_change_implementation(unittest2.TestCase):
113 """ Create sequence objects and change their ``implementation`` field. """
115 def test_ir_sequence_1_create(self):
116 """ Try to create a sequence object. """
118 d = dict(code='test_sequence_type_3', name='Test sequence type')
119 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
121 d = dict(code='test_sequence_type_3', name='Test sequence')
122 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
124 d = dict(code='test_sequence_type_4', name='Test sequence type')
125 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
127 d = dict(code='test_sequence_type_4', name='Test sequence',
128 implementation='no_gap')
129 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
134 def test_ir_sequence_2_write(self):
136 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
137 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
138 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
139 {'implementation': 'standard'}, {})
140 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
141 {'implementation': 'no_gap'}, {})
145 def test_ir_sequence_3_unlink(self):
147 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
148 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
149 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})
153 class test_ir_sequence_generate(unittest2.TestCase):
154 """ Create sequence objects and generate some values. """
156 def test_ir_sequence_create(self):
157 """ Try to create a sequence object. """
159 d = dict(code='test_sequence_type_5', name='Test sequence type')
160 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
162 d = dict(code='test_sequence_type_5', name='Test sequence')
163 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
169 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})
170 assert all(str(x) == f() for x in xrange(1,1000))
174 def test_ir_sequence_create_no_gap(self):
175 """ Try to create a sequence object. """
177 d = dict(code='test_sequence_type_6', name='Test sequence type',
178 implementation='no_gap')
179 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
181 d = dict(code='test_sequence_type_6', name='Test sequence')
182 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
188 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})
189 assert all(str(x) == f() for x in xrange(1,1000))
195 if __name__ == '__main__':
199 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: