-
Notifications
You must be signed in to change notification settings - Fork 1
/
a3_checker.py
293 lines (236 loc) · 9.98 KB
/
a3_checker.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
"""A simple checker for types of functions in twitterverse_functions.py."""
from typing import Any, Dict, List
from io import StringIO
import unittest
import checker_generic
import twitterverse_functions as tf
FILENAME = 'twitterverse_functions.py'
PYTA_CONFIG = 'pyta/a3_pyta.txt'
TARGET_LEN = 79
SEP = '='
CONSTANTS = {
'USERNAME': 'username',
'NAME': 'name',
'LOCATION': 'location',
'WEB': 'web',
'BIO': 'bio',
'FOLLOWING': 'following',
'ENDBIO': 'ENDBIO',
'END': 'END',
'SEARCH': 'SEARCH',
'FILTER': 'FILTER',
'PRESENT': 'PRESENT',
'OPERATIONS': 'operations',
'FOLLOWER': 'follower',
'FOLLOWERS': 'followers',
'NAME_INCLUDES': 'name-includes',
'LOCATION_INCLUDES': 'location-includes',
'SORT_BY': 'sort-by',
'POPULARITY': 'popularity',
'FORMAT': 'format',
'LONG': 'long'
}
DATA_FILE = """tomCruise
Tom Cruise
Los Angeles, CA
http://www.tomcruise.com
Official TomCruise.com crew tweets. We love you guys!
Visit us at Facebook!
ENDBIO
katieH
END
katieH
Katie Holmes
www.tomkat.com
ENDBIO
END
"""
QUERY_FILE = """SEARCH
tomCruise
following
followers
FILTER
following katieH
name-includes tom
location-includes CA
PRESENT
sort-by username
format long
"""
TWITTER_DATA = {'tomCruise': {'name': 'Tom Cruise',
'location': 'Los Angeles, CA',
'web': 'http://www.tomcruise.com',
'bio': 'Official TomCruise.com crew tweets. ' +
'We love you guys!\nVisit us at Facebook!',
'following': ['katieH']},
'katieH': {'name': 'Katie Holmes', 'location': '',
'web': 'www.tomkat.com', 'bio': '', 'following': []}}
QUERY = {'SEARCH': {'username': 'tomCruise',
'operations': ['following', 'followers']},
'FILTER': {'following': 'katieH',
'name-includes': 'tom', 'location-includes': 'CA'},
'PRESENT': {'sort-by': 'username', 'format': 'long'}}
LONG_RESULT = """----------
katieH
name: Katie Holmes
location:
website: www.tomkat.com
bio:
following: []
----------
tomCruise
name: Tom Cruise
location: Los Angeles, CA
website: http://www.tomcruise.com
bio:
Official TomCruise.com crew tweets. We love you guys!
Visit us at Facebook!
following: ['katieH']
----------
"""
class CheckTest(unittest.TestCase):
"""Type checker for assignment functions."""
def test_process_data(self) -> None:
"""Test function process_data."""
data_keys = ['name', 'location', 'web', 'bio', 'following']
msg = 'process_data should return a TwitterverseDict'
open_data_file = StringIO(DATA_FILE)
result = tf.process_data(open_data_file)
for user in result:
self.assertTrue(isinstance(user, str), msg)
self._has_these_keys(result[user], data_keys, msg)
for key in result[user]:
if key == 'following':
self.assertTrue(isinstance(result[user][key], list), msg)
for item in result[user][key]:
self.assertTrue(isinstance(item, str), msg)
else:
self.assertTrue(isinstance(result[user][key], str), msg)
def test_process_query(self) -> None:
"""Test function process_query."""
query_keys = ['SEARCH', 'FILTER', 'PRESENT']
msg = 'process_query should return a valid QueryDict'
open_query_file = StringIO(QUERY_FILE)
result = tf.process_query(open_query_file)
self._has_these_keys(result, query_keys, msg)
# Search spec
self._has_these_keys(result['SEARCH'], ['username', 'operations'], msg)
self.assertTrue(isinstance(result['SEARCH']['operations'], list), msg)
for item in result['SEARCH']['operations']:
self.assertTrue(isinstance(item, str), msg)
self.assertTrue(isinstance(result['SEARCH']['username'], str), msg)
# Filter spec
filter_keys = ['following', 'follower', 'name-includes',
'location-includes', 'bio-includes']
self._has_these_keys(result['FILTER'], filter_keys, msg)
self._is_dict_of_Ks_and_Vs(result['FILTER'], str, str, msg)
# Sorting spec
self._has_these_keys(result['PRESENT'], ['sort-by', 'format'], msg)
self._is_dict_of_Ks_and_Vs(result['PRESENT'], str, str, msg)
def test_get_search_results(self) -> None:
"""Test function get_search_results."""
self._test_returns_list_of(tf.get_search_results,
[TWITTER_DATA, QUERY['SEARCH']], [str])
def test_get_filter_results(self) -> None:
"""Test function get_filter_results."""
self._test_returns_list_of(tf.get_filter_results,
[TWITTER_DATA, ['tomCruise', 'katieH'],
QUERY['FILTER']], [str])
def test_get_present_string(self) -> None:
"""Test function get_present_string."""
result = tf.get_present_string(TWITTER_DATA,
['tomCruise', 'katieH'],
QUERY['PRESENT'])
msg = '''get_present_string should return a str, but returned {}'''
self.assertTrue(isinstance(result, str), msg.format(type(result)))
msg = '''incorrect formatting of presentation string, expected {}\n got {}\n'''
self.assertEqual(result, LONG_RESULT, msg.format(LONG_RESULT, result))
def test_all_followers(self) -> None:
"""Test function all_followers."""
self._test_returns_list_of(tf.all_followers,
[TWITTER_DATA, 'katieH'], [str])
def test_check_constants(self) -> None:
"""Values of constants."""
print('\nChecking that constants refer to their original values')
self._check_constants(CONSTANTS, tf)
print(' check complete')
def _test_returns_list_of(self, func, args, types):
"""Check that func when called with args returns a list of elements
of typef from types.
"""
result = checker_generic.type_check_simple(func, args, list)
self.assertTrue(result[0], result[1])
msg = '{} should return a list of length {}'
self.assertEqual(len(result[1]), len(types),
msg.format(func.__name__, len(types)))
msg = ('Element at index {} in the list returned by {} '
'should be of type {}. Got {}.')
for i, typ in enumerate(types):
self.assertTrue(isinstance(result[1][i], typ),
msg.format(i, func.__name__, typ, result[1][i]))
def _has_these_keys(self, result: object, valid_keys: List[str], msg: str):
"""Check if result is a dict with keys from a set of valid keys.
"""
self.assertTrue(isinstance(result, dict), msg)
for k in result:
self.assertTrue(k in valid_keys,
msg + ', but key ' + str(k) + ' is not in ' +
str(valid_keys))
def _is_dict_of_Ks_and_Vs(self, result: object, key_tp: type,
val_tp: type, msg: str):
"""Check if result is a dict with keys of type key_tp and values
of type val_tp.
"""
self.assertTrue(isinstance(result, dict), msg)
for (key, val) in result.items():
self.assertTrue(isinstance(key, key_tp),
(msg + ', but one or more keys is not of type '
+ str(key_tp)))
self.assertTrue(isinstance(val, val_tp),
(msg + ', but value ' + str(val) + ' is not of type '
+ str(val_tp)))
def _check_simple_type(self, func: callable, args: list,
expected: type) -> None:
"""Check that func called with arguments args returns a value of type
expected. Display the progress and the result of the check.
"""
print('\nChecking {}...'.format(func.__name__))
result = checker_generic.type_check_simple(func, args, expected)
self.assertTrue(result[0], result[1])
print(' check complete')
def _test_returns_list_of(self, func, args, types):
"""Check that func when called with args returns a list of elements
of typef from types.
"""
print('\nChecking {}...'.format(func.__name__))
result = checker_generic.type_check_simple(func, args, list)
self.assertTrue(result[0], result[1])
msg = '{} should return a list of length {}'
self.assertEqual(len(result[1]), len(types),
msg.format(func.__name__, len(types)))
msg = ('Element at index {} in the list returned by get_station '
'should be of type {}. Got {}.')
for i, typ in enumerate(types):
self.assertTrue(isinstance(result[1][i], typ),
msg.format(i, typ, result[1][i]))
print(' check complete')
def _check_constants(self, name2value: Dict[str, object], mod: Any) -> None:
"""Check that, for each (name, value) pair in name2value, the value of
a variable named name in module mod is value.
"""
for name, expected in name2value.items():
actual = getattr(mod, name)
msg = 'The value of constant {} should be {} but is {}.'.format(
name, expected, actual)
self.assertEqual(expected, actual, msg)
checker_generic.ensure_no_io('twitterverse_functions')
print(''.center(TARGET_LEN, SEP))
print(' Start: checking coding style '.center(TARGET_LEN, SEP))
checker_generic.run_pyta(FILENAME, PYTA_CONFIG)
print(' End checking coding style '.center(TARGET_LEN, SEP))
print(' Start: checking type contracts '.center(TARGET_LEN, SEP))
unittest.main(exit=False)
print(' End checking type contracts '.center(TARGET_LEN, SEP))
print('\nScroll up to see ALL RESULTS:')
print(' - checking coding style')
print(' - checking type contract\n')