diff --git a/openerp/addons/base/ir/ir_sequence.py b/openerp/addons/base/ir/ir_sequence.py index 3ff5009b567..086d48ac44d 100644 --- a/openerp/addons/base/ir/ir_sequence.py +++ b/openerp/addons/base/ir/ir_sequence.py @@ -54,7 +54,7 @@ class ir_sequence(openerp.osv.osv.osv): _order = 'name' _columns = { 'name': openerp.osv.fields.char('Name', size=64, required=True), - 'code': openerp.osv.fields.selection(_code_get, 'Code', size=64, required=True), # TODO should it be unique? + 'code': openerp.osv.fields.selection(_code_get, 'Code', size=64, required=True), 'implementation': openerp.osv.fields.selection( # TODO update the view IMPLEMENTATION_SELECTION, 'Implementation', required=True, help="Two sequence object implementations are offered: Standard " @@ -79,8 +79,8 @@ class ir_sequence(openerp.osv.osv.osv): def create(self, cr, uid, values, context=None): values = self._add_missing_default_values(cr, uid, values, context) - go = super(ir_sequence, self).create_postgres \ - if values['implementation'] == 'standard' else self.create + go = super(ir_sequence, self).create \ + if values['implementation'] == 'no_gap' else self.create_postgres return go(cr, uid, values, context) def create_postgres(self, cr, uid, values, context=None): @@ -92,7 +92,7 @@ class ir_sequence(openerp.osv.osv.osv): :return: id of the newly created record """ id = super(ir_sequence, self).create(cr, uid, values, context) - self._create_sequence(cr, + self._create_sequence(cr, id, values['number_increment'], values['number_next']) return id @@ -161,7 +161,7 @@ class ir_sequence(openerp.osv.osv.osv): if res['implementation'] == 'standard': cr.execute(""" SELECT nextval('ir_sequence_%03d') - """, (res['id'],)) + """ % res['id']) res['number_next'] = cr.fetchone() else: cr.execute(""" @@ -207,14 +207,15 @@ class ir_sequence(openerp.osv.osv.osv): """ % code_or_id, (sequence_code_or_id, tuple(company_ids))) return cr.dictfetchone() - def _create_sequence(self, cr, number_increment, number_next): + def _create_sequence(self, cr, id, number_increment, number_next): """ Create a PostreSQL sequence. There is no access rights check. """ + assert isinstance(id, (int, long)) cr.execute(""" - CREATE SEQUENCE ir_sequence_%03d INCREMENT BY %s START WITH %s - """, (id, number_increment, number_next)) + CREATE SEQUENCE ir_sequence_%03d INCREMENT BY %%s START WITH %%s + """ % id, (number_increment, number_next)) def _drop_sequence(self, cr, ids): """ Drop the PostreSQL sequence if it exists. @@ -238,9 +239,10 @@ class ir_sequence(openerp.osv.osv.osv): There is no access rights check. """ + assert isinstance(id, (int, long)) cr.execute(""" - ALTER SEQUENCE ir_sequence_%03d INCREMENT BY %s RESTART WITH %s - """, (id, number_increment, number_next)) + ALTER SEQUENCE ir_sequence_%03d INCREMENT BY %%s RESTART WITH %%s + """ % id, (number_increment, number_next)) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/tests/common.py b/tests/common.py new file mode 100644 index 00000000000..eb34772d1af --- /dev/null +++ b/tests/common.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +import os +import time +import unittest2 +import xmlrpclib + +import openerp + +ADDONS_PATH = os.environ['OPENERP_ADDONS_PATH'] +PORT = int(os.environ['OPENERP_PORT']) +DB = os.environ['OPENERP_DATABASE'] + +HOST = '127.0.0.1' + +ADMIN_USER = 'admin' +ADMIN_USER_ID = 1 +ADMIN_PASSWORD = 'admin' + +common_proxy_60 = None +db_proxy_60 = None +object_proxy_60 = None + +def setUpModule(): + """ + Start the OpenERP server similary to the openerp-server script and + setup some xmlrpclib proxies. + """ + openerp.tools.config['addons_path'] = ADDONS_PATH + openerp.tools.config['xmlrpc_port'] = PORT + openerp.service.start_services() + + global common_proxy_60 + global db_proxy_60 + global object_proxy_60 + + # Use the old (pre 6.1) API. + url = 'http://%s:%d/xmlrpc/' % (HOST, PORT) + common_proxy_60 = xmlrpclib.ServerProxy(url + 'common') + db_proxy_60 = xmlrpclib.ServerProxy(url + 'db') + object_proxy_60 = xmlrpclib.ServerProxy(url + 'object') + + # Ugly way to ensure the server is listening. + time.sleep(2) + +def tearDownModule(): + """ Shutdown the OpenERP server similarly to a single ctrl-c. """ + openerp.service.stop_services() diff --git a/tests/test_ir_sequence.py b/tests/test_ir_sequence.py new file mode 100644 index 00000000000..ef7e6784daa --- /dev/null +++ b/tests/test_ir_sequence.py @@ -0,0 +1,57 @@ +# -*- coding: utf-8 -*- +# Run with one of these commands: +# > OPENERP_ADDONS_PATH='../../addons/trunk' OPENERP_PORT=8069 \ +# OPENERP_DATABASE=yy PYTHONPATH=. python tests/test_ir_sequence.py +# > OPENERP_ADDONS_PATH='../../addons/trunk' OPENERP_PORT=8069 \ +# OPENERP_DATABASE=yy nosetests tests/test_ir_sequence.py +# > OPENERP_ADDONS_PATH='../../../addons/trunk' OPENERP_PORT=8069 \ +# OPENERP_DATABASE=yy PYTHONPATH=../:. unit2 test_ir_sequence +import os +import time +import unittest2 +import xmlrpclib + +import openerp +import common + +DB = common.DB +ADMIN_USER_ID = common.ADMIN_USER_ID + +setUpModule = common.setUpModule +tearDownModule = common.tearDownModule + +def registry(model): + return openerp.modules.registry.RegistryManager.get(DB)[model] + +def cursor(): + return openerp.modules.registry.RegistryManager.get(DB).db.cursor() + +class test_ir_sequence(unittest2.TestCase): + + def test_ir_sequence_create(self): + """ Try to create a sequence object. """ + cr = cursor() + try: + d = dict(code='test_sequence_type', name='Test sequence type') + c = registry('ir.sequence.type').create(cr, ADMIN_USER_ID, d, {}) + assert c + d = dict(code='test_sequence_type', name='Test sequence') + c = registry('ir.sequence').create(cr, ADMIN_USER_ID, d, {}) + assert c + cr.commit() + finally: + cr.close() + + def test_ir_sequence_search(self): + """ Try a search. """ + cr = cursor() + try: + ids = registry('ir.sequence').search(cr, ADMIN_USER_ID, [], {}) + assert ids + cr.commit() + finally: + cr.close() + +if __name__ == '__main__': + unittest2.main() + diff --git a/tests/test_xmlrpc.py b/tests/test_xmlrpc.py index 30af863f12d..35ba07b940a 100644 --- a/tests/test_xmlrpc.py +++ b/tests/test_xmlrpc.py @@ -12,43 +12,15 @@ import unittest2 import xmlrpclib import openerp +import common -ADDONS_PATH = os.environ['OPENERP_ADDONS_PATH'] -PORT = int(os.environ['OPENERP_PORT']) -DB = os.environ['OPENERP_DATABASE'] +DB = common.DB +ADMIN_USER = common.ADMIN_USER +ADMIN_USER_ID = common.ADMIN_USER_ID +ADMIN_PASSWORD = common.ADMIN_PASSWORD -HOST = '127.0.0.1' - -ADMIN_USER = 'admin' -ADMIN_USER_ID = 1 -ADMIN_PASSWORD = 'admin' - -common_proxy_60 = None -db_proxy_60 = None -object_proxy_60 = None - -def setUpModule(): - """ - Start the OpenERP server similary to the openerp-server script and - setup some xmlrpclib proxies. - """ - openerp.tools.config['addons_path'] = ADDONS_PATH - openerp.tools.config['xmlrpc_port'] = PORT - openerp.service.start_services() - - global common_proxy_60 - global db_proxy_60 - global object_proxy_60 - - # Use the old (pre 6.1) API. - url = 'http://%s:%d/xmlrpc/' % (HOST, PORT) - common_proxy_60 = xmlrpclib.ServerProxy(url + 'common') - db_proxy_60 = xmlrpclib.ServerProxy(url + 'db') - object_proxy_60 = xmlrpclib.ServerProxy(url + 'object') - -def tearDownModule(): - """ Shutdown the OpenERP server similarly to a single ctrl-c. """ - openerp.service.stop_services() +setUpModule = common.setUpModule +tearDownModule = common.tearDownModule class test_xmlrpc(unittest2.TestCase): @@ -57,26 +29,26 @@ class test_xmlrpc(unittest2.TestCase): Simulate a OpenERP client requesting the creation of a database and polling the server until the creation is complete. """ - progress_id = db_proxy_60.create(ADMIN_PASSWORD, DB, True, False, - ADMIN_PASSWORD) + progress_id = common.db_proxy_60.create(ADMIN_PASSWORD, DB, True, + False, ADMIN_PASSWORD) while True: time.sleep(1) - progress, users = db_proxy_60.get_progress(ADMIN_PASSWORD, + progress, users = common.db_proxy_60.get_progress(ADMIN_PASSWORD, progress_id) if progress == 1.0: break def test_xmlrpc_login(self): """ Try to login on the common service. """ - uid = common_proxy_60.login(DB, ADMIN_USER, ADMIN_PASSWORD) + uid = common.common_proxy_60.login(DB, ADMIN_USER, ADMIN_PASSWORD) assert uid == ADMIN_USER_ID def test_xmlrpc_ir_model_search(self): """ Try a search on the object service. """ - ids = object_proxy_60.execute(DB, ADMIN_USER_ID, ADMIN_PASSWORD, + ids = common.object_proxy_60.execute(DB, ADMIN_USER_ID, ADMIN_PASSWORD, 'ir.model', 'search', []) assert ids - ids = object_proxy_60.execute(DB, ADMIN_USER_ID, ADMIN_PASSWORD, + ids = common.object_proxy_60.execute(DB, ADMIN_USER_ID, ADMIN_PASSWORD, 'ir.model', 'search', [], {}) assert ids