move tests
[odoo/odoo.git] / openerp / tests / common.py
1 # -*- coding: utf-8 -*-
2 """
3 The module :mod:`openerp.tests.common` provides a few helpers and classes to write
4 tests.
5 """
6 import json
7 import os
8 import select
9 import subprocess
10 import threading
11 import time
12 import unittest2
13 import uuid
14 import xmlrpclib
15 import logging
16
17 import openerp
18
19 _logger = logging.getLogger(__name__)
20
21 # The openerp library is supposed already configured.
22 ADDONS_PATH = openerp.tools.config['addons_path']
23 HOST = '127.0.0.1'
24 PORT = openerp.tools.config['xmlrpc_port']
25 DB = openerp.tools.config['db_name']
26
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
33
34 ADMIN_USER = 'admin'
35 ADMIN_USER_ID = openerp.SUPERUSER_ID
36 ADMIN_PASSWORD = 'admin'
37
38 HTTP_SESSION = {}
39
40 class BaseCase(unittest2.TestCase):
41     """
42     Subclass of TestCase for common OpenERP-specific code.
43     
44     This class is abstract and expects self.cr and self.uid to be initialized by subclasses.
45     """
46
47     @classmethod
48     def cursor(self):
49         return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
50
51     @classmethod
52     def registry(self, model):
53         return openerp.modules.registry.RegistryManager.get(DB)[model]
54
55     @classmethod
56     def ref(self, xid):
57         """ Returns database ID corresponding to a given identifier.
58
59             :param xid: fully-qualified record identifier, in the form ``module.identifier``
60             :raise: ValueError if not found
61         """
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)
65         return id
66
67     @classmethod
68     def browse_ref(self, xid):
69         """ Returns a browsable record for the given identifier.
70
71             :param xid: fully-qualified record identifier, in the form ``module.identifier``
72             :raise: ValueError if not found
73         """
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)
77
78
79 class TransactionCase(BaseCase):
80     """
81     Subclass of BaseCase with a single transaction, rolled-back at the end of
82     each test (method).
83     """
84
85     def setUp(self):
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
90
91     def tearDown(self):
92         self.cr.rollback()
93         self.cr.close()
94
95
96 class SingleTransactionCase(BaseCase):
97     """
98     Subclass of BaseCase with a single transaction for the whole class,
99     rolled-back after all the tests.
100     """
101
102     @classmethod
103     def setUpClass(cls):
104         cls.cr = cls.cursor()
105         cls.uid = openerp.SUPERUSER_ID
106
107     @classmethod
108     def tearDownClass(cls):
109         cls.cr.rollback()
110         cls.cr.close()
111
112
113 class RpcCase(unittest2.TestCase):
114     """
115     Subclass of TestCase with a few XML-RPC proxies.
116     """
117
118     def __init__(self, methodName='runTest'):
119         super(RpcCase, self).__init__(methodName)
120
121         class A(object):
122             pass
123         self.proxy = A()
124
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')
131
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')
137
138     @classmethod
139     def generate_database_name(cls):
140         if hasattr(cls, '_database_id'):
141             cls._database_id += 1
142         else:
143             cls._database_id = 0
144         return '_fresh_name_' + str(cls._database_id) + '_'
145
146 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: