forked from github/issue-metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_config.py
238 lines (213 loc) · 6.63 KB
/
test_config.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
"""A module containing unit tests for the config module functions.
Classes:
TestGetIntFromEnv: A class to test the get_int_env_var function.
TestEnvVars: A class to test the get_env_vars function.
"""
import os
import unittest
from unittest.mock import patch
from config import EnvVars, get_env_vars, get_int_env_var
SEARCH_QUERY = "is:issue is:open repo:user/repo"
TOKEN = "test_token"
class TestGetIntFromEnv(unittest.TestCase):
"""
Test suite for the get_int_from_env function.
...
Test methods:
- test_get_int_env_var: Test returns the expected integer value.
- test_get_int_env_var_with_empty_env_var: Test returns None when environment variable
is empty.
- test_get_int_env_var_with_non_integer: Test returns None when environment variable
is a non-integer.
"""
@patch.dict(os.environ, {"INT_ENV_VAR": "12345"})
def test_get_int_env_var(self):
"""
Test that get_int_env_var returns the expected integer value.
"""
result = get_int_env_var("INT_ENV_VAR")
self.assertEqual(result, 12345)
@patch.dict(os.environ, {"INT_ENV_VAR": ""})
def test_get_int_env_var_with_empty_env_var(self):
"""
This test verifies that the get_int_env_var function returns None
when the environment variable is empty.
"""
result = get_int_env_var("INT_ENV_VAR")
self.assertIsNone(result)
@patch.dict(os.environ, {"INT_ENV_VAR": "not_an_int"})
def test_get_int_env_var_with_non_integer(self):
"""
Test that get_int_env_var returns None when the environment variable is
a non-integer.
"""
result = get_int_env_var("INT_ENV_VAR")
self.assertIsNone(result)
class TestGetEnvVars(unittest.TestCase):
"""
Test suite for the get_env_vars function.
"""
def setUp(self):
env_keys = [
"GH_APP_ID",
"GH_APP_INSTALLATION_ID",
"GH_APP_PRIVATE_KEY",
"GH_TOKEN",
"GHE",
"HIDE_AUTHOR",
"HIDE_LABEL_METRICS",
"HIDE_TIME_TO_ANSWER",
"HIDE_TIME_TO_CLOSE",
"HIDE_TIME_TO_FIRST_RESPONSE",
"IGNORE_USERS",
"LABELS_TO_MEASURE",
"SEARCH_QUERY",
]
for key in env_keys:
if key in os.environ:
del os.environ[key]
@patch.dict(
os.environ,
{
"GH_APP_ID": "12345",
"GH_APP_INSTALLATION_ID": "678910",
"GH_APP_PRIVATE_KEY": "hello",
"GH_TOKEN": "",
"GH_ENTERPRISE_URL": "",
"HIDE_AUTHOR": "",
"HIDE_LABEL_METRICS": "",
"HIDE_TIME_TO_ANSWER": "",
"HIDE_TIME_TO_CLOSE": "",
"HIDE_TIME_TO_FIRST_RESPONSE": "",
"IGNORE_USERS": "",
"LABELS_TO_MEASURE": "",
"SEARCH_QUERY": SEARCH_QUERY,
},
clear=True,
)
def test_get_env_vars_with_github_app(self):
"""Test that all environment variables are set correctly using GitHub App"""
expected_result = EnvVars(
12345,
678910,
b"hello",
"",
"",
False,
False,
False,
False,
False,
[],
[],
SEARCH_QUERY,
)
result = get_env_vars(True)
self.assertEqual(str(result), str(expected_result))
@patch.dict(
os.environ,
{
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_ENTERPRISE_URL": "",
"GH_TOKEN": TOKEN,
"HIDE_AUTHOR": "",
"HIDE_LABEL_METRICS": "",
"HIDE_TIME_TO_ANSWER": "",
"HIDE_TIME_TO_CLOSE": "",
"HIDE_TIME_TO_FIRST_RESPONSE": "",
"IGNORE_USERS": "",
"LABELS_TO_MEASURE": "",
"SEARCH_QUERY": SEARCH_QUERY,
},
clear=True,
)
def test_get_env_vars_with_token(self):
"""Test that all environment variables are set correctly using a list of repositories"""
expected_result = EnvVars(
None,
None,
b"",
TOKEN,
"",
False,
False,
False,
False,
False,
[],
[],
SEARCH_QUERY,
)
result = get_env_vars(True)
self.assertEqual(str(result), str(expected_result))
@patch.dict(
os.environ,
{
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"GH_ENTERPRISE_URL": "",
"HIDE_AUTHOR": "true",
"HIDE_LABEL_METRICS": "true",
"HIDE_TIME_TO_ANSWER": "true",
"HIDE_TIME_TO_CLOSE": "true",
"HIDE_TIME_TO_FIRST_RESPONSE": "true",
"IGNORE_USERS": "",
"LABELS_TO_MEASURE": "waiting-for-review,waiting-for-manager",
"SEARCH_QUERY": SEARCH_QUERY,
},
)
def test_get_env_vars_optional_values(self):
"""Test that optional values are set to their default values if not provided"""
expected_result = EnvVars(
None,
None,
b"",
TOKEN,
"",
True,
True,
True,
True,
True,
[],
["waiting-for-review", "waiting-for-manager"],
SEARCH_QUERY,
)
result = get_env_vars(True)
self.assertEqual(str(result), str(expected_result))
@patch.dict(
os.environ,
{
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "",
"SEARCH_QUERY": SEARCH_QUERY,
},
clear=True,
)
def test_get_env_vars_missing_token(self):
"""Test that an error is raised if the TOKEN environment variables is not set"""
with self.assertRaises(ValueError):
get_env_vars(True)
@patch.dict(
os.environ,
{
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": TOKEN,
"SEARCH_QUERY": "",
},
clear=True,
)
def test_get_env_vars_missing_query(self):
"""Test that an error is raised if the SEARCH_QUERY environment variable is not set."""
with self.assertRaises(ValueError):
get_env_vars(True)
if __name__ == "__main__":
unittest.main()