Skip to content

Commit

Permalink
Increase test coverage (#78)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
emdavis02 authored May 8, 2024
1 parent 9516f47 commit 151ddb3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
4 changes: 3 additions & 1 deletion sleap_roots/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")

Expand Down
82 changes: 82 additions & 0 deletions tests/test_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 151ddb3

Please sign in to comment.