test/unittest_utils.py
changeset 7954 a3d3220669d6
parent 7791 31bb51ea5485
child 8682 20bd1cdf86ae
equal deleted inserted replaced
7953:a37531c8a4a6 7954:a3d3220669d6
    24 
    24 
    25 from logilab.common.testlib import TestCase, DocTest, unittest_main
    25 from logilab.common.testlib import TestCase, DocTest, unittest_main
    26 
    26 
    27 from cubicweb.devtools.testlib import CubicWebTC
    27 from cubicweb.devtools.testlib import CubicWebTC
    28 from cubicweb.utils import (make_uid, UStringIO, SizeConstrainedList,
    28 from cubicweb.utils import (make_uid, UStringIO, SizeConstrainedList,
    29                             RepeatList, HTMLHead)
    29                             RepeatList, HTMLHead, QueryCache)
    30 from cubicweb.entity import Entity
    30 from cubicweb.entity import Entity
    31 
    31 
    32 try:
    32 try:
    33     from cubicweb.utils import CubicWebJsonEncoder, json
    33     from cubicweb.utils import CubicWebJsonEncoder, json
    34 except ImportError:
    34 except ImportError:
    48             if re.match('\d', uid):
    48             if re.match('\d', uid):
    49                 self.fail('make_uid must not return something begining with '
    49                 self.fail('make_uid must not return something begining with '
    50                           'some numeric character, got %s' % uid)
    50                           'some numeric character, got %s' % uid)
    51             d.add(uid)
    51             d.add(uid)
    52 
    52 
       
    53 class TestQueryCache(TestCase):
       
    54     def test_querycache(self):
       
    55         c = QueryCache(ceiling=20)
       
    56         # write only
       
    57         for x in xrange(10):
       
    58             c[x] = x
       
    59         self.assertEqual(c._usage_report(),
       
    60                          {'transientcount': 0,
       
    61                           'itemcount': 10,
       
    62                           'permanentcount': 0})
       
    63         c = QueryCache(ceiling=10)
       
    64         # we should also get a warning
       
    65         for x in xrange(20):
       
    66             c[x] = x
       
    67         self.assertEqual(c._usage_report(),
       
    68                          {'transientcount': 0,
       
    69                           'itemcount': 10,
       
    70                           'permanentcount': 0})
       
    71         # write + reads
       
    72         c = QueryCache(ceiling=20)
       
    73         for n in xrange(4):
       
    74             for x in xrange(10):
       
    75                 c[x] = x
       
    76                 c[x]
       
    77         self.assertEqual(c._usage_report(),
       
    78                          {'transientcount': 10,
       
    79                           'itemcount': 10,
       
    80                           'permanentcount': 0})
       
    81         c = QueryCache(ceiling=20)
       
    82         for n in xrange(17):
       
    83             for x in xrange(10):
       
    84                 c[x] = x
       
    85                 c[x]
       
    86         self.assertEqual(c._usage_report(),
       
    87                          {'transientcount': 0,
       
    88                           'itemcount': 10,
       
    89                           'permanentcount': 10})
       
    90         c = QueryCache(ceiling=20)
       
    91         for n in xrange(17):
       
    92             for x in xrange(10):
       
    93                 c[x] = x
       
    94                 if n % 2:
       
    95                     c[x]
       
    96                 if x % 2:
       
    97                     c[x]
       
    98         self.assertEqual(c._usage_report(),
       
    99                          {'transientcount': 5,
       
   100                           'itemcount': 10,
       
   101                           'permanentcount': 5})
    53 
   102 
    54 class UStringIOTC(TestCase):
   103 class UStringIOTC(TestCase):
    55     def test_boolean_value(self):
   104     def test_boolean_value(self):
    56         self.assert_(UStringIO())
   105         self.assert_(UStringIO())
    57 
   106