Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 27, 2025
1 parent 5e65979 commit 70dd4d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
9 changes: 4 additions & 5 deletions src/virtualship/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,24 @@ def _generic_load_yaml(data: str, model: BaseModel) -> BaseModel:

def load_coordinates(file_path):
"""Loads coordinates from a file based on its extension."""

if not os.path.isfile(file_path):
raise FileNotFoundError(f"File not found: {file_path}")

ext = os.path.splitext(file_path)[-1].lower()

try:
if ext in ['.xls', '.xlsx']:
if ext in [".xls", ".xlsx"]:
return pd.read_excel(file_path)

if ext == '.csv':
if ext == ".csv":
return pd.read_csv(file_path)

raise ValueError(f"Unsupported file extension {ext}.")

except Exception as e:
raise RuntimeError(
f"Could not read coordinates data from the provided file. "
f"Ensure it is either a csv or excel file."
"Could not read coordinates data from the provided file. "
"Ensure it is either a csv or excel file."
) from e


Expand Down
44 changes: 21 additions & 23 deletions tests/test_mfp_to_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ def valid_csv_mfp_file(tmp_path):
@pytest.fixture
def valid_csv_mfp_file_with_commas(tmp_path):
path = tmp_path / "file.csv"
valid_mfp_data().to_csv(path, decimal=',', index=False)
valid_mfp_data().to_csv(path, decimal=",", index=False)
return path


@pytest.fixture
def invalid_mfp_file(tmp_path):
path = tmp_path / "file.csv"
valid_mfp_data().to_csv(path, decimal=',', sep='|', index=False)
valid_mfp_data().to_csv(path, decimal=",", sep="|", index=False)

return path

Expand All @@ -69,18 +69,14 @@ def nonexistent_mfp_file(tmp_path):
@pytest.fixture
def missing_instruments_column_mfp_file(tmp_path):
path = tmp_path / "file.xlsx"
valid_mfp_data().drop(columns=["Instrument"]).to_excel(
path, index=False
)
valid_mfp_data().drop(columns=["Instrument"]).to_excel(path, index=False)
return path


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


Expand All @@ -95,15 +91,10 @@ def unexpected_header_mfp_file(tmp_path):

@pytest.mark.parametrize(
"fixture_name",
[
"valid_excel_mfp_file",
"valid_csv_mfp_file",
"valid_csv_mfp_file_with_commas"
]
["valid_excel_mfp_file", "valid_csv_mfp_file", "valid_csv_mfp_file_with_commas"],
)
def test_mfp_to_yaml_success(request, fixture_name, tmp_path):
"""Test that mfp_to_yaml correctly processes a valid MFP file."""

valid_mfp_file = request.getfixturevalue(fixture_name)

yaml_output_path = tmp_path / "schedule.yaml"
Expand All @@ -130,36 +121,43 @@ def test_mfp_to_yaml_success(request, fixture_name, tmp_path):
@pytest.mark.parametrize(
"fixture_name,error,match",
[
pytest.param("nonexistent_mfp_file", FileNotFoundError, os.path.basename("/non_file.csv"), id="FileNotFound"),
pytest.param(
"nonexistent_mfp_file",
FileNotFoundError,
os.path.basename("/non_file.csv"),
id="FileNotFound",
),
pytest.param(
"unsupported_extension_mfp_file",
RuntimeError,
"Could not read coordinates data from the provided file. Ensure it is either a csv or excel file.",
id="UnsupportedExtension"
id="UnsupportedExtension",
),
pytest.param(
"invalid_mfp_file",
RuntimeError,
"Could not read coordinates data from the provided file. Ensure it is either a csv or excel file.",
id="InvalidFile"
id="InvalidFile",
),
pytest.param(
"missing_instruments_column_mfp_file",
ValueError,
"Error: Missing column 'Instrument'. Have you added this column after exporting from MFP?",
id="MissingInstruments"
id="MissingInstruments",
),
pytest.param(
"missing_columns_mfp_file",
ValueError,
(r"Error: Found columns \[.*?('Station Type'| 'Name'| 'Latitude'| 'Instrument').*?\], "
r"but expected columns \[.*?('Station Type'| 'Name'| 'Latitude'| 'Instrument'| 'Longitude').*?\]."),
id="MissingColumns"),
]
(
r"Error: Found columns \[.*?('Station Type'| 'Name'| 'Latitude'| 'Instrument').*?\], "
r"but expected columns \[.*?('Station Type'| 'Name'| 'Latitude'| 'Instrument'| 'Longitude').*?\]."
),
id="MissingColumns",
),
],
)
def test_mfp_to_yaml_exceptions(request, fixture_name, error, match, tmp_path):
"""Test that mfp_to_yaml raises an error when input file is not valid."""

fixture = request.getfixturevalue(fixture_name)

yaml_output_path = tmp_path / "schedule.yaml"
Expand Down

0 comments on commit 70dd4d5

Please sign in to comment.