Random generation of default avatar
[odoo/odoo.git] / openerp / tests / common.py
1 # -*- coding: utf-8 -*-
2 import os
3 import threading
4 import time
5 import unittest2
6 import xmlrpclib
7
8 import openerp
9
10 # The openerp library is supposed already configured.
11 ADDONS_PATH = openerp.tools.config['addons_path']
12 PORT = openerp.tools.config['xmlrpc_port']
13 DB = openerp.tools.config['db_name']
14
15 # If the database name is not provided on the command-line,
16 # use the one on the thread (which means if it is provided on
17 # the command-line, this will break when installing another
18 # database from XML-RPC).
19 if not DB and hasattr(threading.current_thread(), 'dbname'):
20     DB = threading.current_thread().dbname
21
22 HOST = '127.0.0.1'
23
24 ADMIN_USER = 'admin'
25 ADMIN_USER_ID = 1
26 ADMIN_PASSWORD = 'admin'
27
28 def start_openerp():
29     """
30     Start the OpenERP server similary to the openerp-server script.
31     """
32     openerp.service.start_services()
33
34     # Ugly way to ensure the server is listening.
35     time.sleep(2)
36
37 def stop_openerp():
38     """
39     Shutdown the OpenERP server similarly to a single ctrl-c.
40     """
41     openerp.service.stop_services()
42
43 class TransactionCase(unittest2.TestCase):
44     """
45     Subclass of TestCase with a single transaction, rolled-back at the end of
46     the tests.
47     """
48
49     def setUp(self):
50         self.cr = self.cursor()
51         self.uid = openerp.SUPERUSER_ID
52
53     def tearDown(self):
54         self.cr.rollback()
55         self.cr.close()
56
57     def cursor(self):
58         return openerp.modules.registry.RegistryManager.get(DB).db.cursor()
59
60     def registry(self, model):
61         return openerp.modules.registry.RegistryManager.get(DB)[model]
62
63 class RpcCase(unittest2.TestCase):
64     """
65     Subclass of TestCase with a few XML-RPC proxies.
66     """
67
68     def __init__(self, name):
69         super(RpcCase, self).__init__(name)
70
71         class A(object):
72             pass
73         self.proxy = A()
74
75         # Use the old (pre 6.1) API.
76         self.proxy.url_60 = url_60 = 'http://%s:%d/xmlrpc/' % (HOST, PORT)
77         self.proxy.common_60 = xmlrpclib.ServerProxy(url_60 + 'common')
78         self.proxy.db_60 = xmlrpclib.ServerProxy(url_60 + 'db')
79         self.proxy.object_60 = xmlrpclib.ServerProxy(url_60 + 'object')
80
81         # Use the new (6.1) API.
82         self.proxy.url_61 = url_61 = 'http://%s:%d/openerp/xmlrpc/1/' % (HOST, PORT)
83         self.proxy.common_61 = xmlrpclib.ServerProxy(url_61 + 'common')
84         self.proxy.db_61 = xmlrpclib.ServerProxy(url_61 + 'db')
85         self.proxy.model_61 = xmlrpclib.ServerProxy(url_61 + 'model/' + DB)
86
87     @classmethod
88     def generate_database_name(cls):
89         if hasattr(cls, '_database_id'):
90             cls._database_id += 1
91         else:
92             cls._database_id = 0
93         return '_fresh_name_' + str(cls._database_id) + '_'
94
95 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: