From b49f536baff6fab13884f4b2b6fdee1cb407f128 Mon Sep 17 00:00:00 2001 From: Raphael Collet Date: Tue, 8 Apr 2014 13:49:36 +0200 Subject: [PATCH] [IMP] tests.common: turn class methods into instance methods (this will ease some future refactoring) bzr revid: rco@openerp.com-20140408114936-jfagg4qvft8bk3ms --- openerp/addons/base/tests/test_views.py | 2 +- openerp/modules/registry.py | 4 ++++ openerp/tests/common.py | 22 ++++++++-------------- 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/openerp/addons/base/tests/test_views.py b/openerp/addons/base/tests/test_views.py index 06030a84792..f0967836726 100644 --- a/openerp/addons/base/tests/test_views.py +++ b/openerp/addons/base/tests/test_views.py @@ -28,7 +28,7 @@ class ViewCase(common.TransactionCase): self.assertTreesEqual(c1, c2, msg) -class TestNodeLocator(common.BaseCase): +class TestNodeLocator(common.TransactionCase): """ The node locator returns None when it can not find a node, and the first match when it finds something (no jquery-style node sets) diff --git a/openerp/modules/registry.py b/openerp/modules/registry.py index ff0d56a409c..2db14744915 100644 --- a/openerp/modules/registry.py +++ b/openerp/modules/registry.py @@ -102,6 +102,10 @@ class Registry(Mapping): """ Return the model with the given name or raise KeyError if it doesn't exist.""" return self.models[model_name] + def __call__(self, model_name): + """ Same as ``self[model_name]``. """ + return self.models[model_name] + def do_parent_store(self, cr): for o in self._init_parent: self.get(o)._parent_store_compute(cr) diff --git a/openerp/tests/common.py b/openerp/tests/common.py index a926726c370..6c4864e46d5 100644 --- a/openerp/tests/common.py +++ b/openerp/tests/common.py @@ -84,18 +84,13 @@ class BaseCase(unittest2.TestCase): """ Subclass of TestCase for common OpenERP-specific code. - This class is abstract and expects self.cr and self.uid to be initialized by subclasses. + This class is abstract and expects self.registry, self.cr and self.uid to be + initialized by subclasses. """ - @classmethod def cursor(self): - return openerp.modules.registry.RegistryManager.get(DB).db.cursor() + return self.registry.db.cursor() - @classmethod - def registry(self, model): - return openerp.modules.registry.RegistryManager.get(DB)[model] - - @classmethod def ref(self, xid): """ Returns database ID corresponding to a given identifier. @@ -107,7 +102,6 @@ class BaseCase(unittest2.TestCase): _, id = self.registry('ir.model.data').get_object_reference(self.cr, self.uid, module, xid) return id - @classmethod def browse_ref(self, xid): """ Returns a browsable record for the given identifier. @@ -126,10 +120,9 @@ class TransactionCase(BaseCase): """ def setUp(self): - # Store cr and uid in class variables, to allow ref() and browse_ref to be BaseCase @classmethods - # and still access them - TransactionCase.cr = self.cursor() - TransactionCase.uid = openerp.SUPERUSER_ID + self.registry = openerp.modules.registry.RegistryManager.get(DB) + self.cr = self.cursor() + self.uid = openerp.SUPERUSER_ID def tearDown(self): self.cr.rollback() @@ -144,7 +137,8 @@ class SingleTransactionCase(BaseCase): @classmethod def setUpClass(cls): - cls.cr = cls.cursor() + cls.registry = openerp.modules.registry.RegistryManager.get(DB) + cls.cr = cls.registry.db.cursor() cls.uid = openerp.SUPERUSER_ID @classmethod