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()
25 class test_ir_sequence_standard(unittest2.TestCase):
26 """ A few tests for a 'Standard' (i.e. PostgreSQL) sequence. """
28 def test_ir_sequence_create(self):
29 """ Try to create a sequence object. """
31 d = dict(code='test_sequence_type', name='Test sequence type')
32 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
34 d = dict(code='test_sequence_type', name='Test sequence')
35 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
40 def test_ir_sequence_search(self):
43 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {})
48 def test_ir_sequence_draw(self):
49 """ Try to draw a number. """
51 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type', {})
56 def test_ir_sequence_draw_twice(self):
57 """ Try to draw a number from two transactions. """
60 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type', {})
62 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type', {})
69 class test_ir_sequence_no_gap(unittest2.TestCase):
70 """ Copy of the previous tests for a 'No gap' sequence. """
72 def test_ir_sequence_create_no_gap(self):
73 """ Try to create a sequence object. """
75 d = dict(code='test_sequence_type_2', name='Test sequence type')
76 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
78 d = dict(code='test_sequence_type_2', name='Test sequence',
79 implementation='no_gap')
80 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
85 def test_ir_sequence_draw_no_gap(self):
86 """ Try to draw a number. """
88 n = registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_2', {})
93 def test_ir_sequence_draw_twice_no_gap(self):
94 """ Try to draw a number from two transactions.
95 This is expected to not work.
99 cr1._default_log_exceptions = False # Prevent logging a traceback
100 msg_re = '^could not obtain lock on row in relation "ir_sequence"$'
101 with self.assertRaisesRegexp(psycopg2.OperationalError, msg_re):
102 n0 = registry('ir.sequence').get(cr0, ADMIN_USER_ID, 'test_sequence_type_2', {})
104 n1 = registry('ir.sequence').get(cr1, ADMIN_USER_ID, 'test_sequence_type_2', {})
108 class test_ir_sequence_change_implementation(unittest2.TestCase):
109 """ Create sequence objects and change their ``implementation`` field. """
111 def test_ir_sequence_1_create(self):
112 """ Try to create a sequence object. """
114 d = dict(code='test_sequence_type_3', name='Test sequence type')
115 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
117 d = dict(code='test_sequence_type_3', name='Test sequence')
118 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
120 d = dict(code='test_sequence_type_4', name='Test sequence type')
121 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
123 d = dict(code='test_sequence_type_4', name='Test sequence',
124 implementation='no_gap')
125 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
130 def test_ir_sequence_2_write(self):
132 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
133 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
134 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
135 {'implementation': 'standard'}, {})
136 registry('ir.sequence').write(cr, ADMIN_USER_ID, ids,
137 {'implementation': 'no_gap'}, {})
141 def test_ir_sequence_3_unlink(self):
143 ids = registry('ir.sequence').search(cr, ADMIN_USER_ID,
144 [('code', 'in', ['test_sequence_type_3', 'test_sequence_type_4'])], {})
145 registry('ir.sequence').unlink(cr, ADMIN_USER_ID, ids, {})
149 class test_ir_sequence_generate(unittest2.TestCase):
150 """ Create sequence objects and generate some values. """
152 def test_ir_sequence_create(self):
153 """ Try to create a sequence object. """
155 d = dict(code='test_sequence_type_5', name='Test sequence type')
156 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
158 d = dict(code='test_sequence_type_5', name='Test sequence')
159 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
165 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_5', {})
166 assert all(str(x) == f() for x in xrange(1,1000))
170 def test_ir_sequence_create_no_gap(self):
171 """ Try to create a sequence object. """
173 d = dict(code='test_sequence_type_6', name='Test sequence type')
174 c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {})
176 d = dict(code='test_sequence_type_6', name='Test sequence')
177 c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {})
183 f = lambda *a: registry('ir.sequence').get(cr, ADMIN_USER_ID, 'test_sequence_type_6', {})
184 assert all(str(x) == f() for x in xrange(1,1000))
190 if __name__ == '__main__':
194 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: