Skip to content

Commit

Permalink
Update mfp message to use warning
Browse files Browse the repository at this point in the history
And also use pytests warn() system
  • Loading branch information
VeckoTheGecko committed Feb 24, 2025
1 parent 682c551 commit 6deef1c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 17 deletions.
8 changes: 5 additions & 3 deletions src/virtualship/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from datetime import timedelta
from functools import lru_cache
from importlib.resources import files
Expand Down Expand Up @@ -89,11 +90,12 @@ def mfp_to_yaml(excel_file_path: str, yaml_output_path: str): # noqa: D417

extra_columns = actual_columns - expected_columns
if extra_columns:
print(
f"Warning: Found additional unexpected columns {list(extra_columns)}. "
warnings.warn(
f"Found additional unexpected columns {list(extra_columns)}. "
"Manually added columns have no effect. "
"If the MFP export format changed, please submit an issue: "
"https://github.com/OceanParcels/virtualship/issues."
"https://github.com/OceanParcels/virtualship/issues.",
stacklevel=2,
)

# Drop unexpected columns (optional, only if you want to ensure strict conformity)
Expand Down
17 changes: 3 additions & 14 deletions tests/test_mfp_to_yaml.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from unittest.mock import patch

import pandas as pd
import pytest

Expand Down Expand Up @@ -79,21 +77,12 @@ def test_mfp_to_yaml_missing_headers(missing_columns_mfp_file, tmp_path):
mfp_to_yaml(missing_columns_mfp_file, yaml_output_path)


@patch("builtins.print") # Capture printed warnings
def test_mfp_to_yaml_extra_headers(mock_print, unexpected_header_mfp_file, tmp_path):
def test_mfp_to_yaml_extra_headers(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(unexpected_header_mfp_file, yaml_output_path)

# Ensure a warning message was printed
mock_print.assert_any_call(
"Warning: Found additional unexpected columns ['Unexpected Column']. "
"Manually added columns have no effect. "
"If the MFP export format changed, please submit an issue: "
"https://github.com/OceanParcels/virtualship/issues."
)
with pytest.warns(UserWarning, match="Found additional unexpected columns.*"):
mfp_to_yaml(unexpected_header_mfp_file, yaml_output_path)


def test_mfp_to_yaml_instrument_conversion(valid_mfp_file, tmp_path):
Expand Down

0 comments on commit 6deef1c

Please sign in to comment.