Skip to content

Commit 7dafe94

Browse files
authored
[feat] Add tests (#8)
* write initial tests * update sample data to use strings for `id` column * update type hint * extract fixtures into conftest * add tests for scoring script * extract fixtures into conftest; add docstrings * update python versions to test for * update readme to mention unit tests * fix typo
1 parent 0ac3c03 commit 7dafe94

File tree

9 files changed

+561
-22
lines changed

9 files changed

+561
-22
lines changed

.github/workflows/run-tests.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Run tests
2+
on:
3+
push:
4+
branches: [ "main" ]
5+
pull_request:
6+
branches: [ "main" ]
7+
workflow_dispatch:
8+
9+
10+
jobs:
11+
test:
12+
runs-on: ubuntu-latest
13+
strategy:
14+
matrix:
15+
python-version: ["3.10", "3.11", "3.12"]
16+
permissions:
17+
contents: read
18+
19+
steps:
20+
- name: Checkout repository
21+
uses: actions/checkout@v4
22+
23+
- name: Set up Python ${{ matrix.python-version }}
24+
uses: actions/setup-python@v5
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Install dependencies
29+
run: |
30+
python -m pip install --upgrade pip
31+
pip install -r requirements.txt
32+
33+
- name: Test with pytest
34+
run: |
35+
pip install pytest pytest-cov
36+
pytest --cov=. --cov-report=xml tests/test_validate.py
37+
pytest --cov=. --cov-report=xml tests/test_score.py

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ in Python. R support TBD.
6666
> for each task.
6767
6868
> [!IMPORTANT]
69-
> Modifying the `main()` function is highly discouraged. This function has
70-
> specifically been written to interact with ORCA.
69+
> The `main()` function is specifically designed for seamless interaction with
70+
> ORCA. **Modifying this function is strongly discouraged** and could lead to
71+
> compatibility issues. Unit tests are in place to ensure its proper integration.
7172
7273
3. Update `requirements.txt` with any additional libraries/packages used by the
7374
script.
@@ -117,8 +118,7 @@ in Python. R support TBD.
117118
> for each task.
118119
119120
> [!IMPORTANT]
120-
> Modifying the `main()` function is highly discouraged. This function has
121-
> specifically been written to interact with ORCA.
121+
> Like `validate.py`, modifying `main()` is strongly discouraged.
122122
123123
3. Update `requirements.txt` with any additional libraries/packages used by the script.
124124

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
id,disease
2-
1,1
3-
2,1
4-
3,0
5-
4,1
6-
5,1
2+
A_01,1
3+
A_02,1
4+
A_03,0
5+
A_04,1
6+
A_05,1

sample_data/invalid_pred.csv

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
id,probability,note
2-
1,-0.011,invalid value (bad range)
3-
2,NA,invalid value (null)
4-
3,0.092,
5-
3,0.913,invalid ID (duplicate)
6-
5,0.543,
7-
10,0.290,invalid ID (unkonwn)
2+
A_01,-0.011,invalid value (bad range)
3+
A_02,NA,invalid value (null)
4+
A_03,0.092,
5+
A_03,0.913,invalid ID (duplicate)
6+
A_05,0.543,
7+
A_10,0.290,invalid ID (unkonwn)

sample_data/valid_pred.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
id,probability
2-
1,0.011
3-
2,0.765
4-
3,0.092
5-
4,0.913
6-
5,0.543
2+
A_01,0.011
3+
A_02,0.765
4+
A_03,0.092
5+
A_04,0.913
6+
A_05,0.543

tests/conftest.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import json
2+
import os
3+
import sys
4+
import tempfile
5+
6+
import pandas as pd
7+
import pytest
8+
9+
10+
def pytest_configure(config):
11+
"""Allow test scripts to import scripts from parent folder."""
12+
src_path = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
13+
sys.path.insert(0, src_path)
14+
15+
16+
@pytest.fixture(scope="module")
17+
def temp_dir():
18+
"""Creates a temporary directory for test files."""
19+
with tempfile.TemporaryDirectory() as tmpdir:
20+
yield tmpdir
21+
22+
23+
@pytest.fixture(scope="module")
24+
def groundtruth_dir(temp_dir):
25+
"""Creates a temporary groundtruth directory."""
26+
gt_dir = os.path.join(temp_dir, "groundtruth")
27+
os.makedirs(gt_dir)
28+
return gt_dir
29+
30+
31+
@pytest.fixture(scope="module")
32+
def gt_file(groundtruth_dir):
33+
"""Creates a dummy groundtruth file."""
34+
truth = pd.DataFrame(
35+
{
36+
"id": ["A_01", "A_02", "A_03"],
37+
"disease": [1, 0, 1],
38+
}
39+
)
40+
gt_file = os.path.join(groundtruth_dir, "truth.csv")
41+
truth.to_csv(gt_file, index=False)
42+
return gt_file
43+
44+
45+
@pytest.fixture(scope="module")
46+
def pred_file(temp_dir):
47+
"""Creates a dummy prediction file."""
48+
pred = pd.DataFrame(
49+
{
50+
"id": ["A_01", "A_02", "A_03"],
51+
"probability": [0.5, 0.5, 0.5],
52+
}
53+
)
54+
pred_file = os.path.join(temp_dir, "predictions.csv")
55+
pred.to_csv(pred_file, index=False)
56+
return pred_file
57+
58+
59+
@pytest.fixture(scope="module")
60+
def valid_predictions_json():
61+
"""Creates a dummy valid results JSON."""
62+
return json.dumps(
63+
{
64+
"validation_status": "VALIDATED",
65+
"validation_errors": "",
66+
}
67+
)
68+
69+
70+
@pytest.fixture(scope="module")
71+
def invalid_predictions_json():
72+
"""Creates a dummy invalid results JSON."""
73+
return json.dumps(
74+
{
75+
"validation_status": "INVALID",
76+
"validation_errors": "Something went wrong.",
77+
}
78+
)

0 commit comments

Comments
 (0)