[ADD] ormcache: add a unit test
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user