Skip to content

Commit a9cfb60

Browse files
committed
extract fixtures into conftest; add docstrings
1 parent 24d1347 commit a9cfb60

File tree

2 files changed

+60
-47
lines changed

2 files changed

+60
-47
lines changed

tests/conftest.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,22 @@ def pytest_configure(config):
1515

1616
@pytest.fixture(scope="module")
1717
def temp_dir():
18+
"""Creates a temporary directory for test files."""
1819
with tempfile.TemporaryDirectory() as tmpdir:
1920
yield tmpdir
2021

2122

2223
@pytest.fixture(scope="module")
2324
def groundtruth_dir(temp_dir):
25+
"""Creates a temporary groundtruth directory."""
2426
gt_dir = os.path.join(temp_dir, "groundtruth")
2527
os.makedirs(gt_dir)
2628
return gt_dir
2729

2830

2931
@pytest.fixture(scope="module")
3032
def gt_file(groundtruth_dir):
33+
"""Creates a dummy groundtruth file."""
3134
truth = pd.DataFrame(
3235
{
3336
"id": ["A_01", "A_02", "A_03"],
@@ -41,6 +44,7 @@ def gt_file(groundtruth_dir):
4144

4245
@pytest.fixture(scope="module")
4346
def pred_file(temp_dir):
47+
"""Creates a dummy prediction file."""
4448
pred = pd.DataFrame(
4549
{
4650
"id": ["A_01", "A_02", "A_03"],
@@ -54,17 +58,21 @@ def pred_file(temp_dir):
5458

5559
@pytest.fixture(scope="module")
5660
def valid_predictions_json():
57-
return json.dumps({
58-
"validation_status": "VALIDATED",
59-
"validation_errors": "",
60-
})
61+
"""Creates a dummy valid results JSON."""
62+
return json.dumps(
63+
{
64+
"validation_status": "VALIDATED",
65+
"validation_errors": "",
66+
}
67+
)
6168

6269

6370
@pytest.fixture(scope="module")
6471
def invalid_predictions_json():
65-
return json.dumps({
66-
"validation_status": "INVALID",
67-
"validation_errors": "Something went wrong.",
68-
})
69-
70-
72+
"""Creates a dummy invalid results JSON."""
73+
return json.dumps(
74+
{
75+
"validation_status": "INVALID",
76+
"validation_errors": "Something went wrong.",
77+
}
78+
)

tests/test_validate.py

Lines changed: 42 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
runner = CliRunner()
2020

2121

22+
# ----- Tests for validate() function -----
2223
def test_validate_valid_task_number(gt_file, pred_file):
23-
"""Check that the return type for validate() is a list, filter, or tuple."""
24+
"""
25+
Test: validate() returns a list, filter, or tuple for valid
26+
task number.
27+
"""
2428
task_number = 1
2529
errors = validate(
2630
task_number=task_number,
@@ -31,7 +35,7 @@ def test_validate_valid_task_number(gt_file, pred_file):
3135

3236

3337
def test_validate_invalid_task_number():
34-
"""Check that error message about invalid task number is returned in the list of error messages."""
38+
"""Test: validate() notifies about invalid task number."""
3539
task_number = 99999
3640
errors = validate(
3741
task_number=task_number,
@@ -41,42 +45,13 @@ def test_validate_invalid_task_number():
4145
assert f"Invalid challenge task number specified: `{task_number}`" in errors
4246

4347

44-
@patch("validate.extract_gt_file")
45-
@patch("validate.validate")
46-
def test_main_invalid_submission_type(
47-
mock_validate, mock_extract_gt_file, groundtruth_dir, temp_dir
48-
):
49-
invalid_file = os.path.join(temp_dir, "INVALID_predictions.txt")
50-
with open(invalid_file, "w") as f:
51-
f.write("foo")
52-
output_file = os.path.join(temp_dir, "results.json")
53-
result = runner.invoke(
54-
app,
55-
[
56-
"-p",
57-
invalid_file,
58-
"-g",
59-
groundtruth_dir,
60-
"-t",
61-
"1",
62-
"-o",
63-
output_file,
64-
],
65-
)
66-
assert result.exit_code == 0
67-
assert result.stdout.strip() == "INVALID"
68-
with open(output_file, "r") as f:
69-
output_data = json.load(f)
70-
assert output_data["validation_status"] == "INVALID"
71-
mock_extract_gt_file.assert_not_called()
72-
mock_validate.assert_not_called()
73-
74-
48+
# ----- Tests for main() function -----
7549
@patch("validate.extract_gt_file")
7650
@patch("validate.validate")
7751
def test_main_valid_submission_type(
7852
mock_validate, mock_extract_gt_file, gt_file, pred_file, temp_dir
7953
):
54+
"""Test: final results should be INVALID or VALIDATED."""
8055
mock_extract_gt_file.return_value = gt_file
8156
mock_validate.return_value = []
8257
groundtruth_dir = os.path.dirname(gt_file)
@@ -111,26 +86,56 @@ def test_main_valid_submission_type(
11186
assert output_data["validation_status"] == "INVALID"
11287
assert not output_data["validation_errors"]
11388

114-
print(mock_extract_gt_file.mock_calls)
115-
print(mock_validate.mock_calls)
116-
11789
mock_extract_gt_file.assert_called_once_with(groundtruth_dir)
11890
mock_validate.assert_called_once_with(
11991
task_number=1, gt_file=gt_file, pred_file=pred_file
12092
)
12193

12294

95+
@patch("validate.extract_gt_file")
96+
@patch("validate.validate")
97+
def test_main_invalid_submission_type(
98+
mock_validate, mock_extract_gt_file, groundtruth_dir, temp_dir
99+
):
100+
"""Test: final results should be INVALID for incorrect submission type."""
101+
invalid_file = os.path.join(temp_dir, "INVALID_predictions.txt")
102+
with open(invalid_file, "w") as f:
103+
f.write("foo")
104+
output_file = os.path.join(temp_dir, "results.json")
105+
result = runner.invoke(
106+
app,
107+
[
108+
"-p",
109+
invalid_file,
110+
"-g",
111+
groundtruth_dir,
112+
"-t",
113+
"1",
114+
"-o",
115+
output_file,
116+
],
117+
)
118+
assert result.exit_code == 0
119+
assert result.stdout.strip() == "INVALID"
120+
with open(output_file, "r") as f:
121+
output_data = json.load(f)
122+
assert output_data["validation_status"] == "INVALID"
123+
mock_extract_gt_file.assert_not_called()
124+
mock_validate.assert_not_called()
125+
126+
123127
@patch("validate.extract_gt_file")
124128
@patch("validate.validate")
125129
def test_main_long_error_message(
126130
mock_validate, mock_extract_gt_file, gt_file, pred_file, temp_dir
127131
):
132+
"""Test: validation errors should never exceed 500 characters."""
128133
mock_extract_gt_file.return_value = gt_file
129134

130135
# Create a dummy string longer than 500 characters.
131136
long_error_message = "foo" * 500
132137
mock_validate.return_value = [long_error_message]
133-
138+
134139
groundtruth_dir = os.path.dirname(gt_file)
135140
output_file = os.path.join(temp_dir, "results.json")
136141

0 commit comments

Comments
 (0)