|
| 1 | +from unittest.mock import patch |
| 2 | + |
| 3 | +import pandas as pd |
| 4 | +import pytest |
| 5 | + |
| 6 | +from virtualship.expedition.instrument_type import InstrumentType |
| 7 | +from virtualship.expedition.schedule import Schedule |
| 8 | +from virtualship.utils import mfp_to_yaml |
| 9 | + |
| 10 | +# Sample correct MFP data |
| 11 | +VALID_MFP_DATA = pd.DataFrame( |
| 12 | + { |
| 13 | + "Station Type": ["A", "B", "C"], |
| 14 | + "Name": ["Station1", "Station2", "Station3"], |
| 15 | + "Latitude": [30, 31, 32], |
| 16 | + "Longitude": [-44, -45, -46], |
| 17 | + "Instrument": ["CTD, DRIFTER", "ARGO_FLOAT", "XBT, CTD, DRIFTER"], |
| 18 | + } |
| 19 | +) |
| 20 | + |
| 21 | +# Missing required columns |
| 22 | +MISSING_HEADERS_DATA = pd.DataFrame( |
| 23 | + {"Station Type": ["A"], "Name": ["Station1"], "Latitude": [10.5]} |
| 24 | +) |
| 25 | + |
| 26 | +# Extra unexpected columns |
| 27 | +EXTRA_HEADERS_DATA = VALID_MFP_DATA.copy() |
| 28 | +EXTRA_HEADERS_DATA["Unexpected Column"] = ["Extra1", "Extra2", "Extra3"] |
| 29 | + |
| 30 | + |
| 31 | +@patch("pandas.read_excel", return_value=VALID_MFP_DATA) |
| 32 | +def test_mfp_to_yaml_success(mock_read_excel, tmp_path): |
| 33 | + """Test that mfp_to_yaml correctly processes a valid MFP Excel file.""" |
| 34 | + yaml_output_path = tmp_path / "schedule.yaml" |
| 35 | + |
| 36 | + # Run function (No need to mock open() for YAML, real file is created) |
| 37 | + mfp_to_yaml("mock_file.xlsx", yaml_output_path) |
| 38 | + |
| 39 | + # Ensure the YAML file was written |
| 40 | + assert yaml_output_path.exists() |
| 41 | + |
| 42 | + # Load YAML and validate contents |
| 43 | + data = Schedule.from_yaml(yaml_output_path) |
| 44 | + |
| 45 | + assert len(data.waypoints) == 3 |
| 46 | + assert data.waypoints[0].instrument == [InstrumentType.CTD, InstrumentType.DRIFTER] |
| 47 | + assert data.waypoints[1].instrument == [InstrumentType.ARGO_FLOAT] |
| 48 | + assert data.waypoints[2].instrument == [ |
| 49 | + InstrumentType.XBT, |
| 50 | + InstrumentType.CTD, |
| 51 | + InstrumentType.DRIFTER, |
| 52 | + ] |
| 53 | + |
| 54 | + |
| 55 | +@patch("pandas.read_excel", return_value=MISSING_HEADERS_DATA) |
| 56 | +def test_mfp_to_yaml_missing_headers(mock_read_excel, tmp_path): |
| 57 | + """Test that mfp_to_yaml raises an error when required columns are missing.""" |
| 58 | + yaml_output_path = tmp_path / "schedule.yaml" |
| 59 | + |
| 60 | + with pytest.raises( |
| 61 | + ValueError, match="Error: Found columns .* but expected columns .*" |
| 62 | + ): |
| 63 | + mfp_to_yaml("mock_file.xlsx", yaml_output_path) |
| 64 | + |
| 65 | + |
| 66 | +@patch("pandas.read_excel", return_value=EXTRA_HEADERS_DATA) |
| 67 | +@patch("builtins.print") # Capture printed warnings |
| 68 | +def test_mfp_to_yaml_extra_headers(mock_print, mock_read_excel, tmp_path): |
| 69 | + """Test that mfp_to_yaml prints a warning when extra columns are found.""" |
| 70 | + yaml_output_path = tmp_path / "schedule.yaml" |
| 71 | + |
| 72 | + # Run function |
| 73 | + mfp_to_yaml("mock_file.xlsx", yaml_output_path) |
| 74 | + |
| 75 | + # Ensure a warning message was printed |
| 76 | + mock_print.assert_any_call( |
| 77 | + "Warning: Found additional unexpected columns ['Unexpected Column']. " |
| 78 | + "Manually added columns have no effect. " |
| 79 | + "If the MFP export format changed, please submit an issue: " |
| 80 | + "https://github.com/OceanParcels/virtualship/issues." |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +@patch("pandas.read_excel", return_value=VALID_MFP_DATA) |
| 85 | +def test_mfp_to_yaml_instrument_conversion(mock_read_excel, tmp_path): |
| 86 | + """Test that instruments are correctly converted into InstrumentType enums.""" |
| 87 | + yaml_output_path = tmp_path / "schedule.yaml" |
| 88 | + |
| 89 | + # Run function |
| 90 | + mfp_to_yaml("mock_file.xlsx", yaml_output_path) |
| 91 | + |
| 92 | + # Load the generated YAML |
| 93 | + data = Schedule.from_yaml(yaml_output_path) |
| 94 | + |
| 95 | + assert isinstance(data.waypoints[0].instrument, list) |
| 96 | + assert data.waypoints[0].instrument == [InstrumentType.CTD, InstrumentType.DRIFTER] |
| 97 | + assert data.waypoints[1].instrument == [InstrumentType.ARGO_FLOAT] |
| 98 | + assert data.waypoints[2].instrument == [ |
| 99 | + InstrumentType.XBT, |
| 100 | + InstrumentType.CTD, |
| 101 | + InstrumentType.DRIFTER, |
| 102 | + ] |
0 commit comments