-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
204 lines (145 loc) · 6.73 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import unittest
import sys
from time import sleep
try:
import dev_appserver
except ImportError:
raise ImportError('App Engine must be in PYTHONPATH.')
sys.exit()
dev_appserver.fix_sys_path()
from google.appengine.api import memcache
from google.appengine.ext import testbed, webapp
from google.appengine.datastore import datastore_stub_util
import __init__ as gae_html
UCHAR = u"\u03B4" # lowercase delta
class BaseTestCase(unittest.TestCase):
def setUp(self):
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# Create a consistency policy that will simulate the High Replication consistency model.
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=0)
# Next, declare which service stubs you want to use.
self.testbed.init_datastore_v3_stub()
self.testbed.init_memcache_stub()
self.testbed.init_user_stub()
def tearDown(self):
self.testbed.deactivate()
class MockController(webapp.RequestHandler):
PATH = "/test-path"
HTML = "<html> <body>test response<!-- with comment -->" + UCHAR + " </body></html>"
MINIFIED = "<html> <body>test response" + UCHAR + " </body></html>"
MINIFIED_COMMENT = "<html> <body>test response<!-- with comment -->" + UCHAR + " </body></html>"
def __init__(self):
super(MockController, self).__init__()
self.called = False
def get(self):
self.response.write(self.HTML)
self.called = True
class BaseTestController(BaseTestCase):
""" abstract base class for tests that need request and response mocking """
def setUp(self):
super(BaseTestController, self).setUp()
self.controller = MockController()
self.controller.initialize(self.getMockRequest(), self.getMockResponse())
def getMockRequest(self):
class MockRequest(webapp.Request):
path = url = MockController.PATH
return MockRequest({})
def getMockResponse(self):
class MockResponse(webapp.Response):
unicode_body = ""
def write(self, content):
self.unicode_body = content
return MockResponse()
class TestDecorator(BaseTestController):
def test_default(self):
decorator = gae_html.cacheAndRender()(MockController.get)
response = decorator(self.controller)
assert response == MockController.MINIFIED
assert self.controller.called
# should be in memcache but not datastore by default
from_memcache = memcache.get(MockController.PATH)
assert from_memcache == MockController.MINIFIED
from_datastore = gae_html.HTMLCache.get_by_id(MockController.PATH, use_memcache=False)
assert from_datastore is None
def test_expires(self):
# we also use the datastore here so that we can check the expires on that
decorator = gae_html.cacheAndRender(expires=1, use_datastore=True)(MockController.get)
response = decorator(self.controller)
assert response == MockController.MINIFIED
assert self.controller.called
from_memcache = memcache.get(MockController.PATH)
assert from_memcache == MockController.MINIFIED
from_datastore = gae_html.HTMLCache.get_by_id(MockController.PATH, use_memcache=False)
assert from_datastore is not None
assert from_datastore.expires == 1
# sleep and then check the cache again
sleep(1)
from_memcache = memcache.get(MockController.PATH)
assert from_memcache is None
def test_minify(self):
decorator = gae_html.cacheAndRender(minify=False)(MockController.get)
response = decorator(self.controller)
assert response == MockController.HTML
assert self.controller.called
def test_include_comments(self):
decorator = gae_html.cacheAndRender(include_comments=True)(MockController.get)
response = decorator(self.controller)
assert response == MockController.MINIFIED_COMMENT
assert self.controller.called
def test_use_datastore(self):
decorator = gae_html.cacheAndRender(use_datastore=True)(MockController.get)
response = decorator(self.controller)
assert response == MockController.MINIFIED
assert self.controller.called
from_datastore = gae_html.HTMLCache.get_by_id(MockController.PATH, use_memcache=False)
assert from_datastore.html == MockController.MINIFIED
def test_skip_check(self):
decorator = gae_html.cacheAndRender(skip_check=lambda controller: True)(MockController.get)
response = decorator(self.controller)
assert response is None
assert self.controller.called
from_memcache = memcache.get(MockController.PATH)
assert from_memcache is None
def test_cached_memcache(self):
# test what happens when the response is already in memcache
memcache.set(MockController.PATH, MockController.MINIFIED)
decorator = gae_html.cacheAndRender()(MockController.get)
response = decorator(self.controller)
assert response == MockController.MINIFIED
assert not self.controller.called
assert self.controller.response.unicode_body == MockController.MINIFIED
def test_cached_datastore(self):
# test what happens when the response is already in the datastore
html_cache = gae_html.HTMLCache(id=MockController.PATH, html=MockController.MINIFIED)
html_cache.put()
decorator = gae_html.cacheAndRender(use_datastore=True)(MockController.get)
response = decorator(self.controller)
assert response == MockController.MINIFIED
assert not self.controller.called
assert self.controller.response.unicode_body == MockController.MINIFIED
class TestUtilities(BaseTestCase):
def test_getFromDatastore(self):
key = "test-key"
html = "test text" + UCHAR
assert gae_html.getFromDatastore(key) is None
html_cache = gae_html.HTMLCache(id=key, html=html, expires=10)
html_cache.put()
from_datastore = gae_html.getFromDatastore(key)
assert from_datastore is not None
assert from_datastore == html
html_cache.expires = 0
html_cache.put()
from_datastore = gae_html.getFromDatastore(key)
assert from_datastore is None
class TestModel(BaseTestCase):
def test_HTMLCache(self):
html_cache = gae_html.HTMLCache(html="test text" + UCHAR, expires=10)
html_cache.put()
assert not html_cache.expired
html_cache.expires = 0
assert html_cache.expired
if __name__ == '__main__':
unittest.main()