Skip to content

Commit

Permalink
Update mfp tests to use fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
VeckoTheGecko committed Feb 24, 2025
1 parent f9cec7b commit 682c551
Showing 1 changed file with 46 additions and 33 deletions.
79 changes: 46 additions & 33 deletions tests/test_mfp_to_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,50 @@
from virtualship.expedition.schedule import Schedule
from virtualship.utils import mfp_to_yaml

# Sample correct MFP data
VALID_MFP_DATA = pd.DataFrame(
{
"Station Type": ["A", "B", "C"],
"Name": ["Station1", "Station2", "Station3"],
"Latitude": [30, 31, 32],
"Longitude": [-44, -45, -46],
"Instrument": ["CTD, DRIFTER", "ARGO_FLOAT", "XBT, CTD, DRIFTER"],
}
)

# Missing required columns
MISSING_HEADERS_DATA = pd.DataFrame(
{"Station Type": ["A"], "Name": ["Station1"], "Latitude": [10.5]}
)

# Extra unexpected columns
EXTRA_HEADERS_DATA = VALID_MFP_DATA.copy()
EXTRA_HEADERS_DATA["Unexpected Column"] = ["Extra1", "Extra2", "Extra3"]


@patch("pandas.read_excel", return_value=VALID_MFP_DATA)
def test_mfp_to_yaml_success(mock_read_excel, tmp_path):

def valid_mfp_data():
return pd.DataFrame(
{
"Station Type": ["A", "B", "C"],
"Name": ["Station1", "Station2", "Station3"],
"Latitude": [30, 31, 32],
"Longitude": [-44, -45, -46],
"Instrument": ["CTD, DRIFTER", "ARGO_FLOAT", "XBT, CTD, DRIFTER"],
}
)


@pytest.fixture
def valid_mfp_file(tmp_path):
path = tmp_path / "file.xlsx"
valid_mfp_data().to_excel(path, index=False)
yield path


@pytest.fixture
def missing_columns_mfp_file(tmp_path):
path = tmp_path / "file.xlsx"
valid_mfp_data().drop(columns=["Longitude", "Instrument"]).to_excel(
path, index=False
)
yield path


@pytest.fixture
def unexpected_header_mfp_file(tmp_path):
path = tmp_path / "file.xlsx"
df = valid_mfp_data()
df["Unexpected Column"] = ["Extra1", "Extra2", "Extra3"]
df.to_excel(path, index=False)
yield path


def test_mfp_to_yaml_success(valid_mfp_file, tmp_path):
"""Test that mfp_to_yaml correctly processes a valid MFP Excel file."""
yaml_output_path = tmp_path / "schedule.yaml"

# Run function (No need to mock open() for YAML, real file is created)
mfp_to_yaml("mock_file.xlsx", yaml_output_path)
mfp_to_yaml(valid_mfp_file, yaml_output_path)

# Ensure the YAML file was written
assert yaml_output_path.exists()
Expand All @@ -52,26 +68,24 @@ def test_mfp_to_yaml_success(mock_read_excel, tmp_path):
]


@patch("pandas.read_excel", return_value=MISSING_HEADERS_DATA)
def test_mfp_to_yaml_missing_headers(mock_read_excel, tmp_path):
def test_mfp_to_yaml_missing_headers(missing_columns_mfp_file, tmp_path):
"""Test that mfp_to_yaml raises an error when required columns are missing."""
yaml_output_path = tmp_path / "schedule.yaml"

with pytest.raises(
ValueError,
match="Error: Missing column 'Instrument'. Have you added this column after exporting from MFP?",
):
mfp_to_yaml("mock_file.xlsx", yaml_output_path)
mfp_to_yaml(missing_columns_mfp_file, yaml_output_path)


@patch("pandas.read_excel", return_value=EXTRA_HEADERS_DATA)
@patch("builtins.print") # Capture printed warnings
def test_mfp_to_yaml_extra_headers(mock_print, mock_read_excel, tmp_path):
def test_mfp_to_yaml_extra_headers(mock_print, unexpected_header_mfp_file, tmp_path):
"""Test that mfp_to_yaml prints a warning when extra columns are found."""
yaml_output_path = tmp_path / "schedule.yaml"

# Run function
mfp_to_yaml("mock_file.xlsx", yaml_output_path)
mfp_to_yaml(unexpected_header_mfp_file, yaml_output_path)

# Ensure a warning message was printed
mock_print.assert_any_call(
Expand All @@ -82,13 +96,12 @@ def test_mfp_to_yaml_extra_headers(mock_print, mock_read_excel, tmp_path):
)


@patch("pandas.read_excel", return_value=VALID_MFP_DATA)
def test_mfp_to_yaml_instrument_conversion(mock_read_excel, tmp_path):
def test_mfp_to_yaml_instrument_conversion(valid_mfp_file, tmp_path):
"""Test that instruments are correctly converted into InstrumentType enums."""
yaml_output_path = tmp_path / "schedule.yaml"

# Run function
mfp_to_yaml("mock_file.xlsx", yaml_output_path)
mfp_to_yaml(valid_mfp_file, yaml_output_path)

# Load the generated YAML
data = Schedule.from_yaml(yaml_output_path)
Expand Down

0 comments on commit 682c551

Please sign in to comment.