88from 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
8590def 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
0 commit comments