diff -r 1bb43e31032d -r a14b6562082b test/unittest_utils.py --- a/test/unittest_utils.py Tue Feb 14 15:14:22 2012 +0100 +++ b/test/unittest_utils.py Tue Jul 10 15:07:23 2012 +0200 @@ -26,7 +26,7 @@ from cubicweb.devtools.testlib import CubicWebTC from cubicweb.utils import (make_uid, UStringIO, SizeConstrainedList, - RepeatList, HTMLHead) + RepeatList, HTMLHead, QueryCache) from cubicweb.entity import Entity try: @@ -50,6 +50,55 @@ 'some numeric character, got %s' % uid) d.add(uid) +class TestQueryCache(TestCase): + def test_querycache(self): + c = QueryCache(ceiling=20) + # write only + for x in xrange(10): + c[x] = x + self.assertEqual(c._usage_report(), + {'transientcount': 0, + 'itemcount': 10, + 'permanentcount': 0}) + c = QueryCache(ceiling=10) + # we should also get a warning + for x in xrange(20): + c[x] = x + self.assertEqual(c._usage_report(), + {'transientcount': 0, + 'itemcount': 10, + 'permanentcount': 0}) + # write + reads + c = QueryCache(ceiling=20) + for n in xrange(4): + for x in xrange(10): + c[x] = x + c[x] + self.assertEqual(c._usage_report(), + {'transientcount': 10, + 'itemcount': 10, + 'permanentcount': 0}) + c = QueryCache(ceiling=20) + for n in xrange(17): + for x in xrange(10): + c[x] = x + c[x] + self.assertEqual(c._usage_report(), + {'transientcount': 0, + 'itemcount': 10, + 'permanentcount': 10}) + c = QueryCache(ceiling=20) + for n in xrange(17): + for x in xrange(10): + c[x] = x + if n % 2: + c[x] + if x % 2: + c[x] + self.assertEqual(c._usage_report(), + {'transientcount': 5, + 'itemcount': 10, + 'permanentcount': 5}) class UStringIOTC(TestCase): def test_boolean_value(self): @@ -67,7 +116,7 @@ # XXX self.assertEqual(l[4], (1, 3)) - self.failIf(RepeatList(0, None)) + self.assertFalse(RepeatList(0, None)) def test_slice(self): l = RepeatList(3, (1, 3)) @@ -159,9 +208,17 @@ self.assertEqual(self.encode(TestCase), 'null') class HTMLHeadTC(CubicWebTC): + + def htmlhead(self, datadir_url): + req = self.request() + base_url = u'http://test.fr/data/' + req.datadir_url = base_url + head = HTMLHead(req) + return head + def test_concat_urls(self): base_url = u'http://test.fr/data/' - head = HTMLHead(base_url) + head = self.htmlhead(base_url) urls = [base_url + u'bob1.js', base_url + u'bob2.js', base_url + u'bob3.js'] @@ -171,7 +228,7 @@ def test_group_urls(self): base_url = u'http://test.fr/data/' - head = HTMLHead(base_url) + head = self.htmlhead(base_url) urls_spec = [(base_url + u'bob0.js', None), (base_url + u'bob1.js', None), (u'http://ext.com/bob2.js', None), @@ -196,7 +253,7 @@ def test_getvalue_with_concat(self): base_url = u'http://test.fr/data/' - head = HTMLHead(base_url) + head = self.htmlhead(base_url) head.add_js(base_url + u'bob0.js') head.add_js(base_url + u'bob1.js') head.add_js(u'http://ext.com/bob2.js') @@ -224,20 +281,22 @@ self.assertEqual(result, expected) def test_getvalue_without_concat(self): - base_url = u'http://test.fr/data/' - head = HTMLHead() - head.add_js(base_url + u'bob0.js') - head.add_js(base_url + u'bob1.js') - head.add_js(u'http://ext.com/bob2.js') - head.add_js(u'http://ext.com/bob3.js') - head.add_css(base_url + u'bob4.css') - head.add_css(base_url + u'bob5.css') - head.add_css(base_url + u'bob6.css', 'print') - head.add_css(base_url + u'bob7.css', 'print') - head.add_ie_css(base_url + u'bob8.css') - head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]') - result = head.getvalue() - expected = u"""<head> + self.config.global_set_option('concat-resources', False) + try: + base_url = u'http://test.fr/data/' + head = self.htmlhead(base_url) + head.add_js(base_url + u'bob0.js') + head.add_js(base_url + u'bob1.js') + head.add_js(u'http://ext.com/bob2.js') + head.add_js(u'http://ext.com/bob3.js') + head.add_css(base_url + u'bob4.css') + head.add_css(base_url + u'bob5.css') + head.add_css(base_url + u'bob6.css', 'print') + head.add_css(base_url + u'bob7.css', 'print') + head.add_ie_css(base_url + u'bob8.css') + head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]') + result = head.getvalue() + expected = u"""<head> <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/> <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/> <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/> @@ -253,7 +312,9 @@ <script type="text/javascript" src="http://ext.com/bob3.js"></script> </head> """ - self.assertEqual(result, expected) + self.assertEqual(result, expected) + finally: + self.config.global_set_option('concat-resources', True) class DocTest(DocTest): from cubicweb import utils as module