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 return openerp.modules.registry.RegistryManager.get(DB)[model]
23 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
26 def drop_sequence(code):
28 for model in ['ir.sequence', 'ir.sequence.type']:
30 ids = s.search(cr, ADMIN_USER_ID, [('code', '=', code)])
31 s.unlink(cr, ADMIN_USER_ID, ids)
35 class test_ir_sequence_standard(unittest2.TestCase):
36 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """
38 def test_ir_sequence_create(self):
39 """ Try to create a sequence object. """
41 d = dict(code='test_sequence_type', name='Test sequence type')
42 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
44 d = dict(code='test_sequence_type', name='Test sequence')
45 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
50 def test_ir_sequence_search(self):
53 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})
58 def test_ir_sequence_draw(self):
59 """ Try to draw a number. """
61 n = registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type', {})
66 def test_ir_sequence_draw_twice(self):
67 """ Try to draw a number from two transactions. """
70 n0 = registry('ir.sequence').next_by_code(cr0, ADMIN_USER_ID, 'test_sequence_type', {})
72 n1 = registry('ir.sequence').next_by_code(cr1, ADMIN_USER_ID, 'test_sequence_type', {})
80 def tearDownClass(cls):
81 drop_sequence('test_sequence_type')
83 class test_ir_sequence_no_gap(unittest2.TestCase):
84 """ Copy of the previous tests for a 'No gap' sequence. """
86 def test_ir_sequence_create_no_gap(self):
87 """ Try to create a sequence object. """
89 d = dict(code='test_sequence_type_2', name='Test sequence type')
90 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
92 d = dict(code='test_sequence_type_2', name='Test sequence',
93 implementation='no_gap')
94 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
99 def test_ir_sequence_draw_no_gap(self):
100 """ Try to draw a number. """
102 n = registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})
107 def test_ir_sequence_draw_twice_no_gap(self):
108 """ Try to draw a number from two transactions.
109 This is expected to not work.
113 cr1._default_log_exceptions = False # Prevent logging a traceback
114 msg_re = '^could not obtain lock on row in relation "ir_sequence"$'
115 with self.assertRaisesRegexp(psycopg2.OperationalError, msg_re):
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', {})
123 def tearDownClass(cls):
124 drop_sequence('test_sequence_type_2')
126 class test_ir_sequence_change_implementation(unittest2.TestCase):
127 """ Create sequence objects and change their ``implementation`` field. """
129 def test_ir_sequence_1_create(self):
130 """ Try to create a sequence object. """
132 d = dict(code='test_sequence_type_3', name='Test sequence type')
133 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
135 d = dict(code='test_sequence_type_3', name='Test sequence')
136 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
138 d = dict(code='test_sequence_type_4', name='Test sequence type')
139 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
141 d = dict(code='test_sequence_type_4', name='Test sequence',
142 implementation='no_gap')
143 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
148 def test_ir_sequence_2_write(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').write(cr, ADMIN_USER_ID, ids,
153 {'implementation': 'standard'}, {})
154 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
155 {'implementation': 'no_gap'}, {})
159 def test_ir_sequence_3_unlink(self):
161 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
162 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
163 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})
168 def tearDownClass(cls):
169 drop_sequence('test_sequence_type_3')
170 drop_sequence('test_sequence_type_4')
172 class test_ir_sequence_generate(unittest2.TestCase):
173 """ Create sequence objects and generate some values. """
175 def test_ir_sequence_create(self):
176 """ Try to create a sequence object. """
178 d = dict(code='test_sequence_type_5', name='Test sequence type')
179 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
181 d = dict(code='test_sequence_type_5', name='Test sequence')
182 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
188 f = lambda *a: registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})
189 assert all(str(x) == f() for x in xrange(1,10))
193 def test_ir_sequence_create_no_gap(self):
194 """ Try to create a sequence object. """
196 d = dict(code='test_sequence_type_6', name='Test sequence type')
197 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
199 d = dict(code='test_sequence_type_6', name='Test sequence')
200 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
206 f = lambda *a: registry('ir.sequence').next_by_code(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})
207 assert all(str(x) == f() for x in xrange(1,10))
212 def tearDownClass(cls):
213 drop_sequence('test_sequence_type_5')
214 drop_sequence('test_sequence_type_6')
217 if __name__ == '__main__':
221 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: