From 6deef1c0d5c801e8ca1e4f56c57e3b37ea00ac74 Mon Sep 17 00:00:00 2001 From: Vecko <36369090+VeckoTheGecko@users.noreply.github.com> Date: Mon, 24 Feb 2025 11:05:35 +0100 Subject: [PATCH] Update mfp message to use warning And also use pytests warn() system --- src/virtualship/utils.py | 8 +++++--- tests/test_mfp_to_yaml.py | 17 +++-------------- 2 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/virtualship/utils.py b/src/virtualship/utils.py index bc8a3dbe..980aa8ea 100644 --- a/src/virtualship/utils.py +++ b/src/virtualship/utils.py @@ -1,3 +1,4 @@ +import warnings from datetime import timedelta from functools import lru_cache from importlib.resources import files @@ -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) diff --git a/tests/test_mfp_to_yaml.py b/tests/test_mfp_to_yaml.py index 66fb3af1..10d9b93d 100644 --- a/tests/test_mfp_to_yaml.py +++ b/tests/test_mfp_to_yaml.py @@ -1,5 +1,3 @@ -from unittest.mock import patch - import pandas as pd import pytest @@ -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):