This repository has been archived by the owner on Jul 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtestutil.py
112 lines (94 loc) · 3.23 KB
/
testutil.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
"""Unit test utilities.
"""
__author__ = ['Ryan Barrett <mockfacebook@ryanb.org>']
import cStringIO
import json
import re
import sqlite3
import sys
import unittest
import urllib
import webapp2
import schemautil
import server
def maybe_read(dataset_cls):
"""Tries to read and return a dataset. If it fails, prints an error.
"""
try:
return dataset_cls.read()
except IOError, e:
print >> sys.stderr, 'Warning: skipping example data tests due to:\n%s' % e
return None
class HandlerTest(unittest.TestCase):
"""Base test class for webapp2 request handlers.
Attributes:
conn: SQLite db connection
app: WSGIApplication
"""
ME = '1'
conn = None
def setUp(self, *handler_classes):
"""Args:
handler_classes: RequestHandlers to initialize
"""
super(HandlerTest, self).setUp()
self.conn = schemautil.get_db(':memory:')
for cls in handler_classes:
cls.init(self.conn, self.ME)
self.app = server.application()
def expect(self, path, expected, args=None, expected_status=200):
"""Makes a request and checks the response.
Args:
path: string
expected: if string, the expected response body. if list or dict,
the expected JSON response contents.
args: passed to get_response()
expected_status: integer, expected HTTP response status
"""
response = None
results = None
try:
response = self.get_response(path, args=args)
self.assertEquals(expected_status, response.status_int)
response = response.body
if isinstance(expected, basestring):
self.assertEquals(expected, response)
else:
results = json.loads(response)
if not isinstance(expected, list):
expected = [expected]
if not isinstance(results, list):
results = [results]
expected.sort()
results.sort()
self.assertEquals(len(expected), len(results), `expected, results`)
for e, r in zip(expected, results):
self.assert_dict_equals(e, r)
except:
print >> sys.stderr, '\nquery: %s %s' % (path, args)
print >> sys.stderr, 'expected: %r' % expected
print >> sys.stderr, 'received: %r' % results if results else response
raise
def get_response(self, path, args=None):
if args:
path = '%s?%s' % (path, urllib.urlencode(args))
return self.app.get_response(path)
# TODO: for the love of god, refactor, or even better, find a more supported
# utility somewhere else.
def assert_dict_equals(self, expected, actual):
msgs = []
if isinstance(expected, re._pattern_type):
if not re.match(expected, actual):
self.fail("%r doesn't match %s" % (expected, actual))
# this is only here because we don't exactly match FB in whether we return
# or omit some "empty" values, e.g. 0, null, ''. see the TODO in graph_on_fql.py.
elif not expected and not actual:
return True
elif isinstance(expected, dict) and isinstance(actual, dict):
for key in set(expected.keys()) | set(actual.keys()):
self.assert_dict_equals(expected.get(key), actual.get(key))
else:
if isinstance(expected, list) and isinstance(actual, list):
expected.sort()
actual.sort()
self.assertEquals(expected, actual)