Skip to content

Commit

Permalink
Test a few corner cases of fetch_data
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrost-mo committed Aug 27, 2024
1 parent 5f66471 commit e2f1853
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/CSET/_workflow_utils/fetch_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def get_file(self, file_path: str, output_dir: str) -> None:
"""
file_paths = glob.glob(os.path.expanduser(file_path))
logging.debug("Copying files:\n%s", "\n".join(file_paths))
if not file_paths:
logging.warning("file_path does not match any files: %s", file_path)
for file in file_paths:
try:
shutil.copy(file, output_dir)
Expand Down
22 changes: 22 additions & 0 deletions tests/workflow_utils/test_fetch_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Tests for fetch_data workflow utility."""

import datetime
from pathlib import Path

import pytest

Expand Down Expand Up @@ -161,3 +162,24 @@ def test_FilesystemFileRetriever(tmp_path):
ffr.get_file("tests/test_data/exeter_em*.nc", str(tmp_path))
assert (tmp_path / "exeter_em01.nc").is_file()
assert (tmp_path / "exeter_em02.nc").is_file()


def test_FilesystemFileRetriever_no_files(tmp_path, caplog):
"""Test warning when no files match the requested path."""
with fetch_data.FilesystemFileRetriever() as ffr:
# Should warn, but not error.
ffr.get_file("/non-existent/file.nc", str(tmp_path))
log_record = caplog.records[0]
assert log_record.levelname == "WARNING"
assert log_record.message.startswith("file_path does not match any files:")


def test_FilesystemFileRetriever_copy_error(caplog):
"""Test warning when file copy errors."""
with fetch_data.FilesystemFileRetriever() as ffr:
# Please don't run as root.
ffr.get_file("tests/test_data/air_temp.nc", "/usr/bin")
assert not Path("/usr/bin/air_temp.nc").is_file()
log_record = caplog.records[0]
assert log_record.levelname == "WARNING"
assert log_record.message.startswith("Failed to copy")

0 comments on commit e2f1853

Please sign in to comment.