From 151ddb375b480bac3adf178c286d1dcb6db9cf0f Mon Sep 17 00:00:00 2001 From: Elise Davis Date: Wed, 8 May 2024 15:32:42 -0700 Subject: [PATCH] Increase test coverage (#78) * add tests for filenotfound error * if video is not found produce error * black * condensed file formatting * black * Path lib * path string * path string * as_possix --- sleap_roots/series.py | 4 ++- tests/test_series.py | 82 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/sleap_roots/series.py b/sleap_roots/series.py index 5b9e4d6..c769fe4 100644 --- a/sleap_roots/series.py +++ b/sleap_roots/series.py @@ -114,7 +114,9 @@ def load( # Attempt to load the video, with error handling video = None try: - video = sio.Video.from_filename(h5_path) if Path(h5_path).exists() else None + if not Path(h5_path).exists(): + raise FileNotFoundError(f"File not found") + video = sio.Video.from_filename(h5_path) except Exception as e: print(f"Error loading video file {h5_path}: {e}") diff --git a/tests/test_series.py b/tests/test_series.py index 207f438..6c861b2 100644 --- a/tests/test_series.py +++ b/tests/test_series.py @@ -4,6 +4,8 @@ from sleap_roots.series import Series, find_all_series from pathlib import Path from typing import Literal +from contextlib import redirect_stdout +import io @pytest.fixture @@ -57,6 +59,76 @@ def csv_path(tmp_path): return csv_path +def test_primary_prediction_not_found(tmp_path): + dummy_video_path = Path(tmp_path) / "dummy_video.mp4" + dummy_video_path.write_text("This is a dummy video file.") + + # Create a dummy Series instance with a non-existent primary prediction file + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=dummy_video_path, primary_name="nonexistent") + + # format file path string for assert statement + new_file_path = Path(dummy_video_path).with_suffix("").as_posix() + print(new_file_path) + + assert ( + output.getvalue() + == f"Primary prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + ) + + +def test_lateral_prediction_not_found(tmp_path): + dummy_video_path = Path(tmp_path) / "dummy_video.mp4" + dummy_video_path.write_text("This is a dummy video file.") + + # Create a dummy Series instance with a non-existent primary prediction file + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=dummy_video_path, lateral_name="nonexistent") + + # format file path string for assert statement + new_file_path = Path(dummy_video_path).with_suffix("").as_posix() + + assert ( + output.getvalue() + == f"Lateral prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + ) + + +def test_crown_prediction_not_found(tmp_path): + dummy_video_path = Path(tmp_path) / "dummy_video.mp4" + dummy_video_path.write_text("This is a dummy video file.") + + # Create a dummy Series instance with a non-existent primary prediction file + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=dummy_video_path, crown_name="nonexistent") + + # format file path string for assert statement + new_file_path = Path(dummy_video_path).with_suffix("").as_posix() + + assert ( + output.getvalue() + == f"Crown prediction file not found: {new_file_path}.nonexistent.predictions.slp\n" + ) + + +def test_video_loading_error(tmp_path): + # Create a dummy Series instance with an invalid video file path + invalid_video_path = Path(tmp_path) / "invalid_video.mp4" + + output = io.StringIO() + with redirect_stdout(output): + Series.load(h5_path=invalid_video_path) + + # Check if the correct error message is output + assert ( + output.getvalue() + == f"Error loading video file {invalid_video_path}: File not found\n" + ) + + def test_series_name(dummy_series): expected_name = "dummy_video" # Based on the dummy_video_path fixture assert dummy_series.series_name == expected_name @@ -85,6 +157,16 @@ def test_expected_count(series_instance, csv_path): assert series_instance.expected_count == 10 +def test_expected_count_error(series_instance, tmp_path): + series_instance.csv_path = tmp_path / "invalid" + + output = io.StringIO() + with redirect_stdout(output): + series_instance.expected_count + # Check if the correct error message is output + assert output.getvalue() == "CSV path is not set or the file does not exist.\n" + + def test_qc_cylinder(series_instance, csv_path): series_instance.csv_path = csv_path assert series_instance.qc_fail == 0