Skip to content

Commit 55b1561

Browse files
authored
Merge pull request #30 from pythonhealthdatascience:dev
Dev
2 parents 36bb72f + 1c0b243 commit 55b1561

14 files changed

Lines changed: 127 additions & 59 deletions

environment.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ channels:
33
- conda-forge
44
dependencies:
55
- flake8=7.3.0
6+
- ipython=9.10.0
7+
- itables=2.7.0
68
- lintquarto=0.7.0
79
- numpy=2.4.1
810
- pandas=2.3.3

examples/python_package/tests/test_data_mock.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from waitingtimes.patient_analysis import import_patient_data
88

9+
910
def test_mocking(monkeypatch):
1011
"""Providing data to a test via mocking"""
1112

examples/python_package/tests/test_data_real.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from waitingtimes.patient_analysis import import_patient_data
88

9+
910
def test_real_data_file():
1011
"""Importing a real data file to a test"""
1112

examples/python_package/tests/test_data_temp.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from waitingtimes.patient_analysis import import_patient_data
88

9+
910
def test_temporary_file(tmp_path):
1011
"""Providing data to a test via a temporary file"""
1112

examples/python_package/tests/test_unit.py

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,29 @@
88
from waitingtimes.patient_analysis import import_patient_data
99

1010

11-
def test_import_success(tmp_path):
11+
def test_import_success(monkeypatch):
1212
"""Small CSV with correct columns should work."""
1313

14+
# Create sample patient data
1415
expected_cols = [
1516
"PATIENT_ID", "ARRIVAL_DATE", "ARRIVAL_TIME",
1617
"SERVICE_DATE", "SERVICE_TIME",
1718
]
18-
19-
# Create temporary CSV file
20-
df_in = pd.DataFrame(
19+
testdata = pd.DataFrame(
2120
[["p1", "2024-01-01", "08:00", "2024-01-01", "09:00"]],
2221
columns=expected_cols,
2322
)
24-
csv_path = tmp_path / "patients.csv"
25-
df_in.to_csv(csv_path, index=False)
2623

27-
# Run function and check it looks correct
28-
result = import_patient_data(csv_path)
24+
# Call function (with mocking for pd.read_csv())
25+
def mock_read_csv(*args, **kwargs):
26+
return testdata
27+
monkeypatch.setattr(pd, "read_csv", mock_read_csv)
28+
result = import_patient_data("path.csv")
29+
30+
# Check the result looks correct
2931
assert isinstance(result, pd.DataFrame)
3032
assert list(result.columns) == expected_cols
31-
pd.testing.assert_frame_equal(result, df_in)
33+
pd.testing.assert_frame_equal(result, testdata)
3234

3335

3436
@pytest.mark.parametrize(
@@ -50,41 +52,44 @@ def test_import_success(tmp_path):
5052
],
5153
],
5254
)
53-
def test_import_errors(tmp_path, columns):
55+
def test_import_errors(monkeypatch, columns):
5456
"""Incorrect columns should trigger ValueError."""
5557

56-
# Create temporary CSV file
57-
df_in = pd.DataFrame([range(len(columns))], columns=columns)
58-
csv_path = tmp_path / "patients.csv"
59-
df_in.to_csv(csv_path, index=False)
58+
# Create sample patient data
59+
testdata = pd.DataFrame([range(len(columns))], columns=columns)
6060

61-
# Check it raises ValueError
61+
# Call function (with mocking for pd.read_csv()), should raise an error
62+
def mock_read_csv(*args, **kwargs):
63+
return testdata
64+
monkeypatch.setattr(pd, "read_csv", mock_read_csv)
6265
with pytest.raises(ValueError):
63-
import_patient_data(csv_path)
66+
import_patient_data("path.csv")
6467

6568

66-
def test_import_empty_csv(tmp_path):
69+
def test_import_empty_csv(monkeypatch):
6770
"""Empty CSV with correct columns should succeed."""
6871

72+
# Create empty CSV with correct header
6973
expected_cols = [
7074
"PATIENT_ID", "ARRIVAL_DATE", "ARRIVAL_TIME",
7175
"SERVICE_DATE", "SERVICE_TIME",
7276
]
77+
testdata = pd.DataFrame(columns=expected_cols)
7378

74-
# Create empty CSV with correct header
75-
df_in = pd.DataFrame(columns=expected_cols)
76-
csv_path = tmp_path / "patients.csv"
77-
df_in.to_csv(csv_path, index=False)
79+
# Call function (with mocking for pd.read_csv())
80+
def mock_read_csv(*args, **kwargs):
81+
return testdata
82+
monkeypatch.setattr(pd, "read_csv", mock_read_csv)
83+
result = import_patient_data("path.csv")
7884

79-
# Should succeed and return empty DataFrame
80-
result = import_patient_data(csv_path)
85+
# Should succeed and return an empty dataframe
8186
assert len(result) == 0
8287
assert list(result.columns) == expected_cols
8388

8489

8590
def test_import_path_types(tmp_path):
8691
"""str and Path inputs should behave identically."""
87-
# Create temporary CSV file
92+
# Create sample patient data
8893
expected_cols = [
8994
"PATIENT_ID",
9095
"ARRIVAL_DATE", "ARRIVAL_TIME",
@@ -94,6 +99,9 @@ def test_import_path_types(tmp_path):
9499
[["p1", "2024-01-01", "08:00", "2024-01-01", "09:00"]],
95100
columns=expected_cols,
96101
)
102+
103+
# Create temporary file (not mocking, as this is about checking
104+
# pd.read_csv is working as expected)
97105
csv_path = tmp_path / "patients.csv"
98106
df_in.to_csv(csv_path, index=False)
99107

pages/case_study.qmd

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ In the example we will:
2828
<!-- We don't use code/patient_analysis__imports.py as we have some additional imports used for displaying things nicely -->
2929

3030
```{python}
31+
from IPython.display import HTML
32+
from itables import to_html_datatable
3133
import json
3234
from pathlib import Path
3335
@@ -42,9 +44,14 @@ pd.set_option("display.max_columns", 8)
4244

4345
::: {.r-content}
4446

47+
<!-- We don't use code/patient_analysis__imports.R as we have some additional imports used for displaying things nicely -->
48+
4549
```{r}
46-
#| file: code/patient_analysis__imports.R
4750
#| output: false
51+
library(dplyr)
52+
library(knitr)
53+
library(lubridate)
54+
library(readr)
4855
```
4956

5057
:::
@@ -83,7 +90,7 @@ You can download a copy of this data here:
8390
raw_data = import_patient_data(
8491
"../examples/python_package/data/patient_data.csv"
8592
)
86-
raw_data
93+
HTML(to_html_datatable(raw_data))
8794
```
8895

8996
:::
@@ -96,7 +103,7 @@ raw_data <- import_patient_data(
96103
"..", "examples", "r_package", "inst", "extdata", "patient_data.csv"
97104
)
98105
)
99-
raw_data
106+
kable(raw_data)
100107
```
101108

102109
:::
@@ -127,7 +134,7 @@ We then apply this function to the raw data.
127134

128135
```{python}
129136
processed_data = calculate_wait_times(raw_data)
130-
processed_data
137+
HTML(to_html_datatable(processed_data))
131138
```
132139

133140
:::
@@ -136,7 +143,7 @@ processed_data
136143

137144
```{r}
138145
processed_data <- calculate_wait_times(raw_data)
139-
processed_data
146+
kable(processed_data)
140147
```
141148

142149
:::
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
def test_import_empty_csv(tmp_path):
1+
def test_import_empty_csv(monkeypatch):
22
"""Empty CSV with correct columns should succeed."""
33

4+
# Create empty CSV with correct header
45
expected_cols = [
56
"PATIENT_ID", "ARRIVAL_DATE", "ARRIVAL_TIME",
67
"SERVICE_DATE", "SERVICE_TIME",
78
]
9+
testdata = pd.DataFrame(columns=expected_cols)
810

9-
# Create empty CSV with correct header
10-
df_in = pd.DataFrame(columns=expected_cols)
11-
csv_path = tmp_path / "patients.csv"
12-
df_in.to_csv(csv_path, index=False)
11+
# Call function (with mocking for pd.read_csv())
12+
def mock_read_csv(*args, **kwargs):
13+
return testdata
14+
monkeypatch.setattr(pd, "read_csv", mock_read_csv)
15+
result = import_patient_data("path.csv")
1316

14-
# Should succeed and return empty DataFrame
15-
result = import_patient_data(csv_path)
17+
# Should succeed and return an empty dataframe
1618
assert len(result) == 0
1719
assert list(result.columns) == expected_cols

pages/code/test_unit__test_import_errors.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717
],
1818
],
1919
)
20-
def test_import_errors(tmp_path, columns):
20+
def test_import_errors(monkeypatch, columns):
2121
"""Incorrect columns should trigger ValueError."""
2222

23-
# Create temporary CSV file
24-
df_in = pd.DataFrame([range(len(columns))], columns=columns)
25-
csv_path = tmp_path / "patients.csv"
26-
df_in.to_csv(csv_path, index=False)
23+
# Create sample patient data
24+
testdata = pd.DataFrame([range(len(columns))], columns=columns)
2725

28-
# Check it raises ValueError
26+
# Call function (with mocking for pd.read_csv()), should raise an error
27+
def mock_read_csv(*args, **kwargs):
28+
return testdata
29+
monkeypatch.setattr(pd, "read_csv", mock_read_csv)
2930
with pytest.raises(ValueError):
30-
import_patient_data(csv_path)
31+
import_patient_data("path.csv")

pages/code/test_unit__test_import_path_types.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
def test_import_path_types(tmp_path):
22
"""str and Path inputs should behave identically."""
3-
# Create temporary CSV file
3+
# Create sample patient data
44
expected_cols = [
55
"PATIENT_ID",
66
"ARRIVAL_DATE", "ARRIVAL_TIME",
@@ -10,6 +10,9 @@ def test_import_path_types(tmp_path):
1010
[["p1", "2024-01-01", "08:00", "2024-01-01", "09:00"]],
1111
columns=expected_cols,
1212
)
13+
14+
# Create temporary file (not mocking, as this is about checking
15+
# pd.read_csv is working as expected)
1316
csv_path = tmp_path / "patients.csv"
1417
df_in.to_csv(csv_path, index=False)
1518

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
def test_import_success(tmp_path):
1+
def test_import_success(monkeypatch):
22
"""Small CSV with correct columns should work."""
33

4+
# Create sample patient data
45
expected_cols = [
56
"PATIENT_ID", "ARRIVAL_DATE", "ARRIVAL_TIME",
67
"SERVICE_DATE", "SERVICE_TIME",
78
]
8-
9-
# Create temporary CSV file
10-
df_in = pd.DataFrame(
9+
testdata = pd.DataFrame(
1110
[["p1", "2024-01-01", "08:00", "2024-01-01", "09:00"]],
1211
columns=expected_cols,
1312
)
14-
csv_path = tmp_path / "patients.csv"
15-
df_in.to_csv(csv_path, index=False)
1613

17-
# Run function and check it looks correct
18-
result = import_patient_data(csv_path)
14+
# Call function (with mocking for pd.read_csv())
15+
def mock_read_csv(*args, **kwargs):
16+
return testdata
17+
monkeypatch.setattr(pd, "read_csv", mock_read_csv)
18+
result = import_patient_data("path.csv")
19+
20+
# Check the result looks correct
1921
assert isinstance(result, pd.DataFrame)
2022
assert list(result.columns) == expected_cols
21-
pd.testing.assert_frame_equal(result, df_in)
23+
pd.testing.assert_frame_equal(result, testdata)

0 commit comments

Comments
 (0)