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 |
65 self.assertEqual(l[-1], (1, 3)) |
114 self.assertEqual(l[-1], (1, 3)) |
66 self.assertEqual(len(l), 3) |
115 self.assertEqual(len(l), 3) |
67 # XXX |
116 # XXX |
68 self.assertEqual(l[4], (1, 3)) |
117 self.assertEqual(l[4], (1, 3)) |
69 |
118 |
70 self.failIf(RepeatList(0, None)) |
119 self.assertFalse(RepeatList(0, None)) |
71 |
120 |
72 def test_slice(self): |
121 def test_slice(self): |
73 l = RepeatList(3, (1, 3)) |
122 l = RepeatList(3, (1, 3)) |
74 self.assertEqual(l[0:1], [(1, 3)]) |
123 self.assertEqual(l[0:1], [(1, 3)]) |
75 self.assertEqual(l[0:4], [(1, 3)]*3) |
124 self.assertEqual(l[0:4], [(1, 3)]*3) |
157 |
206 |
158 def test_encoding_unknown_stuff(self): |
207 def test_encoding_unknown_stuff(self): |
159 self.assertEqual(self.encode(TestCase), 'null') |
208 self.assertEqual(self.encode(TestCase), 'null') |
160 |
209 |
161 class HTMLHeadTC(CubicWebTC): |
210 class HTMLHeadTC(CubicWebTC): |
|
211 |
|
212 def htmlhead(self, datadir_url): |
|
213 req = self.request() |
|
214 base_url = u'http://test.fr/data/' |
|
215 req.datadir_url = base_url |
|
216 head = HTMLHead(req) |
|
217 return head |
|
218 |
162 def test_concat_urls(self): |
219 def test_concat_urls(self): |
163 base_url = u'http://test.fr/data/' |
220 base_url = u'http://test.fr/data/' |
164 head = HTMLHead(base_url) |
221 head = self.htmlhead(base_url) |
165 urls = [base_url + u'bob1.js', |
222 urls = [base_url + u'bob1.js', |
166 base_url + u'bob2.js', |
223 base_url + u'bob2.js', |
167 base_url + u'bob3.js'] |
224 base_url + u'bob3.js'] |
168 result = head.concat_urls(urls) |
225 result = head.concat_urls(urls) |
169 expected = u'http://test.fr/data/??bob1.js,bob2.js,bob3.js' |
226 expected = u'http://test.fr/data/??bob1.js,bob2.js,bob3.js' |
170 self.assertEqual(result, expected) |
227 self.assertEqual(result, expected) |
171 |
228 |
172 def test_group_urls(self): |
229 def test_group_urls(self): |
173 base_url = u'http://test.fr/data/' |
230 base_url = u'http://test.fr/data/' |
174 head = HTMLHead(base_url) |
231 head = self.htmlhead(base_url) |
175 urls_spec = [(base_url + u'bob0.js', None), |
232 urls_spec = [(base_url + u'bob0.js', None), |
176 (base_url + u'bob1.js', None), |
233 (base_url + u'bob1.js', None), |
177 (u'http://ext.com/bob2.js', None), |
234 (u'http://ext.com/bob2.js', None), |
178 (u'http://ext.com/bob3.js', None), |
235 (u'http://ext.com/bob3.js', None), |
179 (base_url + u'bob4.css', 'all'), |
236 (base_url + u'bob4.css', 'all'), |
194 ] |
251 ] |
195 self.assertEqual(list(result), expected) |
252 self.assertEqual(list(result), expected) |
196 |
253 |
197 def test_getvalue_with_concat(self): |
254 def test_getvalue_with_concat(self): |
198 base_url = u'http://test.fr/data/' |
255 base_url = u'http://test.fr/data/' |
199 head = HTMLHead(base_url) |
256 head = self.htmlhead(base_url) |
200 head.add_js(base_url + u'bob0.js') |
257 head.add_js(base_url + u'bob0.js') |
201 head.add_js(base_url + u'bob1.js') |
258 head.add_js(base_url + u'bob1.js') |
202 head.add_js(u'http://ext.com/bob2.js') |
259 head.add_js(u'http://ext.com/bob2.js') |
203 head.add_js(u'http://ext.com/bob3.js') |
260 head.add_js(u'http://ext.com/bob3.js') |
204 head.add_css(base_url + u'bob4.css') |
261 head.add_css(base_url + u'bob4.css') |
222 </head> |
279 </head> |
223 """ |
280 """ |
224 self.assertEqual(result, expected) |
281 self.assertEqual(result, expected) |
225 |
282 |
226 def test_getvalue_without_concat(self): |
283 def test_getvalue_without_concat(self): |
227 base_url = u'http://test.fr/data/' |
284 self.config.global_set_option('concat-resources', False) |
228 head = HTMLHead() |
285 try: |
229 head.add_js(base_url + u'bob0.js') |
286 base_url = u'http://test.fr/data/' |
230 head.add_js(base_url + u'bob1.js') |
287 head = self.htmlhead(base_url) |
231 head.add_js(u'http://ext.com/bob2.js') |
288 head.add_js(base_url + u'bob0.js') |
232 head.add_js(u'http://ext.com/bob3.js') |
289 head.add_js(base_url + u'bob1.js') |
233 head.add_css(base_url + u'bob4.css') |
290 head.add_js(u'http://ext.com/bob2.js') |
234 head.add_css(base_url + u'bob5.css') |
291 head.add_js(u'http://ext.com/bob3.js') |
235 head.add_css(base_url + u'bob6.css', 'print') |
292 head.add_css(base_url + u'bob4.css') |
236 head.add_css(base_url + u'bob7.css', 'print') |
293 head.add_css(base_url + u'bob5.css') |
237 head.add_ie_css(base_url + u'bob8.css') |
294 head.add_css(base_url + u'bob6.css', 'print') |
238 head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]') |
295 head.add_css(base_url + u'bob7.css', 'print') |
239 result = head.getvalue() |
296 head.add_ie_css(base_url + u'bob8.css') |
240 expected = u"""<head> |
297 head.add_ie_css(base_url + u'bob9.css', 'print', u'[if lt IE 7]') |
|
298 result = head.getvalue() |
|
299 expected = u"""<head> |
241 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/> |
300 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob4.css"/> |
242 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/> |
301 <link rel="stylesheet" type="text/css" media="all" href="http://test.fr/data/bob5.css"/> |
243 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/> |
302 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob6.css"/> |
244 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob7.css"/> |
303 <link rel="stylesheet" type="text/css" media="print" href="http://test.fr/data/bob7.css"/> |
245 <!--[if lt IE 8]> |
304 <!--[if lt IE 8]> |
251 <script type="text/javascript" src="http://test.fr/data/bob1.js"></script> |
310 <script type="text/javascript" src="http://test.fr/data/bob1.js"></script> |
252 <script type="text/javascript" src="http://ext.com/bob2.js"></script> |
311 <script type="text/javascript" src="http://ext.com/bob2.js"></script> |
253 <script type="text/javascript" src="http://ext.com/bob3.js"></script> |
312 <script type="text/javascript" src="http://ext.com/bob3.js"></script> |
254 </head> |
313 </head> |
255 """ |
314 """ |
256 self.assertEqual(result, expected) |
315 self.assertEqual(result, expected) |
|
316 finally: |
|
317 self.config.global_set_option('concat-resources', True) |
257 |
318 |
258 class DocTest(DocTest): |
319 class DocTest(DocTest): |
259 from cubicweb import utils as module |
320 from cubicweb import utils as module |
260 |
321 |
261 if __name__ == '__main__': |
322 if __name__ == '__main__': |