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
22 setUpModule = common.setUpModule
23 tearDownModule = common.tearDownModule
26 return openerp.modules.registry.RegistryManager.get(DB)[model]
29 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
31 class test_ir_sequence_standard(unittest2.TestCase):
32 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """
34 def test_ir_sequence_create(self):
35 """ Try to create a sequence object. """
37 d = dict(code='test_sequence_type', name='Test sequence type')
38 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
40 d = dict(code='test_sequence_type', name='Test sequence')
41 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
46 def test_ir_sequence_search(self):
49 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})
54 def test_ir_sequence_draw(self):
55 """ Try to draw a number. """
57 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type', {})
62 def test_ir_sequence_draw_twice(self):
63 """ Try to draw a number from two transactions. """
66 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type', {})
68 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type', {})
75 class test_ir_sequence_no_gap(unittest2.TestCase):
76 """ Copy of the previous tests for a 'No gap' sequence. """
78 def test_ir_sequence_create_no_gap(self):
79 """ Try to create a sequence object. """
81 d = dict(code='test_sequence_type_2', name='Test sequence type')
82 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
84 d = dict(code='test_sequence_type_2', name='Test sequence',
85 implementation='no_gap')
86 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
91 def test_ir_sequence_draw_no_gap(self):
92 """ Try to draw a number. """
94 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})
99 def test_ir_sequence_draw_twice_no_gap(self):
100 """ Try to draw a number from two transactions.
101 This is expected to not work.
105 msg_re = '^could not obtain lock on row in relation "ir_sequence"$'
106 with self.assertRaisesRegexp(psycopg2.OperationalError, msg_re):
107 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type_2', {})
109 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type_2', {})
113 class test_ir_sequence_change_implementation(unittest2.TestCase):
114 """ Create sequence objects and change their ``implementation`` field. """
116 def test_ir_sequence_1_create(self):
117 """ Try to create a sequence object. """
119 d = dict(code='test_sequence_type_3', name='Test sequence type')
120 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
122 d = dict(code='test_sequence_type_3', name='Test sequence')
123 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
125 d = dict(code='test_sequence_type_4', name='Test sequence type')
126 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
128 d = dict(code='test_sequence_type_4', name='Test sequence',
129 implementation='no_gap')
130 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
135 def test_ir_sequence_2_write(self):
137 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
138 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
139 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
140 {'implementation': 'standard'}, {})
141 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
142 {'implementation': 'no_gap'}, {})
146 def test_ir_sequence_3_unlink(self):
148 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
149 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
150 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})
154 class test_ir_sequence_generate(unittest2.TestCase):
155 """ Create sequence objects and generate some values. """
157 def test_ir_sequence_create(self):
158 """ Try to create a sequence object. """
160 d = dict(code='test_sequence_type_5', name='Test sequence type')
161 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
163 d = dict(code='test_sequence_type_5', name='Test sequence')
164 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
170 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})
171 assert all(str(x) == f() for x in xrange(1,1000))
175 def test_ir_sequence_create_no_gap(self):
176 """ Try to create a sequence object. """
178 d = dict(code='test_sequence_type_6', name='Test sequence type',
179 implementation='no_gap')
180 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
182 d = dict(code='test_sequence_type_6', name='Test sequence')
183 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
189 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})
190 assert all(str(x) == f() for x in xrange(1,1000))
196 if __name__ == '__main__':