|
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 from google.appengine.api import users |
|
24 from google.appengine.api import memcache |
|
25 |
|
26 import unittest |
|
27 |
|
28 from soc.cache import sidebar |
|
29 |
|
30 |
|
31 class SidebarCacheTest(unittest.TestCase): |
|
32 """Tests that the sidebar properly uses caching |
|
33 """ |
|
34 |
|
35 def setUp(self): |
|
36 self.called = 0 |
|
37 self.user = users.get_current_user() |
|
38 |
|
39 def tearDown(self): |
|
40 memcache.flush_all() |
|
41 |
|
42 def testGetCurrentUser(self): |
|
43 """Santiy check to see if get_current_user returns same value |
|
44 """ |
|
45 |
|
46 self.assertEqual(self.user, users.get_current_user()) |
|
47 |
|
48 def testKey(self): |
|
49 """Test that the key method returns a unique key |
|
50 """ |
|
51 |
|
52 self.assertEqual("sidebar_for_users.User(email='test@example.com')", |
|
53 sidebar.key(self.user)) |
|
54 |
|
55 def testGet(self): |
|
56 """Test that get without setting something returns None |
|
57 """ |
|
58 self.assertEqual(None, sidebar.get()) |
|
59 |
|
60 def testGetPut(self): |
|
61 """Test that getting after putting gives back what you put in |
|
62 """ |
|
63 |
|
64 sidebar.put(42) |
|
65 self.assertEqual(42, sidebar.get()) |
|
66 |
|
67 def testFlush(self): |
|
68 """Test that getting after putting and flushing returns None |
|
69 """ |
|
70 |
|
71 sidebar.put(42) |
|
72 sidebar.flush() |
|
73 self.assertEqual(None, sidebar.get()) |
|
74 |
|
75 def testCache(self): |
|
76 """Test that the result of a cached sidebar is cached |
|
77 """ |
|
78 |
|
79 @sidebar.cache |
|
80 def getAnswer(): |
|
81 self.called = self.called + 1 |
|
82 return 42 |
|
83 |
|
84 self.assertEqual(42, getAnswer()) |
|
85 self.assertEqual(42, getAnswer()) |
|
86 self.assertEqual(self.called, 1) |
|
87 |
|
88 |