-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtesting.py
274 lines (214 loc) · 10.3 KB
/
testing.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
import os
import unittest
from unittest.mock import patch
import random
import shutil
import settings
from settings import get_full_path, get_full_file_path
from utils import (
check_ids_correspond,
get_files_and_ids,
get_previous_ids,
update_previous_id_database,
)
import utils
unittest.TestLoader.sortTestMethodsUsing = None
settings.terminate_if_batch_num_repeated = False
def clear_folder(folder_path):
if os.listdir(folder_path):
# https://stackoverflow.com/questions/185936/how-to-delete-the-contents-of-a-folder
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print("Failed to delete %s. Reason: %s" % (file_path, e))
class TestUpdateDatabase(unittest.TestCase):
def setUp(self) -> None:
self.output_folder = get_full_path(os.path.join(".", "test_update_database"))
self.output_file = get_full_file_path(
self.output_folder,
settings.database_of_extracted_pdfs,
)
return super().setUp()
@patch("utils.get_batch_continue_input", return_value="yes")
def test_1_new_database(self, mock_input):
clear_folder(self.output_folder)
new_ids = [random.randint(1400000000, 1500000000) for _ in range(10)]
self.assertFalse(os.path.exists(self.output_file))
update_previous_id_database(self.output_file, new_ids)
self.assertTrue(os.path.exists(self.output_file))
previous_ids = get_previous_ids(self.output_file)
self.assertEqual(set(previous_ids), set(new_ids))
@patch("utils.get_batch_continue_input", return_value="yes")
def test_2_append_database(self, mock_input):
self.assertTrue(os.path.exists(self.output_file))
new_ids = [random.randint(1400000000, 1500000000) for _ in range(10)]
previous_ids = get_previous_ids(self.output_file)
settings.batch_number += 1
update_previous_id_database(self.output_file, new_ids)
ids_from_database = get_previous_ids(self.output_file)
previous_ids.extend(new_ids)
self.assertEqual(set(ids_from_database), set(previous_ids))
@patch("utils.get_batch_continue_input", return_value="no")
def test_3_dont_continue(self, mock_input):
self.assertRaises(Exception, utils.get_batch_continue_input())
class TestIDCorrespondence(unittest.TestCase):
def test_banner_target_no_database_not_cumulative(self):
# suceeds
# Should have same behaviour as if banner is not target
path_to_files = get_full_path(
os.path.join(".", "test_no_database_not_cumulative")
)
settings.is_id_file_banner = True
settings.is_banner_cumulative = False
settings.path_to_pdfs_to_extract = path_to_files
settings.path_to_target_file = get_full_file_path(
path_to_files, settings.target_ucas_id_file
)
settings.path_to_database_of_extracted_pdfs = get_full_file_path(
path_to_files, settings.database_of_extracted_pdfs
)
_, applicant_ids = get_files_and_ids(path_to_files)
correct_ids = {1462950865, 1461856964, 1483858362}
solution = check_ids_correspond(applicant_ids)
self.assertSetEqual(correct_ids, set(solution))
def test_banner_target_no_database_is_cumulative(self):
# Fails if batch number != 1
# Suceeds if batch number == 1
# No database but cumulative => Repetition will be present therefore fails
path_to_files = get_full_path(os.path.join(".", "test_no_database_cumulative"))
settings.is_id_file_banner = True
settings.is_banner_cumulative = True
settings.path_to_pdfs_to_extract = path_to_files
settings.path_to_target_file = get_full_file_path(
path_to_files, settings.target_ucas_id_file
)
settings.path_to_database_of_extracted_pdfs = get_full_file_path(
path_to_files, settings.database_of_extracted_pdfs
)
_, applicant_ids = get_files_and_ids(path_to_files)
correct_ids = {1462950865, 1461856964, 1483858362}
solution = check_ids_correspond(applicant_ids)
self.assertSetEqual(correct_ids, set(solution))
settings.batch_number = 2
with self.assertRaises(Exception) as context:
check_ids_correspond(applicant_ids)
self.assertTrue(
"No database values found but banner is cumulative"
in str(context.exception)
)
@patch("utils.get_batch_continue_input", return_value="yes")
def test_banner_target_with_database_not_cumulative_disjoint(self, mock_input):
# Succeeds
# If database is provided but target file is not cumulative,
# THEN there are three scenarios
# 1. database and target are disjoint => succeed as target is truth
# 2. database intersects target => remove database vals from target
# 3. database superset of target => fails
path_to_files = get_full_path(
os.path.join(".", "test_with_database_not_cumulative_disjoint")
)
settings.is_id_file_banner = True
settings.is_banner_cumulative = False
settings.path_to_pdfs_to_extract = path_to_files
settings.path_to_target_file = get_full_file_path(
path_to_files, settings.target_ucas_id_file
)
settings.path_to_database_of_extracted_pdfs = get_full_file_path(
path_to_files, settings.database_of_extracted_pdfs
)
_, applicant_ids = get_files_and_ids(path_to_files)
# These IDs are different from IDs in the target file
database_ids = [1462950865, 1461856964, 1483858362]
if not os.path.exists(settings.path_to_database_of_extracted_pdfs):
update_previous_id_database(
settings.path_to_database_of_extracted_pdfs, database_ids
)
correct_ids = {1491252509, 1491254202, 1493441903}
ids_to_extract = check_ids_correspond(applicant_ids)
self.assertSetEqual(correct_ids, set(ids_to_extract))
@patch("utils.get_batch_continue_input", return_value="yes")
def test_banner_target_with_database_not_cumulative_intersect(self, mock_input):
# Succeeds
# If database is provided but target file is not cumulative,
# THEN there are three scenarios
# 1. database and target are disjoint => succeed as target is truth
# 2. database intersects target => remove database vals from target
# 3. database superset of target => fails
path_to_files = get_full_path(
os.path.join(".", "test_with_database_not_cumulative_intersect")
)
settings.is_id_file_banner = True
settings.is_banner_cumulative = False
settings.path_to_pdfs_to_extract = path_to_files
settings.path_to_target_file = get_full_file_path(
path_to_files, settings.target_ucas_id_file
)
settings.path_to_database_of_extracted_pdfs = get_full_file_path(
path_to_files, settings.database_of_extracted_pdfs
)
_, applicant_ids = get_files_and_ids(path_to_files)
# These IDs are different from IDs in the target file
# EXCEPT for the last one in the list which is in both
database_ids = [1462950865, 1461856964, 1483858362, 1493441903]
if not os.path.exists(settings.path_to_database_of_extracted_pdfs):
update_previous_id_database(
settings.path_to_database_of_extracted_pdfs, database_ids
)
new_ids = check_ids_correspond(applicant_ids)
correct_ids = {1491252509, 1491254202}
self.assertSetEqual(set(new_ids), correct_ids)
@patch("utils.get_batch_continue_input", return_value="yes")
def test_banner_target_database_target_same(self, mock_input):
# Fails - No new IDs
path_to_files = get_full_path(os.path.join(".", "test_database_target_same"))
settings.is_id_file_banner = True
settings.is_banner_cumulative = True
settings.path_to_pdfs_to_extract = path_to_files
settings.path_to_target_file = get_full_file_path(
path_to_files, settings.target_ucas_id_file
)
settings.path_to_database_of_extracted_pdfs = get_full_file_path(
path_to_files, settings.database_of_extracted_pdfs
)
_, applicant_ids = get_files_and_ids(path_to_files)
# These IDs are identical to IDs in the target file
database_ids = [1491252509, 1491254202, 1493441903]
if not os.path.exists(settings.path_to_database_of_extracted_pdfs):
update_previous_id_database(
settings.path_to_database_of_extracted_pdfs, database_ids
)
with self.assertRaises(Exception) as context:
check_ids_correspond(applicant_ids)
self.assertTrue("No new IDs" in str(context.exception))
def test_banner_target_with_database_is_cumulative(self):
# Succeeds - Typical case
path_to_files = get_full_path(
os.path.join(".", "test_with_database_cumulative")
)
settings.is_id_file_banner = True
settings.is_banner_cumulative = True
settings.path_to_pdfs_to_extract = path_to_files
settings.path_to_target_file = get_full_file_path(
path_to_files, settings.target_ucas_id_file
)
settings.path_to_database_of_extracted_pdfs = get_full_file_path(
path_to_files, settings.database_of_extracted_pdfs
)
_, applicant_ids = get_files_and_ids(path_to_files)
# These IDs are a subset of IDs in the target file
database_ids = [1462950865, 1461856964, 1483858362]
if not os.path.exists(settings.path_to_database_of_extracted_pdfs):
update_previous_id_database(
settings.path_to_database_of_extracted_pdfs, database_ids
)
settings.batch_number += 1
correct_ids = {1491252509, 1491254202, 1493441903}
ids_to_extract = check_ids_correspond(applicant_ids)
self.assertSetEqual(correct_ids, set(ids_to_extract))
if __name__ == "__main__":
unittest.main()