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.
11 import psycopg2.errorcodes
18 ADMIN_USER_ID = common.ADMIN_USER_ID
21 return openerp.modules.registry.RegistryManager.get(DB)[model]
24 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
27 def drop_sequence(code):
29 for model in ['ir.sequence', 'ir.sequence.type']:
31 ids = s.search(cr, ADMIN_USER_ID, [('code', '=', code)])
32 s.unlink(cr, ADMIN_USER_ID, ids)
36 class test_ir_sequence_standard(unittest2.TestCase):
37 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """
39 def test_ir_sequence_create(self):
40 """ Try to create a sequence object. """
42 d = dict(code='test_sequence_type', name='Test sequence type')
43 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
45 d = dict(code='test_sequence_type', name='Test sequence')
46 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
51 def test_ir_sequence_search(self):
54 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})
59 def test_ir_sequence_draw(self):
60 """ Try to draw a number. """
62 n = registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type', {})
67 def test_ir_sequence_draw_twice(self):
68 """ Try to draw a number from two transactions. """
71 n0 = registry('ir.sequence').next_by_code(cr0, ADMIN_USER_ID, 'test_sequence_type', {})
73 n1 = registry('ir.sequence').next_by_code(cr1, ADMIN_USER_ID, 'test_sequence_type', {})
81 def tearDownClass(cls):
82 drop_sequence('test_sequence_type')
84 class test_ir_sequence_no_gap(unittest2.TestCase):
85 """ Copy of the previous tests for a 'No gap' sequence. """
87 def test_ir_sequence_create_no_gap(self):
88 """ Try to create a sequence object. """
90 d = dict(code='test_sequence_type_2', name='Test sequence type')
91 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
93 d = dict(code='test_sequence_type_2', name='Test sequence',
94 implementation='no_gap')
95 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
100 def test_ir_sequence_draw_no_gap(self):
101 """ Try to draw a number. """
103 n = registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})
108 def test_ir_sequence_draw_twice_no_gap(self):
109 """ Try to draw a number from two transactions.
110 This is expected to not work.
114 cr1._default_log_exceptions = False # Prevent logging a traceback
115 with self.assertRaises(psycopg2.OperationalError) as e:
116 n0 = registry('ir.sequence').next_by_code(cr0, ADMIN_USER_ID, 'test_sequence_type_2', {})
118 n1 = registry('ir.sequence').next_by_code(cr1, ADMIN_USER_ID, 'test_sequence_type_2', {})
119 self.assertEqual(e.exception.pgcode, psycopg2.errorcodes.LOCK_NOT_AVAILABLE, msg="postgresql returned an incorrect errcode")
124 def tearDownClass(cls):
125 drop_sequence('test_sequence_type_2')
127 class test_ir_sequence_change_implementation(unittest2.TestCase):
128 """ Create sequence objects and change their ``implementation`` field. """
130 def test_ir_sequence_1_create(self):
131 """ Try to create a sequence object. """
133 d = dict(code='test_sequence_type_3', name='Test sequence type')
134 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
136 d = dict(code='test_sequence_type_3', name='Test sequence')
137 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
139 d = dict(code='test_sequence_type_4', name='Test sequence type')
140 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
142 d = dict(code='test_sequence_type_4', name='Test sequence',
143 implementation='no_gap')
144 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
149 def test_ir_sequence_2_write(self):
151 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
152 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
153 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
154 {'implementation': 'standard'}, {})
155 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
156 {'implementation': 'no_gap'}, {})
160 def test_ir_sequence_3_unlink(self):
162 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
163 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
164 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})
169 def tearDownClass(cls):
170 drop_sequence('test_sequence_type_3')
171 drop_sequence('test_sequence_type_4')
173 class test_ir_sequence_generate(unittest2.TestCase):
174 """ Create sequence objects and generate some values. """
176 def test_ir_sequence_create(self):
177 """ Try to create a sequence object. """
179 d = dict(code='test_sequence_type_5', name='Test sequence type')
180 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
182 d = dict(code='test_sequence_type_5', name='Test sequence')
183 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
189 f = lambda *a: registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})
190 assert all(str(x) == f() for x in xrange(1,10))
194 def test_ir_sequence_create_no_gap(self):
195 """ Try to create a sequence object. """
197 d = dict(code='test_sequence_type_6', name='Test sequence type')
198 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
200 d = dict(code='test_sequence_type_6', name='Test sequence')
201 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
207 f = lambda *a: registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})
208 assert all(str(x) == f() for x in xrange(1,10))
213 def tearDownClass(cls):
214 drop_sequence('test_sequence_type_5')
215 drop_sequence('test_sequence_type_6')
218 if __name__ == '__main__':
222 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: