1 # -*- coding: utf-8 -*-
3 The module :mod:`openerp.tests.common` provides a few helpers and classes to write
19 _logger = logging.getLogger(__name__)
21 # The openerp library is supposed already configured.
22 ADDONS_PATH = openerp.tools.config['addons_path']
24 PORT = openerp.tools.config['xmlrpc_port']
25 DB = openerp.tools.config['db_name']
27 # If the database name is not provided on the command-line,
28 # use the one on the thread (which means if it is provided on
29 # the command-line, this will break when installing another
30 # database from XML-RPC).
31 if not DB and hasattr(threading.current_thread(), 'dbname'):
32 DB = threading.current_thread().dbname
35 ADMIN_USER_ID = openerp.SUPERUSER_ID
36 ADMIN_PASSWORD = 'admin'
40 class BaseCase(unittest2.TestCase):
42 Subclass of TestCase for common OpenERP-specific code.
44 This class is abstract and expects self.cr and self.uid to be initialized by subclasses.
49 return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
52 def registry(self, model):
53 return openerp.modules.registry.RegistryManager.get(DB)[model]
57 """ Returns database ID corresponding to a given identifier.
59 :param xid: fully-qualified record identifier, in the form ``module.identifier``
60 :raise: ValueError if not found
62 assert "." in xid, "this method requires a fully qualified parameter, in the following form: 'module.identifier'"
63 module, xid = xid.split('.')
64 _, id = self.registry('ir.model.data').get_object_reference(self.cr, self.uid, module, xid)
68 def browse_ref(self, xid):
69 """ Returns a browsable record for the given identifier.
71 :param xid: fully-qualified record identifier, in the form ``module.identifier``
72 :raise: ValueError if not found
74 assert "." in xid, "this method requires a fully qualified parameter, in the following form: 'module.identifier'"
75 module, xid = xid.split('.')
76 return self.registry('ir.model.data').get_object(self.cr, self.uid, module, xid)
79 class TransactionCase(BaseCase):
81 Subclass of BaseCase with a single transaction, rolled-back at the end of
86 # Store cr and uid in class variables, to allow ref() and browse_ref to be BaseCase @classmethods
87 # and still access them
88 TransactionCase.cr = self.cursor()
89 TransactionCase.uid = openerp.SUPERUSER_ID
96 class SingleTransactionCase(BaseCase):
98 Subclass of BaseCase with a single transaction for the whole class,
99 rolled-back after all the tests.
104 cls.cr = cls.cursor()
105 cls.uid = openerp.SUPERUSER_ID
108 def tearDownClass(cls):
113 class RpcCase(unittest2.TestCase):
115 Subclass of TestCase with a few XML-RPC proxies.
118 def __init__(self, methodName='runTest'):
119 super(RpcCase, self).__init__(methodName)
125 # Use the old (pre 6.1) API.
126 self.proxy.url_60 = url_60 = 'http://%s:%d/xmlrpc/' % (HOST, PORT)
127 self.proxy.common_60 = xmlrpclib.ServerProxy(url_60 + 'common')
128 self.proxy.db_60 = xmlrpclib.ServerProxy(url_60 + 'db')
129 self.proxy.object_60 = xmlrpclib.ServerProxy(url_60 + 'object')
130 #self.proxy.edi_60 = xmlrpclib.ServerProxy(url_60 + 'edi')
132 # Use the new (8) API.
133 self.proxy.url_8 = url_8 = 'http://%s:%d/xmlrpc/2/' % (HOST, PORT)
134 self.proxy.common_8 = xmlrpclib.ServerProxy(url_8 + 'common')
135 self.proxy.db_8 = xmlrpclib.ServerProxy(url_8 + 'db')
136 self.proxy.object_8 = xmlrpclib.ServerProxy(url_8 + 'object')
139 def generate_database_name(cls):
140 if hasattr(cls, '_database_id'):
141 cls._database_id += 1
144 return '_fresh_name_' + str(cls._database_id) + '_'
146 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: