diff --git a/openerp/addons/base/tests/__init__.py b/openerp/addons/base/tests/__init__.py index f41543cffcd..82b79cfc693 100644 --- a/openerp/addons/base/tests/__init__.py +++ b/openerp/addons/base/tests/__init__.py @@ -14,6 +14,7 @@ import test_ir_values import test_mail import test_menu import test_orm +import test_ormcache import test_osv import test_qweb import test_res_config diff --git a/openerp/addons/base/tests/test_ormcache.py b/openerp/addons/base/tests/test_ormcache.py new file mode 100644 index 00000000000..9d44695a32c --- /dev/null +++ b/openerp/addons/base/tests/test_ormcache.py @@ -0,0 +1,36 @@ + +from openerp.tests import common +from openerp.tools import get_cache_key_counter + +class TestOrmcache(common.TransactionCase): + def test_ormcache(self): + """ Test the effectiveness of the ormcache() decorator. """ + IMD = self.env['ir.model.data'] + XMLID = 'base.group_no_one' + + # retrieve the cache, its key and stat counter + cache, key, counter = get_cache_key_counter(IMD.xmlid_lookup, self.cr, self.uid, XMLID) + hit = counter.hit + miss = counter.miss + + # clear the cache of ir.model.data.xmlid_lookup, retrieve its key and + IMD.xmlid_lookup.clear_cache(IMD) + self.assertNotIn(key, cache) + + # lookup some reference + self.env.ref(XMLID) + self.assertEqual(counter.hit, hit) + self.assertEqual(counter.miss, miss + 1) + self.assertIn(key, cache) + + # lookup again + self.env.ref(XMLID) + self.assertEqual(counter.hit, hit + 1) + self.assertEqual(counter.miss, miss + 1) + self.assertIn(key, cache) + + # lookup again + self.env.ref(XMLID) + self.assertEqual(counter.hit, hit + 2) + self.assertEqual(counter.miss, miss + 1) + self.assertIn(key, cache) diff --git a/openerp/tools/cache.py b/openerp/tools/cache.py index 8d244e38f68..339c942f3c7 100644 --- a/openerp/tools/cache.py +++ b/openerp/tools/cache.py @@ -234,5 +234,14 @@ def log_ormcache_stats(sig=None, frame=None): me.dbname = me_dbname + +def get_cache_key_counter(bound_method, *args, **kwargs): + """ Return the cache, key and stat counter for the given call. """ + model = bound_method.im_self + ormcache = bound_method.clear_cache.im_self + cache, key0, counter = ormcache.lru(model) + key = key0 + ormcache.key(model, *args, **kwargs) + return cache, key, counter + # For backward compatibility cache = ormcache