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.
20 ADMIN_USER_ID = common.ADMIN_USER_ID
23 common.create_xmlrpc_proxies()
25 tearDownModule = common.tearDownModule
28 return openerp.modules.registry.RegistryManager.get(DB)[model]
31 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
33 class test_ir_sequence_standard(unittest2.TestCase):
34 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """
36 def test_ir_sequence_create(self):
37 """ Try to create a sequence object. """
39 d = dict(code='test_sequence_type', name='Test sequence type')
40 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
42 d = dict(code='test_sequence_type', name='Test sequence')
43 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
48 def test_ir_sequence_search(self):
51 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})
56 def test_ir_sequence_draw(self):
57 """ Try to draw a number. """
59 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type', {})
64 def test_ir_sequence_draw_twice(self):
65 """ Try to draw a number from two transactions. """
68 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type', {})
70 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type', {})
77 class test_ir_sequence_no_gap(unittest2.TestCase):
78 """ Copy of the previous tests for a 'No gap' sequence. """
80 def test_ir_sequence_create_no_gap(self):
81 """ Try to create a sequence object. """
83 d = dict(code='test_sequence_type_2', name='Test sequence type')
84 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
86 d = dict(code='test_sequence_type_2', name='Test sequence',
87 implementation='no_gap')
88 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
93 def test_ir_sequence_draw_no_gap(self):
94 """ Try to draw a number. """
96 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})
101 def test_ir_sequence_draw_twice_no_gap(self):
102 """ Try to draw a number from two transactions.
103 This is expected to not work.
107 msg_re = '^could not obtain lock on row in relation "ir_sequence"$'
108 with self.assertRaisesRegexp(psycopg2.OperationalError, msg_re):
109 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type_2', {})
111 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type_2', {})
115 class test_ir_sequence_change_implementation(unittest2.TestCase):
116 """ Create sequence objects and change their ``implementation`` field. """
118 def test_ir_sequence_1_create(self):
119 """ Try to create a sequence object. """
121 d = dict(code='test_sequence_type_3', name='Test sequence type')
122 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
124 d = dict(code='test_sequence_type_3', name='Test sequence')
125 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
127 d = dict(code='test_sequence_type_4', name='Test sequence type')
128 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
130 d = dict(code='test_sequence_type_4', name='Test sequence',
131 implementation='no_gap')
132 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
137 def test_ir_sequence_2_write(self):
139 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
140 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
141 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
142 {'implementation': 'standard'}, {})
143 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
144 {'implementation': 'no_gap'}, {})
148 def test_ir_sequence_3_unlink(self):
150 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
151 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
152 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})
156 class test_ir_sequence_generate(unittest2.TestCase):
157 """ Create sequence objects and generate some values. """
159 def test_ir_sequence_create(self):
160 """ Try to create a sequence object. """
162 d = dict(code='test_sequence_type_5', name='Test sequence type')
163 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
165 d = dict(code='test_sequence_type_5', name='Test sequence')
166 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
172 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})
173 assert all(str(x) == f() for x in xrange(1,1000))
177 def test_ir_sequence_create_no_gap(self):
178 """ Try to create a sequence object. """
180 d = dict(code='test_sequence_type_6', name='Test sequence type',
181 implementation='no_gap')
182 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
184 d = dict(code='test_sequence_type_6', name='Test sequence')
185 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
191 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})
192 assert all(str(x) == f() for x in xrange(1,1000))
198 if __name__ == '__main__':
202 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: