diff --git a/openerp/tests/__init__.py b/openerp/tests/__init__.py index 3427743263c..d06e1030d6f 100644 --- a/openerp/tests/__init__.py +++ b/openerp/tests/__init__.py @@ -9,6 +9,7 @@ See the :ref:`test-framework` section in the :ref:`features` list. """ from . import test_expression, test_html_sanitize, test_ir_sequence, test_orm,\ + test_per_class_teardown, \ test_view_validation, test_uninstall, test_misc, test_db_cursor fast_suite = [ @@ -20,6 +21,7 @@ checks = [ test_html_sanitize, test_db_cursor, test_orm, + test_per_class_teardown, test_view_validation, test_misc, ] diff --git a/openerp/tests/common.py b/openerp/tests/common.py index 53c67c68844..b3ee4ac53f2 100644 --- a/openerp/tests/common.py +++ b/openerp/tests/common.py @@ -40,10 +40,25 @@ def stop_openerp(): """ openerp.service.stop_services() -class TransactionCase(unittest2.TestCase): + +class BaseCase(unittest2.TestCase): """ - Subclass of TestCase with a single transaction, rolled-back at the end of - the tests. + Subclass of TestCase for common OpenERP-specific code. + """ + + @classmethod + def cursor(self): + return openerp.modules.registry.RegistryManager.get(DB).db.cursor() + + @classmethod + def registry(self, model): + return openerp.modules.registry.RegistryManager.get(DB)[model] + + +class TransactionCase(BaseCase): + """ + Subclass of BaseCase with a single transaction, rolled-back at the end of + each test (method). """ def setUp(self): @@ -54,11 +69,23 @@ class TransactionCase(unittest2.TestCase): self.cr.rollback() self.cr.close() - def cursor(self): - return openerp.modules.registry.RegistryManager.get(DB).db.cursor() - def registry(self, model): - return openerp.modules.registry.RegistryManager.get(DB)[model] +class SingleTransactionCase(BaseCase): + """ + Subclass of BaseCase with a single transaction for the whole class, + rolled-back after all the tests. + """ + + @classmethod + def setUpClass(cls): + cls.cr = cls.cursor() + cls.uid = openerp.SUPERUSER_ID + + @classmethod + def tearDownClass(cls): + cls.cr.rollback() + cls.cr.close() + class RpcCase(unittest2.TestCase): """ diff --git a/openerp/tests/test_per_class_teardown.py b/openerp/tests/test_per_class_teardown.py new file mode 100644 index 00000000000..a1890095c09 --- /dev/null +++ b/openerp/tests/test_per_class_teardown.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- +import unittest2 + +import openerp +import common + +class test_per_class_teardown(common.SingleTransactionCase): + """ + Check the whole-class transaction behavior of SingleTransactionCase. + """ + + def test_00(self): + """Create a partner.""" + cr, uid = self.cr, self.uid + self.registry('res.partner').create(cr, uid, {'name': 'test_per_class_teardown_partner'}) + ids = self.registry('res.partner').search(cr, uid, [('name', '=', 'test_per_class_teardown_partner')]) + self.assertEqual(1, len(ids), "Test partner not found.") + + def test_01(self): + """Find the created partner.""" + cr, uid = self.cr, self.uid + ids = self.registry('res.partner').search(cr, uid, [('name', '=', 'test_per_class_teardown_partner')]) + self.assertEqual(1, len(ids), "Test partner not found.") + +class test_per_method_teardown(common.TransactionCase): + """ + Check the per-method transaction behavior of TransactionCase. + """ + + def test_00(self): + """Create a partner.""" + cr, uid = self.cr, self.uid + ids = self.registry('res.partner').search(cr, uid, [('name', '=', 'test_per_class_teardown_partner')]) + self.assertEqual(0, len(ids), "Test partner found.") + self.registry('res.partner').create(cr, uid, {'name': 'test_per_class_teardown_partner'}) + ids = self.registry('res.partner').search(cr, uid, [('name', '=', 'test_per_class_teardown_partner')]) + self.assertEqual(1, len(ids), "Test partner not found.") + + def test_01(self): + """Don't find the created partner.""" + cr, uid = self.cr, self.uid + ids = self.registry('res.partner').search(cr, uid, [('name', '=', 'test_per_class_teardown_partner')]) + self.assertEqual(0, len(ids), "Test partner found.") + +if __name__ == '__main__': + unittest2.main() + +# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: