-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
424 lines (343 loc) · 16.1 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
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
from __future__ import unicode_literals, print_function
import numpy as np
import pandas as pd
import requests
import logging
import os
import random
import subprocess
import time
from typing import Generator
import unittest
from stutils import decorators as d
from stutils import email_utils as email
from stutils import mapreduce
from stutils import sysutils
from stutils import versions
def test_dataframe(x, y, *args):
return pd.DataFrame(np.random.rand(x, y) * 10000,
columns=[str(i) for i in range(y)], dtype=int)
def test_series(length, *args):
return test_dataframe(length, 1)['0']
class TestDecorators(unittest.TestCase):
@d.cached_method
def rand(self, length, *args):
return pd.Series(np.random.rand(length))
def test_cached_method(self):
self.assertIs(self.rand(10, 'one'), self.rand(10, 'one'))
self.assertIsNot(self.rand(10, 'one'), self.rand(10, 'two'))
def test_memoize(self):
mtest = d.memoize(test_series)
self.assertIs(mtest(10, 'one'), mtest(10, 'one'))
self.assertIsNot(mtest(10, 'one'), mtest(10, 'two'))
def test_fs_cache(self):
# just in case those are set in environment variables
defaults = {
'cache_dir': d.DEFAULT_PATH,
'expires': 3
}
# no app, no type
cseries = d.fs_cache(**defaults)(test_series)
self.assertIsInstance(cseries(10, 'one'), pd.Series)
self.assertTrue((cseries(10, 'one') == cseries(10, 'one')).all())
self.assertIsNot(cseries(10, 'one'), cseries(10, 'one'))
cdf = d.fs_cache(**defaults)(test_dataframe)
res = cdf(10, 2, 'one')
self.assertIsInstance(res, pd.DataFrame)
self.assertTrue((res == cdf(10, 2, 'one')).all(None))
self.assertIsNot(res, cdf(10, 2, 'one'))
# cache expiration
time.sleep(defaults['expires'])
self.assertFalse((res == cdf(10, 2, 'one')).all(None))
# app
old_cdf = cdf
cdf = d.fs_cache('my_app', **defaults)(test_dataframe)
self.assertIsInstance(cdf(10, 2, 'one'), pd.DataFrame)
self.assertTrue((cdf(10, 2, 'one') == cdf(10, 2, 'one')).all(None))
self.assertFalse((old_cdf(10, 2, 'one') == cdf(10, 2, 'one')).all(None))
self.assertIsNot(cdf(10, 2, 'one'), cdf(10, 2, 'one'))
# type
old2 = cdf
cdf = d.fs_cache(
'my_app', cache_type='ttype', **defaults)(test_dataframe)
self.assertIsInstance(cdf(10, 2, 'one'), pd.DataFrame)
self.assertTrue((cdf(10, 2, 'one') == cdf(10, 2, 'one')).all(None))
self.assertFalse((old_cdf(10, 2, 'one') == cdf(10, 2, 'one')).all(None))
self.assertFalse((old2(10, 2, 'one') == cdf(10, 2, 'one')).all(None))
self.assertIsNot(cdf(10, 2, 'one'), cdf(10, 2, 'one'))
# multilevel index
def test_multiindex_df(*args):
return test_dataframe(10, 4, *args).set_index(['0', '1'])
def test_multiindex_series(*args):
return test_multiindex_df(*args)['2']
cseries = d.fs_cache(idx=2, **defaults)(test_multiindex_series)
self.assertIsInstance(cseries(10, 'one'), pd.Series)
self.assertIsInstance(cseries(10, 'one').index, pd.MultiIndex)
self.assertTrue((cseries(10, 'one') == cseries(10, 'one')).all())
self.assertIsNot(cseries(10, 'one'), cseries(10, 'one'))
cdf = d.fs_cache(idx=2, **defaults)(test_multiindex_df)
self.assertIsInstance(cdf(10, 2, 'one'), pd.DataFrame)
self.assertIsInstance(cdf(10, 2, 'one').index, pd.MultiIndex)
self.assertTrue((cdf(10, 2, 'one') == cdf(10, 2, 'one')).all(None))
self.assertIsNot(cdf(10, 2, 'one'), cdf(10, 2, 'one'))
cdf = d.fs_cache(idx=['0', '1'], **defaults)(test_multiindex_df)
self.assertIsInstance(cdf(10, 2, 'two'), pd.DataFrame)
self.assertIsInstance(cdf(10, 2, 'two').index, pd.MultiIndex)
self.assertTrue((cdf(10, 2, 'two') == cdf(10, 2, 'two')).all(None))
self.assertIsNot(cdf(10, 2, 'two'), cdf(10, 2, 'two'))
def test_cache_iterator(self):
# just in case those are set in environment variables
defaults = {
'cache_dir': d.DEFAULT_PATH,
'expires': 3
}
def test_iterator(*args):
if args and args[0] == 'empty':
return
yield "string"
yield random.randint(0, 10000)
yield ['one', 'tow', 1]
# no app, no type
iter = d.cache_iterator(**defaults)(test_iterator)
res = iter('one')
res_list = list(res)
res2 = iter('one')
res2_list = list(res2)
res3_list = list(iter('two'))
# check result is still a generator
self.assertIsInstance(res, Generator)
self.assertIsInstance(res2, Generator)
self.assertTrue(res_list == res2_list)
self.assertIsNot(res, res2)
self.assertFalse(res_list == res3_list)
self.assertTrue(iter.cached('one'))
self.assertFalse(iter.cached('three'))
time.sleep(defaults['expires'])
self.assertFalse(iter.cached('one'))
self.assertFalse(iter.cached('three'))
res4 = iter('empty')
res4_list = list(res4)
res5_list = list(iter('empty'))
self.assertIsInstance(res4, Generator)
self.assertTrue(res4_list == res5_list)
def test_file_naming(self):
# <cache_dir>/<app_name>.cache/<cache_type>/<func_name>.[<param_string>.]csv
cseries = d.fs_cache('my_app')(test_series)
self.assertTrue(
cseries.get_cache_fname(10).endswith(
'/my_app.cache/test_series.10.csv'))
typed_cseries = d.typed_fs_cache('my_app')('my_ctype')(test_series)
self.assertTrue(
typed_cseries.get_cache_fname(10).endswith(
'/my_app.cache/my_ctype/test_series.10.csv'))
def test_threadpool(self):
n = 1000
@d.threadpool(4) # sleep in four threads
def increment(*args):
time.sleep(0.005)
start_time = time.time()
increment(list(range(n)))
elapsed = time.time() - start_time
self.assertLess(elapsed, 0.005 * n * 0.3)
def test_guard(self):
n = 1000
data = {'counter': 0}
@d.threadpool(4)
@d.guard
def increment(*args):
i = data['counter']
time.sleep(0.005)
data['counter'] = i + 1
increment(list(range(n)))
self.assertEqual(data['counter'], n)
class TestMapReduce(unittest.TestCase):
def setUp(self):
users = ("pandas-dev", "numpy", "django", "requests", "saltstack",
"keras-team", "ansible", "scikit-learn", "conda", "scipy",
"gevent", "tornadoweb", "aio-libs", "lxml", "python-pillow",
"chardet", "pallets", "zopefoundation", "openstack",
"alskdfjalskd", "owietmqwoinvq")
self.urls = ["https://github.com/orgs/%s/people" % user
for user in users]
start = time.time()
self.reference_results = [requests.get(url).status_code
for url in self.urls]
self.baseline_perf = time.time() - start
# logging.basicConfig(level=logging.INFO)
def test_thread_pool(self):
tp = mapreduce.ThreadPool(8)
results_dict = {}
def callback(res):
status, url = res
results_dict[url] = status
def do(url):
return requests.get(url).status_code, url
start = time.time()
_ = [tp.submit(do, url, callback=callback) for url in self.urls]
tp.shutdown()
results = [results_dict[url] for url in self.urls]
perf = time.time() - start
self.assertSequenceEqual(self.reference_results, results)
self.assertGreater(self.baseline_perf/perf, 3.5)
logging.info("Async mapping: %s seconds, sync baseline: %s"
"" % (perf, self.baseline_perf))
def test_native_threadpool(self):
# this test shows native Threadpool is 30..50% slower
from multiprocessing.pool import ThreadPool
def do(url):
return requests.get(url).status_code
pool = ThreadPool()
start = time.time()
results = list(pool.imap(do, self.urls))
perf = time.time() - start
self.assertSequenceEqual(self.reference_results, results)
logging.info("Native ThreadPool mapping: %s seconds, sync baseline: %s"
"" % (perf, self.baseline_perf))
''
class TestEmailUtils(unittest.TestCase):
def test_parse(self):
self.assertEqual(email.parse(
'<[email protected]@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>'),
('me', 'someorg.com'))
self.assertRaises(email.InvalidEmail, lambda: email.parse(42))
self.assertRaises(email.InvalidEmail, lambda: email.parse("boo"))
def test_clean(self):
self.assertEqual(email.clean('[email protected]'), '[email protected]')
self.assertEqual(email.clean('<[email protected]'), '[email protected]')
self.assertEqual(email.clean('[email protected]>'), '[email protected]')
self.assertEqual(
email.clean('John Doe <[email protected]>'), '[email protected]')
self.assertEqual(
email.clean('John Doe [email protected]'), '[email protected]')
# git2svn produces addresses like this:
self.assertEqual(email.clean(
'<[email protected]@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>'),
self.assertEqual(email.clean("invalid email"), None)
def test_domain(self):
self.assertEqual(email.domain(
'Missing [email protected]@ce2b1a6d-e550-0410-aec6-3dcde31c8c00>'),
'dep.uni.edu')
self.assertEqual(email.domain("invalid email"), None)
def test_university_domains(self):
self.assertIsInstance(email.university_domains(), set)
# 4902 as of Jan 2018
self.assertGreater(len(email.university_domains()), 4000)
# .edu domains are excluded
self.assertNotIn('cmu.edu', email.university_domains())
self.assertIn('upsa.es', email.university_domains())
def test_public_domains(self):
self.assertIsInstance(email.public_domains(), set)
# 3628 as of Jan 2018
self.assertGreater(len(email.public_domains()), 3000)
self.assertFalse(
email.public_domains().intersection(email.university_domains()))
self.assertNotIn('google.com', email.public_domains())
self.assertIn('yandex.ru', email.public_domains())
self.assertIn('163.com', email.public_domains())
def test_domain_user_stats(self):
self.assertIsInstance(email.domain_user_stats(), pd.Series)
self.assertGreater(len(email.domain_user_stats()), 100000)
self.assertTrue((email.domain_user_stats() > 0).all())
def test_commercial_domains(self):
self.assertIn("google.com", email.commercial_domains()) # commercial
self.assertNotIn("gmail.com", email.commercial_domains()) # public
self.assertNotIn("jaraco.com", email.commercial_domains()) # personal
self.assertNotIn("cmu.edu", email.commercial_domains()) # university
def test_domain_intersections(self):
self.assertFalse(email.public_domains().intersection(
email.commercial_domains()))
self.assertFalse(email.public_domains().intersection(
email.university_domains()))
self.assertFalse(email.commercial_domains().intersection(
email.university_domains()))
def test_is_university(self):
self.assertTrue(email.is_university("[email protected]"))
self.assertTrue(email.is_university("[email protected]"))
self.assertTrue(email.is_university("[email protected]"))
self.assertFalse(email.is_university("[email protected]"))
self.assertTrue(email.is_university("[email protected]"))
self.assertTrue(email.is_university("[email protected]"))
self.assertFalse(email.is_university("[email protected]"))
self.assertFalse(email.is_university("John Doe"))
# two cool exceptions
self.assertFalse(email.is_university("[email protected]"))
self.assertFalse(email.is_university("[email protected]"))
def test_is_public(self):
self.assertTrue(email.is_public("[email protected]"))
self.assertTrue(email.is_public("[email protected]"))
self.assertFalse(email.is_public("[email protected]"))
self.assertTrue(email.is_public("john@localhost"))
self.assertTrue(email.is_public("John Doe"))
# two cool exceptions
self.assertTrue(email.is_public("[email protected]"))
self.assertTrue(email.is_public("[email protected]"))
# new TLDs might be really long
self.assertFalse(email.is_public("[email protected]"))
def test_is_commercial(self):
self.assertFalse(email.is_commercial("[email protected]")) # public
self.assertFalse(email.is_commercial("john@localhost")) # local
self.assertFalse(email.is_commercial("[email protected]")) # academic
self.assertFalse(email.is_commercial("[email protected]")) # personal
self.assertFalse(email.is_commercial("John Doe"))
self.assertTrue(email.is_commercial("[email protected]"))
self.assertTrue(email.is_commercial("[email protected]"))
self.assertTrue(email.is_commercial("[email protected]"))
# two cool exceptions
self.assertFalse(email.is_commercial("[email protected]"))
self.assertFalse(email.is_commercial("[email protected]"))
def test_bulk_methods(self):
s = email.domain_user_stats().iloc[:300]
es = pd.Series("test@" + s.index, index=s.index)
is_public = email.is_public_bulk(es)
self.assertIsInstance(is_public, pd.Series)
self.assertEqual(len(is_public), len(es))
is_university = email.is_university_bulk(es)
self.assertIsInstance(is_university, pd.Series)
self.assertEqual(len(is_university), len(es))
is_commercial = email.is_commercial_bulk(es)
self.assertIsInstance(is_commercial, pd.Series)
self.assertEqual(len(is_commercial), len(es))
class TestSysutils(unittest.TestCase):
def test_shell(self):
# system command
status, output = sysutils.shell("echo", "Hello world!")
self.assertEqual(status, 0)
self.assertEqual(output.strip(), "Hello world!")
# relative file path
rel_path = os.path.dirname(__file__) or '.'
status, output = sysutils.shell("LICENSE", rel_path=rel_path)
self.assertEqual(status, 0)
self.assertGreater(len(output), 35000)
# invalid commands
stderr = open('/dev/null', 'wb')
with self.assertRaises(OSError):
sysutils.shell("ls", rel_path=".", stderr=stderr)
with self.assertRaises(subprocess.CalledProcessError):
sysutils.shell("ls", "eatmyshorts", stderr=stderr)
status, output = sysutils.shell("eatmyshorts", raise_on_status=False)
self.assertEqual(status, -1)
status, output = sysutils.shell(
"ls", "eatmyshorts", raise_on_status=False, stderr=stderr)
self.assertEqual(status, 2)
stderr.close()
def test_filesize(self):
rel_path = os.path.dirname(__file__) or '.'
self.assertGreater(sysutils.raw_filesize(rel_path), 100000)
self.assertIsNone(sysutils.raw_filesize(rel_path + '/eatmyshorts'))
class TestVersions(unittest.TestCase):
def test_is_alpha(self):
self.assertFalse(versions.is_alpha("1.0.0"))
self.assertTrue(versions.is_alpha("1.0rc1"))
def test_parse(self):
self.assertEqual(versions.parse("1"), [1])
self.assertEqual(versions.parse("0.0.1"), [0, 0, 1])
self.assertEqual(versions.parse("0.11.23.rc1"), [0, 11, 23, 'rc1'])
def test_compare(self):
self.assertEqual(versions.compare("0.1.1", "0.1.2"), -1)
self.assertEqual(versions.compare("0.1.2", "0.1.1"), 1)
self.assertEqual(versions.compare("0.1", "0.1.1"), 0)
self.assertEqual(versions.compare("0.1.1rc1", "0.1.1a"), 1)
self.assertEqual(versions.compare("0.1.1rc1", "0.1.1"), -1)
if __name__ == "__main__":
unittest.main()