-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·153 lines (127 loc) · 5.65 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
#!/usr/bin/env python
import csv
import json
import os
from typing import Generator
import unittest
from bs4 import BeautifulSoup
import pandas as pd
import six
import stgithub
class TestGitHub(unittest.TestCase):
def setUp(self):
self.fixtures_dir = 'fixtures'
self.scraper = stgithub.Scraper()
self.repo_slug = 'pandas-dev/pandas'
self.user = 'user2589'
def _test_datestring(self, ds, month=False):
# month = False: test for %Y-%m-%d
# month = True: test for %Y-%m
self.assertIsInstance(ds, str)
self.assertEqual(len(ds), 7 if month else 10)
self.assertTrue(ds[4] == '-')
self.assertTrue(ds[:4].isdigit() and ds[5:7].isdigit())
if not month:
self.assertTrue(ds[7] == '-')
self.assertTrue(ds[8:].isdigit(),
"%s is not a proper date" % ds)
return True
def test_normalize_text(self):
self.assertEqual(
stgithub.normalize_text("\nHello world \t\n!"), 'Hello world !')
def test_extract_repo(self):
self.assertEqual(stgithub.extract_repo("/org/repo"), 'org/repo')
self.assertEqual(
stgithub.extract_repo("/org/repo/blabla?something=foo"), 'org/repo')
def test_parse_record(self):
fixtures_dir = os.path.join(self.fixtures_dir, 'record')
test_filename = os.path.join(fixtures_dir, 'record_test.csv')
fh = open(test_filename)
for fname, result_json in csv.reader(fh):
expected_result = json.loads(result_json)
if 'null' in expected_result:
# fix json serialization of None
expected_result[None] = expected_result.pop('null')
with open(os.path.join(fixtures_dir, fname), 'rb') as fh:
input_text = fh.read()
tree = BeautifulSoup(input_text, 'html.parser')
result = stgithub._parse_timeline_update_record(tree)
self.assertDictEqual(
expected_result, result,
"Record parsing result is different from expected for %s:\n"
"Expected: %s\n"
"Got: %s" % (fname, expected_result, result)
)
fh.close()
def test_parse_month(self):
fixtures_dir = os.path.join(self.fixtures_dir, 'month')
for fname in os.listdir(fixtures_dir):
if not fname.endswith('.html'):
continue
with open(os.path.join(fixtures_dir, fname)) as fh:
input_text = fh.read()
tree = BeautifulSoup(input_text, 'html.parser')
result = stgithub._parse_timeline_update(tree)
self.assertIsInstance(result, Generator)
month, chunk = next(result)
self.assertIsInstance(month, str)
self.assertIsInstance(chunk, dict)
self._test_datestring(month, True)
def test_project_contributor_stats(self):
stats = self.scraper.project_contributor_stats(self.repo_slug)
self.assertIsInstance(stats, list)
self.assertGreaterEqual(len(stats), 0)
self.assertIsInstance(stats[0], dict)
self.assertTrue(
all(field in stats[0] for field in ('author', 'total', 'weeks')))
self.assertIsInstance(stats[0]['total'], int)
def test_user_daily_contrib_num(self):
contribs = self.scraper.user_daily_contrib_num('user2589', 2018)
self.assertIsInstance(contribs, dict)
self.assertEqual(len(contribs), 365)
self.assertTrue(all(self._test_datestring(k) for k in contribs.keys()))
self.assertTrue(all(isinstance(v, int) for v in contribs.values()))
self.assertTrue(all(v >= 0 for v in contribs.values()))
def test_extract_activity_feed_links(self):
fpath = os.path.join(self.fixtures_dir, 'activity_feed', 'chunk.html')
fh = open(fpath, 'rb')
chunk_text = fh.read()
fh.close()
gen = stgithub._extract_activity_feed_links(chunk_text)
self.assertIsInstance(gen, Generator)
date, href = next(gen)
self.assertEqual(date, '2019-01-09')
self.assertEqual(href, '/CMUSTRUDEL/strudel.ghutils/tree/master')
def test_links_to_recent_user_activity(self):
gen = self.scraper.links_to_recent_user_activity(self.user)
self.assertIsInstance(gen, Generator)
results = list(gen)
self.assertGreater(len(results), 50)
first_res = results[0]
self.assertIsInstance(first_res, tuple)
self.assertEqual(len(first_res), 2)
self._test_datestring(first_res[0])
self.assertIsInstance(first_res[1], six.string_types)
def test_full_user_activity_timeline(self):
gen = self.scraper.full_user_activity_timeline(self.user)
self.assertIsInstance(gen, Generator)
# postpone test on full timeline until the end
results = pd.DataFrame(
self.scraper.full_user_activity_timeline(
self.user, '2017-06', '2017-06-30')
).set_index(['month', 'repo'])
self.assertSetEqual({'commits', 'issues'}, set(results.columns))
self.assertSetEqual(
{'user2589/ghd', 'user2589/csi-project'}, set(results.index.levels[1]))
results = pd.DataFrame(
self.scraper.full_user_activity_timeline(
self.user, '2017-07', '2017-07-31')
).set_index(['month', 'repo'])
self.assertSetEqual(
{'commits', 'created_repository'}, set(results.columns))
self.assertSetEqual({'user2589/ghd'}, set(results.index.levels[1]))
# get the full timeline
results = list(gen)
self.assertGreater(len(results), 50)
if __name__ == "__main__":
unittest.main()