forked from gregorynicholas/flask-funktional-gae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_funktional_gae.py
571 lines (455 loc) · 16.2 KB
/
flask_funktional_gae.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
"""
flask_funktional_gae
~~~~~~~~~~~~~~~~~~~~
flask extension to make functional testing of flask applications with the
app engine sdk easier.
http://gregorynicholas.github.io/flask-funktional-gae
:copyright: (c) 2013 by gregorynicholas.
:license: MIT, see LICENSE for more details.
"""
from io import BytesIO
from json import loads
from flask import url_for, TestClient
from flask.testsuite import FlaskTestCase
from werkzeug import cached_property
from google.appengine.ext import ndb
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util as ds_stub_util
from random import choice
__all__ = ['TestCase', 'GaeTestClient',
'open_file', 'create_file',
'random_ndb_entity', 'random_word', 'random_email', 'ndbpprint',
'testbed']
# Global test client which should be set in setUp and cleared in tearDown for
# test cases which need to test the result of running tasks
taskqueue_test_client = None
try:
# we'll use signals for template-related tests if
# available in this version of Flask
import blinker
from flask import template_rendered
_is_signals = True
except ImportError:
_is_signals = False
class JsonResponseMixin:
"""
Mixin with testing helper methods.
"""
@cached_property
def json(self):
return loads(self.data)
class TestCase(FlaskTestCase):
"""
Enable app engine sdk stubs and disable services. This will replace calls
to the service with calls to the service stub.
"""
# for high replication datastore tests
# see: https://developers.google.com/appengine/docs/python/tools/localunittesting#Introducing_the_Python_Testing_Utilities
# create a consistency policy that will simulate the high replication
# consistency model.
pseudorandomhrconsistencypolicy = None
def create_app(self):
"""
Create your Flask app here, with any configuration you need.
"""
raise NotImplementedError
def __call__(self, result=None):
"""
Intitializes the appengine sdk service stubs, doing setup here
means subclasses don't have to call super.setUp.
"""
try:
self._pre_setup()
FlaskTestCase.__call__(self, result)
finally:
self._post_teardown()
def _pre_setup(self):
if hasattr(self, 'pre_setup_hook'):
self.pre_setup_hook()
# First, create an instance of the Testbed class.
self.tb = testbed.Testbed()
# Then activate the testbed, which prepares the
# service stubs for use
self.tb.activate()
# Next, declare which service stubs you want to use.
self.tb.init_mail_stub()
self.tb.init_xmpp_stub()
self.tb.init_files_stub()
# if PIL is not installed this will raise..
try:
import PIL
self.testbed.init_images_stub()
except ImportError:
pass
except testbed.StubNotSupportedError:
pass
self.tb.init_channel_stub()
self.tb.init_memcache_stub()
self.tb.init_urlfetch_stub()
self.tb.init_blobstore_stub()
self.tb.init_taskqueue_stub()
# hack to load in yaml file definition for queue names
# see: http://stackoverflow.com/questions/5324515/task-queue-works-from-view-but-unknownqueueerror-when-run-from-unit-tests
if hasattr(self, 'taskqueue_yaml_path'):
self.taskqueue_stub._root_path = self.taskqueue_yaml_path
self.tb.init_capability_stub()
self.tb.init_logservice_stub()
self.tb.init_app_identity_stub()
# for high replication datastore tests
if self.pseudorandomhrconsistencypolicy is not None:
self.policy = ds_stub_util.PseudoRandomHRConsistencyPolicy(
probability=self.pseudorandomhrconsistencypolicy)
# initialize the datastore stub with this policy.
# self.tb.init_datastore_v3_stub(consistency_policy=self.policy)
self.tb.init_datastore_v3_stub()
# from google.appengine.ext.ndb import model, tasklets
# model.make_connection()
# model.Model._reset_kind_map()
# ctx = tasklets.make_default_context()
# tasklets.set_context(ctx)
# ctx.set_datastore_policy(True)
# ctx.set_cache_policy(False)
# ctx.set_memcache_policy(True)
# flask setup
self.gae_client = GaeTestClient()
self.app = self._ctx = None
self.app = self.create_app()
self._orig_response_class = self.app.response_class
self.app.response_class = self._test_response_cls(self.app.response_class)
self.client = self.app.test_client()
self._ctx = self.app.test_request_context()
self._ctx.push()
self.templates = []
if _is_signals:
template_rendered.connect(self._add_template)
global taskqueue_test_client
taskqueue_test_client = self.app.test_client()
if hasattr(self, 'post_setup_hook'):
self.post_setup_hook()
def _post_teardown(self):
"""
Deactivate the testbed once the tests are completed. Otherwise the
original stubs will not be restored.
"""
if hasattr(self, 'pre_teardown_hook'):
self.pre_teardown_hook()
self.tb.deactivate()
if self._ctx is not None:
self._ctx.pop()
if self.app is not None:
self.app.response_class = self._orig_response_class
if _is_signals:
template_rendered.disconnect(self._add_template)
global taskqueue_test_client
taskqueue_test_client = None
if hasattr(self, 'post_teardown_hook'):
self.post_teardown_hook()
# flask helpers..
# ---------------------------------------------------------------------------
def url_for(self, handler, **kw):
"""
:param handler: String path to the route handler.
"""
with self.app.test_request_context():
url = url_for(handler, **kw)
server_name = self.app.config.get('SERVER_NAME')
if server_name is None:
return url
else:
pre, name, path = url.partition(server_name)
if len(path) == 0:
return None, pre
return pre + name, path
def _test_response_cls(self, response_class):
class TestResponse(response_class, JsonResponseMixin):
pass
return TestResponse
def assertTemplateUsed(self, name):
"""
Checks if a given template is used in the request. Only works if your
version of Flask has signals support (0.6+) and blinker is installed.
:param name: String name of the template.
"""
if not _is_signals:
raise RuntimeError("Signals not supported")
for template, context in self.templates:
if template.name == name:
return True
raise AssertionError("template %s not used".format(name))
assert_template_used = assertTemplateUsed
def assertStatus(self, response, status_code):
"""
Helper method to check matching response status.
:param response: Flask response
:param status_code: response status code (e.g. 200)
"""
self.assertIsNotNone(response, 'response is None')
self.assertIsInstance(response.status_code, int,
'response status_code is not an int.')
self.assertEqual(response.status_code, status_code,
'response status code {} is not {}'.format(
response.status_code, status_code))
assert_status = assertStatus
def assert200(self, response):
"""
Checks if response status code is 200
:param response: Flask response
"""
self.assertStatus(response, 200)
assert_200 = assert200
def assert400(self, response):
"""
Checks if response status code is 400
:param response: Flask response
"""
self.assertStatus(response, 400)
assert_400 = assert400
def assert401(self, response):
"""
Checks if response status code is 401
:param response: Flask response
"""
self.assertStatus(response, 401)
assert_401 = assert401
def assert403(self, response):
"""
Checks if response status code is 403
:param response: Flask response
"""
self.assertStatus(response, 403)
assert_403 = assert403
def assert404(self, response):
"""
Checks if response status code is 404
:param response: Flask response
"""
self.assertStatus(response, 404)
assert_404 = assert404
def assert405(self, response):
"""
Checks if response status code is 405
:param response: Flask response
"""
self.assertStatus(response, 405)
assert_405 = assert405
# mail api helpers..
# ---------------------------------------------------------------------------
@property
def mail_stub(self):
return self.tb.get_stub(testbed.MAIL_SERVICE_NAME)
def get_sent_messages(self, to=None, sender=None, subject=None, body=None,
html=None):
"""Get a list of ```mail.EmailMessage``` objects sent via the Mail API."""
return self.mail_stub.get_sent_messages(
to=to, sender=sender, subject=subject, body=body, html=html)
def assertMailSent(self, to=None, sender=None, subject=None, body=None,
html=None):
messages = self.get_sent_messages(
to=to, sender=sender, subject=subject, body=body, html=html)
self.assertNotEqual(0, len(messages),
"No matching email messages were sent.")
assert_mail_sent = assertMailSent
# memcache api helpers..
# ---------------------------------------------------------------------------
@property
def memcache_stub(self):
return self.tb.get_stub(testbed.MEMCACHE_SERVICE_NAME)
def assertMemcacheHits(self, hits):
"""Asserts that the memcache API has had ``hits`` successful lookups."""
self.assertEqual(
hits, self.memcache_stub.get_stats()['hits'])
assert_memcache_hits = assertMemcacheHits
def assertMemcacheItems(self, items):
"""Asserts that the memcache API has ``items`` key-value pairs."""
self.assertEqual(
items, self.memcache_stub.get_stats()['items'])
assert_memcache_items = assertMemcacheItems
# taskqueue api helpers..
# ---------------------------------------------------------------------------
@property
def taskqueue_stub(self):
return self.tb.get_stub(testbed.TASKQUEUE_SERVICE_NAME)
def get_tasks(self, url=None, name=None, queue_names=None):
"""Returns a list of `Task`_ objects with the specified criteria.
:param url:
URL criteria tasks must match. If ``url`` is ``None``, all tasks
will be matched.
:param name:
name criteria tasks must match. If ``name`` is ``None``, all tasks
will be matched.
:param queue_names:
queue name criteria tasks must match. If ``queue_name`` is ``None``
tasks in all queues will be matched."""
return self.taskqueue_stub.get_filtered_tasks(
url=url, name=name, queue_names=queue_names)
def assertTasksInQueue(self, n=None, url=None, name=None, queue_names=None):
"""Search for `Task`_ objects matching the given criteria and assert that
there are ``n`` tasks.
:usage::
deferred.defer(some_task, _name='some_task')
self.assertTasksInQueue(n=1, name='some_task')
:param n:
the number of tasks in the queue. If not specified, ``n`` defaults
to 0.
:param url:
URL criteria tasks must match. If ``url`` is ``None``, all tasks
will be matched.
:param name:
name criteria tasks must match. If ``name`` is ``None``, all tasks
will be matched.
:param queue_names:
queue name criteria tasks must match. If ``queue_name`` is ``None``
tasks in all queues will be matched."""
tasks = self.get_tasks(
url=url, name=name, queue_names=queue_names)
self.assertEqual(n or 0, len(tasks))
assert_tasks_in_queue = assertTasksInQueue
# blobstore api helpers..
# ---------------------------------------------------------------------------
@property
def blobstore_stub(self):
return self.tb.get_stub(testbed.BLOBSTORE_SERVICE_NAME)
def create_blob(self, blob_key, content):
"""Create new blob and put in storage and Datastore.
:param blob_key: String blob-key of new blob.
:param content: Content of new blob as a string.
:returns:
New Datastore BlobInfo entity without blob meta-data fields.
"""
return self.blobstore_stub.CreateBlob(blob_key, content)
def random_ndb_entity(self, model_class, **kw):
"""
:param model_class:
:param **kw:
:returns:
Instance of an `ndb.Model` subclass, with randomly selected values.
"""
return random_ndb_entity(model_class, **kw)
# mock a file upload request..
# see README for usage.
from StringIO import StringIO
from flask import Request
class FileObj(StringIO):
type_options = {}
def close(self):
print 'file upload test..'
class FileUploadRequest(Request):
def _get_file_stream(*args, **kwargs):
return FileObj()
def open_file(filename):
"""
:param filename: String, path to a file.
:returns: Instance of a tuple.
"""
f = open(filename, 'r')
data = f.read()
size = len(data)
f.close()
return (StringIO(data), filename, size)
def create_file(data='testing', filename=None):
"""
:param data: String of the filename.
:param filename: Optional, string of the filename.
:returns: Instance of a tuple for data, filename, size.
"""
if filename is None:
filename = random_word()
return (BytesIO(data), filename, len(data))
def random_email(domain=None):
"""
:param domain: String hostname of the email domain.
:returns:
"""
if domain is None:
domain = random_word()
return '{}@{}.com'.format(random_word(), domain)
def random_word():
"""
:returns: uuid1 as string.
"""
import uuid
return str(uuid.uuid1())
_propertytype_method_map = {
ndb.StringProperty: lambda _: random_word(),
ndb.TextProperty: lambda _: random_word(),
ndb.KeyProperty: lambda _: _random_key(_),
ndb.BooleanProperty: lambda _: None,
ndb.IntegerProperty: lambda _: None,
ndb.FloatProperty: lambda _: None,
ndb.DateTimeProperty: lambda _: None,
ndb.DateProperty: lambda _: None,
ndb.TimeProperty: lambda _: None,
ndb.BlobProperty: lambda _: None,
ndb.BlobKeyProperty: lambda _: None,
}
def _random_key(prop):
if prop._kind:
ndb.Key(prop._kind, random_word())
else:
ndb.Key(random_word(), random_word())
def random_ndb_entity(cls, **values):
"""
:returns:
Instance of an `ndb.Model` subclass, with randomly selected values.
"""
entity = cls()
entity._fix_up_properties() # this must be called to avoid uninitialized props
props = entity._properties
# set property specific values..
for key, prop in props.iteritems():
if key in values:
continue
if prop._default:
values[key] = prop._default
elif prop._choices:
values[key] = choice(prop._choices)
property_meth = None
for _proptype in _propertytype_method_map.keys():
if isinstance(prop, _proptype):
property_meth = _proptype
break
if property_meth:
values[key] = property_meth(prop)
if prop._repeated:
if values[key] is not None:
values[key] = [values[key]]
else:
values.pop(key)
entity.populate(**values)
return entity
def ndbpprint(model, level=1):
"""
Pretty prints an `ndb.Model`.
"""
body = ['<', type(model).__name__, ':']
values = model.to_dict()
for key, field in model._properties.iteritems():
value = values.get(key)
if value is not None:
body.append('\n%s%s: %s' % (
' '.join([' ' for idx in range(level)]), key, repr(value)))
body.append('>')
return ''.join(body)
class GaeTestClient(TestClient):
def __init__(self, test_case=None, *args, **kw):
TestClient.__init__(self, *args, **kw)
self.test_case = test_case
def open(self, handler, method_args, *args, **kw):
base_url, url = self.test_case.url_for(handler, **method_args)
follow_redirects = kw.pop('follow_redirects', True)
return self.test_case.app.open(
url, follow_redirects=follow_redirects, base_url=base_url, *args, **kw)
def post_raw(self, handler, method_args={}, *args, **kw):
return self.open(
handler, method_args, method='POST', *args, **kw)
def post_file(self, path, handler):
data, filename, size = open_file(filename=path)
return self.post_raw(handler,
data={'files[]': (data, filename)},
headers=[('enctype', 'multipart/form-data')])
def assertSuccessHttpResponse(self, test_case, response):
test_case.assert200(response)
assert_success_http_response = assertSuccessHttpResponse
def assertErrorHttpResponse(self, test_case, response, error_code=400):
test_case.assertStatus(response, error_code)
assert_error_http_response = assertErrorHttpResponse