forked from github/issue-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_json_writer.py
208 lines (194 loc) · 7.78 KB
/
test_json_writer.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
"""Tests for the write_to_json function in json_writer.py."""
import json
import unittest
from datetime import timedelta
from classes import IssueWithMetrics
from json_writer import write_to_json
class TestWriteToJson(unittest.TestCase):
"""Tests for the write_to_json function."""
# Show differences without omission in assertion
maxDiff = None
def test_write_to_json(self):
"""Test that write_to_json writes the correct JSON file."""
issues_with_metrics = [
IssueWithMetrics(
title="Issue 1",
html_url="https://github.com/owner/repo/issues/1",
author="alice",
time_to_first_response=timedelta(days=3),
time_to_close=timedelta(days=6),
time_to_answer=None,
labels_metrics={
"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)
},
),
IssueWithMetrics(
title="Issue 2",
html_url="https://github.com/owner/repo/issues/2",
author="bob",
time_to_first_response=timedelta(days=2),
time_to_close=timedelta(days=4),
time_to_answer=timedelta(days=1),
labels_metrics={},
),
]
stats_time_to_first_response = {
"avg": timedelta(days=2.5),
"med": timedelta(days=2.5),
"90p": timedelta(days=1.5),
}
stats_time_to_close = {
"avg": timedelta(days=5),
"med": timedelta(days=4),
"90p": timedelta(days=3),
}
stats_time_to_answer = {
"avg": timedelta(days=1),
"med": timedelta(days=2),
"90p": timedelta(days=3),
}
stats_time_in_labels = {
"avg": {"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)},
"med": {"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)},
"90p": {"bug": timedelta(days=1, hours=16, minutes=24, seconds=12)},
}
num_issues_opened = 2
num_issues_closed = 1
expected_output = {
"average_time_to_first_response": "2 days, 12:00:00",
"average_time_to_close": "5 days, 0:00:00",
"average_time_to_answer": "1 day, 0:00:00",
"average_time_in_labels": {"bug": "1 day, 16:24:12"},
"median_time_to_first_response": "2 days, 12:00:00",
"median_time_to_close": "4 days, 0:00:00",
"median_time_to_answer": "2 days, 0:00:00",
"median_time_in_labels": {"bug": "1 day, 16:24:12"},
"90_percentile_time_to_first_response": "1 day, 12:00:00",
"90_percentile_time_to_close": "3 days, 0:00:00",
"90_percentile_time_to_answer": "3 days, 0:00:00",
"90_percentile_time_in_labels": {"bug": "1 day, 16:24:12"},
"num_items_opened": 2,
"num_items_closed": 1,
"total_item_count": 2,
"issues": [
{
"title": "Issue 1",
"html_url": "https://github.com/owner/repo/issues/1",
"author": "alice",
"time_to_first_response": "3 days, 0:00:00",
"time_to_close": "6 days, 0:00:00",
"time_to_answer": "None",
"label_metrics": {"bug": "1 day, 16:24:12"},
},
{
"title": "Issue 2",
"html_url": "https://github.com/owner/repo/issues/2",
"author": "bob",
"time_to_first_response": "2 days, 0:00:00",
"time_to_close": "4 days, 0:00:00",
"time_to_answer": "1 day, 0:00:00",
"label_metrics": {},
},
],
"search_query": "is:issue repo:owner/repo",
}
# Call the function and check the output
self.assertEqual(
write_to_json(
issues_with_metrics=issues_with_metrics,
stats_time_to_first_response=stats_time_to_first_response,
stats_time_to_close=stats_time_to_close,
stats_time_to_answer=stats_time_to_answer,
stats_time_in_labels=stats_time_in_labels,
num_issues_opened=num_issues_opened,
num_issues_closed=num_issues_closed,
search_query="is:issue repo:owner/repo",
),
json.dumps(expected_output),
)
def test_write_to_json_with_no_response(self):
"""Test where there is no answer to a issue."""
issues_with_metrics = [
IssueWithMetrics(
title="Issue 1",
html_url="https://github.com/owner/repo/issues/1",
author="alice",
time_to_first_response=None,
time_to_close=None,
time_to_answer=None,
labels_metrics={},
),
IssueWithMetrics(
title="Issue 2",
html_url="https://github.com/owner/repo/issues/2",
author="bob",
time_to_first_response=None,
time_to_close=None,
time_to_answer=None,
labels_metrics={},
),
]
stats_time_to_first_response = None
stats_time_to_close = None
stats_time_to_answer = None
stats_time_in_labels = {
"avg": {},
"med": {},
"90p": {},
}
num_issues_opened = 2
num_issues_closed = 0
expected_output = {
"average_time_to_first_response": "None",
"average_time_to_close": "None",
"average_time_to_answer": "None",
"average_time_in_labels": {},
"median_time_to_first_response": "None",
"median_time_to_close": "None",
"median_time_to_answer": "None",
"median_time_in_labels": {},
"90_percentile_time_to_first_response": "None",
"90_percentile_time_to_close": "None",
"90_percentile_time_to_answer": "None",
"90_percentile_time_in_labels": {},
"num_items_opened": 2,
"num_items_closed": 0,
"total_item_count": 2,
"issues": [
{
"title": "Issue 1",
"html_url": "https://github.com/owner/repo/issues/1",
"author": "alice",
"time_to_first_response": "None",
"time_to_close": "None",
"time_to_answer": "None",
"label_metrics": {},
},
{
"title": "Issue 2",
"html_url": "https://github.com/owner/repo/issues/2",
"author": "bob",
"time_to_first_response": "None",
"time_to_close": "None",
"time_to_answer": "None",
"label_metrics": {},
},
],
"search_query": "is:issue repo:owner/repo",
}
# Call the function and check the output
self.assertEqual(
write_to_json(
issues_with_metrics=issues_with_metrics,
stats_time_to_first_response=stats_time_to_first_response,
stats_time_to_close=stats_time_to_close,
stats_time_to_answer=stats_time_to_answer,
stats_time_in_labels=stats_time_in_labels,
num_issues_opened=num_issues_opened,
num_issues_closed=num_issues_closed,
search_query="is:issue repo:owner/repo",
),
json.dumps(expected_output),
)
if __name__ == "__main__":
unittest.main()