Skip to content

Commit 147b929

Browse files
authored
Merge pull request #177 from KelvinLinBU/unit-test-tempdirs
Fix unit test to avoid opening of already opened files
2 parents 1c8788d + 3640866 commit 147b929

File tree

4 files changed

+68
-69
lines changed

4 files changed

+68
-69
lines changed

process_report/tests/base.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import tempfile
2+
import shutil
3+
from pathlib import Path
4+
from unittest import TestCase
5+
6+
7+
class BaseTestCaseWithTempDir(TestCase):
8+
def setUp(self):
9+
self.tempdir = Path(tempfile.TemporaryDirectory(delete=False).name)
10+
11+
def tearDown(self):
12+
shutil.rmtree(self.tempdir)

process_report/tests/unit/processors/test_new_pi_credit_processor.py

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from unittest import TestCase, mock
2-
import tempfile
32
import pandas
4-
import os
53

64
from process_report.institute_list_models import InstituteList
75
from process_report.tests import util as test_utils
6+
from process_report.tests.base import BaseTestCaseWithTempDir
87

98

109
class TestNERCRates(TestCase):
@@ -56,7 +55,7 @@ def test_flag_limit_new_pi_credit(self, mock_load_institute_list):
5655
self.assertTrue(output_df.equals(answer_df))
5756

5857

59-
class TestNewPICreditProcessor(TestCase):
58+
class TestNewPICreditProcessor(BaseTestCaseWithTempDir):
6059
def _assert_result_invoice_and_old_pi_file(
6160
self,
6261
invoice_month,
@@ -106,18 +105,11 @@ def _get_test_invoice(
106105
}
107106
)
108107

109-
def setUp(self) -> None:
110-
self.test_old_pi_file = tempfile.NamedTemporaryFile(
111-
delete=False, mode="w+", suffix=".csv"
112-
)
113-
114-
def tearDown(self) -> None:
115-
os.remove(self.test_old_pi_file.name)
116-
117108
def test_no_new_pi(self):
118109
test_invoice = self._get_test_invoice(
119110
["PI" for _ in range(3)], [100 for _ in range(3)]
120111
)
112+
test_old_pi_file = self.tempdir / "old_pi.csv"
121113

122114
# Other fields of old PI file not accessed if PI is no longer
123115
# eligible for new-PI credit
@@ -128,7 +120,7 @@ def test_no_new_pi(self):
128120
"Initial Credits": [1000],
129121
}
130122
)
131-
test_old_pi_df.to_csv(self.test_old_pi_file.name, index=False)
123+
test_old_pi_df.to_csv(test_old_pi_file, index=False)
132124

133125
answer_invoice = pandas.concat(
134126
[
@@ -150,7 +142,7 @@ def test_no_new_pi(self):
150142
self._assert_result_invoice_and_old_pi_file(
151143
"2024-06",
152144
test_invoice,
153-
self.test_old_pi_file.name,
145+
str(test_old_pi_file),
154146
answer_invoice,
155147
answer_old_pi_df,
156148
)
@@ -160,9 +152,8 @@ def test_one_new_pi(self):
160152

161153
# One allocation
162154
invoice_month = "2024-06"
163-
164155
test_invoice = self._get_test_invoice(["PI"], [100])
165-
156+
test_old_pi_file = self.tempdir / "old_pi.csv"
166157
test_old_pi_df = pandas.DataFrame(
167158
columns=[
168159
"PI",
@@ -172,7 +163,7 @@ def test_one_new_pi(self):
172163
"2nd Month Used",
173164
]
174165
)
175-
test_old_pi_df.to_csv(self.test_old_pi_file.name, index=False)
166+
test_old_pi_df.to_csv(test_old_pi_file, index=False)
176167

177168
answer_invoice = pandas.concat(
178169
[
@@ -202,7 +193,7 @@ def test_one_new_pi(self):
202193
self._assert_result_invoice_and_old_pi_file(
203194
invoice_month,
204195
test_invoice,
205-
self.test_old_pi_file.name,
196+
str(test_old_pi_file),
206197
answer_invoice,
207198
answer_old_pi_df,
208199
)
@@ -238,7 +229,7 @@ def test_one_new_pi(self):
238229
self._assert_result_invoice_and_old_pi_file(
239230
invoice_month,
240231
test_invoice,
241-
self.test_old_pi_file.name,
232+
str(test_old_pi_file),
242233
answer_invoice,
243234
answer_old_pi_df,
244235
)
@@ -274,7 +265,7 @@ def test_one_new_pi(self):
274265
self._assert_result_invoice_and_old_pi_file(
275266
invoice_month,
276267
test_invoice,
277-
self.test_old_pi_file.name,
268+
str(test_old_pi_file),
278269
answer_invoice,
279270
answer_old_pi_df,
280271
)
@@ -285,7 +276,7 @@ def test_one_month_pi(self):
285276
# Remaining credits completely covers costs
286277
invoice_month = "2024-07"
287278
test_invoice = self._get_test_invoice(["PI"], [200])
288-
279+
test_old_pi_file = self.tempdir / "old_pi.csv"
289280
test_old_pi_df = pandas.DataFrame(
290281
{
291282
"PI": ["PI"],
@@ -295,7 +286,7 @@ def test_one_month_pi(self):
295286
"2nd Month Used": [0],
296287
}
297288
)
298-
test_old_pi_df.to_csv(self.test_old_pi_file.name, index=False)
289+
test_old_pi_df.to_csv(test_old_pi_file, index=False)
299290

300291
answer_invoice = pandas.concat(
301292
[
@@ -325,7 +316,7 @@ def test_one_month_pi(self):
325316
self._assert_result_invoice_and_old_pi_file(
326317
invoice_month,
327318
test_invoice,
328-
self.test_old_pi_file.name,
319+
str(test_old_pi_file),
329320
answer_invoice,
330321
answer_old_pi_df,
331322
)
@@ -361,7 +352,7 @@ def test_one_month_pi(self):
361352
self._assert_result_invoice_and_old_pi_file(
362353
invoice_month,
363354
test_invoice,
364-
self.test_old_pi_file.name,
355+
str(test_old_pi_file),
365356
answer_invoice,
366357
answer_old_pi_df,
367358
)
@@ -372,7 +363,7 @@ def test_two_new_pi(self):
372363
# Costs partially and completely covered
373364
invoice_month = "2024-07"
374365
test_invoice = self._get_test_invoice(["PI1", "PI1", "PI2"], [800, 500, 500])
375-
366+
test_old_pi_file = self.tempdir / "old_pi.csv"
376367
test_old_pi_df = pandas.DataFrame(
377368
{
378369
"PI": ["PI1"],
@@ -382,7 +373,7 @@ def test_two_new_pi(self):
382373
"2nd Month Used": [0],
383374
}
384375
)
385-
test_old_pi_df.to_csv(self.test_old_pi_file.name, index=False)
376+
test_old_pi_df.to_csv(test_old_pi_file, index=False)
386377

387378
answer_invoice = pandas.concat(
388379
[
@@ -412,7 +403,7 @@ def test_two_new_pi(self):
412403
self._assert_result_invoice_and_old_pi_file(
413404
invoice_month,
414405
test_invoice,
415-
self.test_old_pi_file.name,
406+
str(test_old_pi_file),
416407
answer_invoice,
417408
answer_old_pi_df,
418409
)
@@ -423,6 +414,7 @@ def test_old_pi_file_overwritten(self):
423414

424415
invoice_month = "2024-06"
425416
test_invoice = self._get_test_invoice(["PI", "PI"], [500, 500])
417+
test_old_pi_file = self.tempdir / "old_pi.csv"
426418
test_old_pi_df = pandas.DataFrame(
427419
{
428420
"PI": ["PI"],
@@ -432,7 +424,7 @@ def test_old_pi_file_overwritten(self):
432424
"2nd Month Used": [0],
433425
}
434426
)
435-
test_old_pi_df.to_csv(self.test_old_pi_file.name, index=False)
427+
test_old_pi_df.to_csv(test_old_pi_file, index=False)
436428

437429
answer_invoice = pandas.concat(
438430
[
@@ -462,7 +454,7 @@ def test_old_pi_file_overwritten(self):
462454
self._assert_result_invoice_and_old_pi_file(
463455
invoice_month,
464456
test_invoice,
465-
self.test_old_pi_file.name,
457+
str(test_old_pi_file),
466458
answer_invoice,
467459
answer_old_pi_df,
468460
)
@@ -481,7 +473,7 @@ def test_excluded_su_types(self):
481473
"OpenStack GPUA100SXM4",
482474
],
483475
)
484-
476+
test_old_pi_file = self.tempdir / "old_pi.csv"
485477
test_old_pi_df = pandas.DataFrame(
486478
columns=[
487479
"PI",
@@ -491,7 +483,7 @@ def test_excluded_su_types(self):
491483
"2nd Month Used",
492484
]
493485
)
494-
test_old_pi_df.to_csv(self.test_old_pi_file.name, index=False)
486+
test_old_pi_df.to_csv(test_old_pi_file, index=False)
495487

496488
answer_invoice = pandas.concat(
497489
[
@@ -521,7 +513,7 @@ def test_excluded_su_types(self):
521513
self._assert_result_invoice_and_old_pi_file(
522514
invoice_month,
523515
test_invoice,
524-
self.test_old_pi_file.name,
516+
str(test_old_pi_file),
525517
answer_invoice,
526518
answer_old_pi_df,
527519
)

0 commit comments

Comments
 (0)