[MERGE] test BaseCase now has ref() and browse_ref() helpers for working with ir.model.data identifiers, courtesy of Stephane Bidoul (Acsone)
bzr revid: odo@openerp.com-20121031162433-6ydiqbno4s78bnta
This commit is contained in:
commit
da47373e2a
|
@ -9,7 +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_basecase, \
|
||||
test_view_validation, test_uninstall, test_misc, test_db_cursor
|
||||
|
||||
fast_suite = [
|
||||
|
@ -21,7 +21,7 @@ checks = [
|
|||
test_html_sanitize,
|
||||
test_db_cursor,
|
||||
test_orm,
|
||||
test_per_class_teardown,
|
||||
test_basecase,
|
||||
test_view_validation,
|
||||
test_misc,
|
||||
]
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
The module :mod:`openerp.tests.common` provides a few helper and classes to write
|
||||
The module :mod:`openerp.tests.common` provides a few helpers and classes to write
|
||||
tests.
|
||||
"""
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
import unittest2
|
||||
|
@ -44,10 +43,11 @@ def stop_openerp():
|
|||
"""
|
||||
openerp.service.stop_services()
|
||||
|
||||
|
||||
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.
|
||||
"""
|
||||
|
||||
@classmethod
|
||||
|
@ -58,6 +58,29 @@ class BaseCase(unittest2.TestCase):
|
|||
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.
|
||||
|
||||
:param xid: fully-qualified record identifier, in the form ``module.identifier``
|
||||
:raise: ValueError if not found
|
||||
"""
|
||||
assert "." in xid, "this method requires a fully qualified parameter, in the following form: 'module.identifier'"
|
||||
module, xid = xid.split('.')
|
||||
_, 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.
|
||||
|
||||
:param xid: fully-qualified record identifier, in the form ``module.identifier``
|
||||
:raise: ValueError if not found
|
||||
"""
|
||||
assert "." in xid, "this method requires a fully qualified parameter, in the following form: 'module.identifier'"
|
||||
module, xid = xid.split('.')
|
||||
return self.registry('ir.model.data').get_object(self.cr, self.uid, module, xid)
|
||||
|
||||
|
||||
class TransactionCase(BaseCase):
|
||||
"""
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
import unittest2
|
||||
|
||||
import openerp
|
||||
import common
|
||||
|
||||
class test_per_class_teardown(common.SingleTransactionCase):
|
||||
class test_single_transaction_case(common.SingleTransactionCase):
|
||||
"""
|
||||
Check the whole-class transaction behavior of SingleTransactionCase.
|
||||
"""
|
||||
|
@ -22,7 +21,30 @@ class test_per_class_teardown(common.SingleTransactionCase):
|
|||
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):
|
||||
def test_20a(self):
|
||||
""" Create a partner with a XML ID """
|
||||
cr, uid = self.cr, self.uid
|
||||
res_partner = self.registry('res.partner')
|
||||
ir_model_data = self.registry('ir.model.data')
|
||||
pid, _ = res_partner.name_create(cr, uid, 'Mr Blue')
|
||||
ir_model_data.create(cr, uid, {'name': 'test_partner_blue',
|
||||
'module': 'base',
|
||||
'model': 'res.partner',
|
||||
'res_id': pid})
|
||||
def test_20b(self):
|
||||
""" Resolve xml id with ref() and browse_ref() """
|
||||
cr, uid = self.cr, self.uid
|
||||
res_partner = self.registry('res.partner')
|
||||
xid = 'base.test_partner_blue'
|
||||
p_ref = self.ref(xid)
|
||||
self.assertTrue(p_ref, "ref() should resolve xid to database ID")
|
||||
partner = res_partner.browse(cr, uid, p_ref)
|
||||
p_browse_ref = self.browse_ref(xid)
|
||||
self.assertEqual(partner, p_browse_ref, "browse_ref() should resolve xid to browse records")
|
||||
|
||||
|
||||
|
||||
class test_transaction_case(common.TransactionCase):
|
||||
"""
|
||||
Check the per-method transaction behavior of TransactionCase.
|
||||
"""
|
Loading…
Reference in New Issue