From 6acc5a157dc287adde7d31978e2ecc163a3ef9ef Mon Sep 17 00:00:00 2001
From: Vo Minh Thu <vmt@openerp.com>
Date: Tue, 15 Nov 2011 17:38:43 +0100
Subject: [PATCH] [IMP] Moved the tests directory inside the openerp module,
 added --run-tests command-line argument.

bzr revid: vmt@openerp.com-20111115163843-djq6hybp24lk9f5e
---
 openerp-server                                | 14 +++++++++++
 openerp/addons/base/test/test_ir_cron.py      | 12 +++++-----
 openerp/tests/__init__.py                     | 17 +++++++++++++
 .../tests}/addons/test_exceptions/__init__.py |  0
 .../addons/test_exceptions/__openerp__.py     |  0
 .../tests}/addons/test_exceptions/models.py   |  0
 .../tests}/addons/test_exceptions/view.xml    |  0
 {tests => openerp/tests}/common.py            | 24 ++++++++++---------
 {tests => openerp/tests}/test_ir_sequence.py  |  4 +++-
 {tests => openerp/tests}/test_orm.py          |  8 +++----
 {tests => openerp/tests}/test_xmlrpc.py       |  5 +++-
 openerp/tools/config.py                       |  5 ++++
 tests/__init__.py                             |  2 --
 13 files changed, 66 insertions(+), 25 deletions(-)
 create mode 100644 openerp/tests/__init__.py
 rename {tests => openerp/tests}/addons/test_exceptions/__init__.py (100%)
 rename {tests => openerp/tests}/addons/test_exceptions/__openerp__.py (100%)
 rename {tests => openerp/tests}/addons/test_exceptions/models.py (100%)
 rename {tests => openerp/tests}/addons/test_exceptions/view.xml (100%)
 rename {tests => openerp/tests}/common.py (84%)
 rename {tests => openerp/tests}/test_ir_sequence.py (99%)
 rename {tests => openerp/tests}/test_orm.py (97%)
 rename {tests => openerp/tests}/test_xmlrpc.py (96%)
 delete mode 100644 tests/__init__.py

diff --git a/openerp-server b/openerp-server
index 71a52d5f06b6..c108d9f93726 100755
--- a/openerp-server
+++ b/openerp-server
@@ -221,6 +221,20 @@ if __name__ == "__main__":
 
     setup_signal_handlers()
 
+    if config["run_tests_no_db"]:
+        import unittest2
+        import openerp.tests
+        # This test suite creates a database.
+        unittest2.TextTestRunner(verbosity=2).run(openerp.tests.make_suite_no_db())
+        sys.exit(0)
+
+    if config["run_tests"]:
+        import unittest2
+        import openerp.tests
+        # This test suite assumes a database.
+        unittest2.TextTestRunner(verbosity=2).run(openerp.tests.make_suite())
+        sys.exit(0)
+
     if config["test_file"]:
         run_test_file(config['db_name'], config['test_file'])
         sys.exit(0)
diff --git a/openerp/addons/base/test/test_ir_cron.py b/openerp/addons/base/test/test_ir_cron.py
index 6ef79ac3a784..2154c014f8cd 100644
--- a/openerp/addons/base/test/test_ir_cron.py
+++ b/openerp/addons/base/test/test_ir_cron.py
@@ -40,7 +40,7 @@ JOB = {
     'model': u'ir.cron'
 }
 
-class test_ir_cron(openerp.osv.osv.osv):
+class x_test_ir_cron(openerp.osv.osv.osv):
     """ Add a few handy methods to test cron jobs scheduling. """
     _inherit = "ir.cron"
 
@@ -57,7 +57,7 @@ class test_ir_cron(openerp.osv.osv.osv):
         time.sleep(80)
         print ">>> out _80_seconds"
 
-    def test_0(self, cr, uid):
+    def x_test_0(self, cr, uid):
         now = datetime.now()
         t1 = (now + relativedelta(minutes=1)).strftime('%Y-%m-%d %H:%M:%S')
         t2 = (now + relativedelta(minutes=1, seconds=5)).strftime('%Y-%m-%d %H:%M:%S')
@@ -66,17 +66,17 @@ class test_ir_cron(openerp.osv.osv.osv):
         self.create(cr, uid, dict(JOB, name='test_0 _20_seconds B', function='_20_seconds', nextcall=t2))
         self.create(cr, uid, dict(JOB, name='test_0 _20_seconds C', function='_20_seconds', nextcall=t3))
 
-    def test_1(self, cr, uid):
+    def x_test_1(self, cr, uid):
         now = datetime.now()
         t1 = (now + relativedelta(minutes=1)).strftime('%Y-%m-%d %H:%M:%S')
         self.create(cr, uid, dict(JOB, name='test_1 _20_seconds * 3', function='_20_seconds', nextcall=t1, numbercall=3))
 
-    def test_2(self, cr, uid):
+    def x_test_2(self, cr, uid):
         now = datetime.now()
         t1 = (now + relativedelta(minutes=1)).strftime('%Y-%m-%d %H:%M:%S')
         self.create(cr, uid, dict(JOB, name='test_2 _80_seconds * 2', function='_80_seconds', nextcall=t1, numbercall=2))
 
-    def test_3(self, cr, uid):
+    def x_test_3(self, cr, uid):
         now = datetime.now()
         t1 = (now + relativedelta(minutes=1)).strftime('%Y-%m-%d %H:%M:%S')
         t2 = (now + relativedelta(minutes=1, seconds=5)).strftime('%Y-%m-%d %H:%M:%S')
@@ -86,7 +86,7 @@ class test_ir_cron(openerp.osv.osv.osv):
         self.create(cr, uid, dict(JOB, name='test_3 _20_seconds C', function='_20_seconds', nextcall=t3))
 
     # This test assumes 4 cron threads.
-    def test_00(self, cr, uid):
+    def x_test_00(self, cr, uid):
         self.test_00_set = set()
         now = datetime.now()
         t1 = (now + relativedelta(minutes=1)).strftime('%Y-%m-%d %H:%M:%S')
diff --git a/openerp/tests/__init__.py b/openerp/tests/__init__.py
new file mode 100644
index 000000000000..306edcd68ce0
--- /dev/null
+++ b/openerp/tests/__init__.py
@@ -0,0 +1,17 @@
+# -*- coding: utf-8 -*-
+import unittest2
+
+import test_orm
+import test_ir_sequence
+import test_xmlrpc
+
+def make_suite():
+    suite = unittest2.TestSuite()
+    suite.addTests(unittest2.TestLoader().loadTestsFromModule(test_ir_sequence))
+    suite.addTests(unittest2.TestLoader().loadTestsFromModule(test_orm))
+    return suite
+
+def make_suite_no_db():
+    suite = unittest2.TestSuite()
+    suite.addTests(unittest2.TestLoader().loadTestsFromModule(test_xmlrpc))
+    return suite
diff --git a/tests/addons/test_exceptions/__init__.py b/openerp/tests/addons/test_exceptions/__init__.py
similarity index 100%
rename from tests/addons/test_exceptions/__init__.py
rename to openerp/tests/addons/test_exceptions/__init__.py
diff --git a/tests/addons/test_exceptions/__openerp__.py b/openerp/tests/addons/test_exceptions/__openerp__.py
similarity index 100%
rename from tests/addons/test_exceptions/__openerp__.py
rename to openerp/tests/addons/test_exceptions/__openerp__.py
diff --git a/tests/addons/test_exceptions/models.py b/openerp/tests/addons/test_exceptions/models.py
similarity index 100%
rename from tests/addons/test_exceptions/models.py
rename to openerp/tests/addons/test_exceptions/models.py
diff --git a/tests/addons/test_exceptions/view.xml b/openerp/tests/addons/test_exceptions/view.xml
similarity index 100%
rename from tests/addons/test_exceptions/view.xml
rename to openerp/tests/addons/test_exceptions/view.xml
diff --git a/tests/common.py b/openerp/tests/common.py
similarity index 84%
rename from tests/common.py
rename to openerp/tests/common.py
index 116898dc7efd..618eafb21f78 100644
--- a/tests/common.py
+++ b/openerp/tests/common.py
@@ -6,9 +6,10 @@ import xmlrpclib
 
 import openerp
 
-ADDONS_PATH = os.environ['OPENERP_ADDONS_PATH']
-PORT = int(os.environ['OPENERP_PORT'])
-DB = os.environ['OPENERP_DATABASE']
+# The openerp library is supposed already configured.
+ADDONS_PATH = openerp.tools.config['addons_path']
+PORT = openerp.tools.config['xmlrpc_port']
+DB = openerp.tools.config['db_name']
 
 HOST = '127.0.0.1'
 
@@ -25,15 +26,19 @@ db_proxy_61 = None
 model_proxy_61 = None
 model_uri_61 = None
 
-def setUpModule():
+def start_openerp():
     """
-    Start the OpenERP server similary to the openerp-server script and
-    setup some xmlrpclib proxies.
+    Start the OpenERP server similary to the openerp-server script.
     """
-    openerp.tools.config['addons_path'] = ADDONS_PATH
-    openerp.tools.config['xmlrpc_port'] = PORT
     openerp.service.start_services()
 
+    # Ugly way to ensure the server is listening.
+    time.sleep(2)
+
+def create_xmlrpc_proxies():
+    """
+    setup some xmlrpclib proxies.
+    """
     global common_proxy_60
     global db_proxy_60
     global object_proxy_60
@@ -55,9 +60,6 @@ def setUpModule():
     db_proxy_61 = xmlrpclib.ServerProxy(model_uri_61 + 'db')
     model_proxy_61 = xmlrpclib.ServerProxy(model_uri_61 + 'model/' + DB)
 
-    # 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/openerp/tests/test_ir_sequence.py
similarity index 99%
rename from tests/test_ir_sequence.py
rename to openerp/tests/test_ir_sequence.py
index 8a4731d524f5..3bf84cf34045 100644
--- a/tests/test_ir_sequence.py
+++ b/openerp/tests/test_ir_sequence.py
@@ -19,7 +19,9 @@ import common
 DB = common.DB
 ADMIN_USER_ID = common.ADMIN_USER_ID
 
-setUpModule = common.setUpModule
+def setUpModule():
+    common.create_xmlrpc_proxies()
+
 tearDownModule = common.tearDownModule
 
 def registry(model):
diff --git a/tests/test_orm.py b/openerp/tests/test_orm.py
similarity index 97%
rename from tests/test_orm.py
rename to openerp/tests/test_orm.py
index 61b574df5ebb..79ac421b7acb 100644
--- a/tests/test_orm.py
+++ b/openerp/tests/test_orm.py
@@ -1,9 +1,10 @@
 import os
 import unittest2
+
 import openerp
 
 UID = 1
-DB = os.environ['OPENERP_DATABASE']
+DB = openerp.tools.config['db_name']
 
 CREATE = lambda values: (0, False, values)
 UPDATE = lambda id, values: (1, id, values)
@@ -13,14 +14,13 @@ LINK_TO = lambda id: (4, id, False)
 DELETE_ALL = lambda: (5, False, False)
 REPLACE_WITH = lambda ids: (6, False, ids)
 
-def setUpModule():
-    openerp.tools.config['addons_path'] = os.environ['OPENERP_ADDONS_PATH']
-
 class TestO2MSerialization(unittest2.TestCase):
+
     def setUp(self):
         self.cr = openerp.modules.registry.RegistryManager.get(DB).db.cursor()
         self.partner = openerp.modules.registry.RegistryManager.get(DB)['res.partner']
         self.address = openerp.modules.registry.RegistryManager.get(DB)['res.partner.address']
+
     def tearDown(self):
         self.cr.rollback()
         self.cr.close()
diff --git a/tests/test_xmlrpc.py b/openerp/tests/test_xmlrpc.py
similarity index 96%
rename from tests/test_xmlrpc.py
rename to openerp/tests/test_xmlrpc.py
index 4ad530cbeda0..a08987cdd857 100644
--- a/tests/test_xmlrpc.py
+++ b/openerp/tests/test_xmlrpc.py
@@ -19,7 +19,10 @@ ADMIN_USER = common.ADMIN_USER
 ADMIN_USER_ID = common.ADMIN_USER_ID
 ADMIN_PASSWORD = common.ADMIN_PASSWORD
 
-setUpModule = common.setUpModule
+def setUpModule():
+  common.start_openerp()
+  common.create_xmlrpc_proxies()
+
 tearDownModule = common.tearDownModule
 
 class test_xmlrpc(unittest2.TestCase):
diff --git a/openerp/tools/config.py b/openerp/tools/config.py
index f1c3931e81f8..2b27844ceed3 100644
--- a/openerp/tools/config.py
+++ b/openerp/tools/config.py
@@ -266,6 +266,10 @@ class configmanager(object):
                          type="int")
         group.add_option("--unaccent", dest="unaccent", my_default=False, action="store_true",
                          help="Use the unaccent function provided by the database when available.")
+        group.add_option("--run-tests", dest="run_tests", my_default=False, action="store_true",
+                         help="Run a test suite.")
+        group.add_option("--run-tests-no-db", dest="run_tests_no_db", my_default=False, action="store_true",
+                         help="Run a test suite (which does'nt assume an existing database).")
 
         parser.add_option_group(group)
 
@@ -370,6 +374,7 @@ class configmanager(object):
             'list_db', 'xmlrpcs',
             'test_file', 'test_disable', 'test_commit', 'test_report_directory',
             'osv_memory_count_limit', 'osv_memory_age_limit', 'max_cron_threads', 'unaccent',
+            'run_tests', 'run_tests_no_db',
         ]
 
         for arg in keys:
diff --git a/tests/__init__.py b/tests/__init__.py
deleted file mode 100644
index 396284efaf7f..000000000000
--- a/tests/__init__.py
+++ /dev/null
@@ -1,2 +0,0 @@
-# -*- coding: utf-8 -*-
-import test_xmlrpc
-- 
GitLab