|
1 #!/usr/bin/python2.5 |
|
2 # |
|
3 # Copyright 2008 the Melange authors. |
|
4 # |
|
5 # Licensed under the Apache License, Version 2.0 (the "License"); |
|
6 # you may not use this file except in compliance with the License. |
|
7 # You may obtain a copy of the License at |
|
8 # |
|
9 # http://www.apache.org/licenses/LICENSE-2.0 |
|
10 # |
|
11 # Unless required by applicable law or agreed to in writing, software |
|
12 # distributed under the License is distributed on an "AS IS" BASIS, |
|
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
14 # See the License for the specific language governing permissions and |
|
15 # limitations under the License. |
|
16 |
|
17 |
|
18 __authors__ = [ |
|
19 '"Sverre Rabbelier" <sverre@rabbelier.nl>', |
|
20 ] |
|
21 |
|
22 |
|
23 import unittest |
|
24 |
|
25 from google.appengine.api import memcache |
|
26 |
|
27 from soc.cache import base |
|
28 |
|
29 |
|
30 class CacheDecoratorTest(unittest.TestCase): |
|
31 """Tests that the @cache decorator caches the result |
|
32 """ |
|
33 |
|
34 def setUp(self): |
|
35 self.called = 0 |
|
36 decorator = base.getCacher(self.get, self.put) |
|
37 |
|
38 @decorator |
|
39 def failOnSecondCall(): |
|
40 self.called = self.called + 1 |
|
41 if self.called > 1: |
|
42 self.fail("method got called twice") |
|
43 |
|
44 self.failOnSecondCall = failOnSecondCall |
|
45 |
|
46 def tearDown(self): |
|
47 memcache.flush_all() |
|
48 |
|
49 def get(self): |
|
50 return memcache.get('answer_to_life') |
|
51 |
|
52 def put(self, answer): |
|
53 memcache.add('answer_to_life', 42) |
|
54 |
|
55 def testMemcache(self): |
|
56 """Santiy check to see if memcache is working |
|
57 """ |
|
58 |
|
59 memcache.add('answer_to_life', 42) |
|
60 self.assertEqual(memcache.get('answer_to_life'), 42) |
|
61 |
|
62 def testSidebarCaching(self): |
|
63 """Test that the caching decorator caches |
|
64 """ |
|
65 |
|
66 self.failOnSecondCall() |
|
67 self.failOnSecondCall() |