Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test coverage #78

Merged
merged 9 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
90 changes: 90 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,84 @@ def csv_path(tmp_path):
return csv_path


def test_primary_prediction_not_found(tmp_path):
dummy_video_path = tmp_path / "dummy_video.mp4"
eberrigan marked this conversation as resolved.
Show resolved Hide resolved
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=str(dummy_video_path), primary_name="nonexistent")

# format file path string for assert statement
path_obj = Path(dummy_video_path)
parent_dir = path_obj.parent
filename_without_extension = path_obj.stem
new_file_path = parent_dir / filename_without_extension

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 = tmp_path / "dummy_video.mp4"
eberrigan marked this conversation as resolved.
Show resolved Hide resolved
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=str(dummy_video_path), lateral_name="nonexistent")

# format file path string for assert statement
path_obj = Path(dummy_video_path)
parent_dir = path_obj.parent
filename_without_extension = path_obj.stem
new_file_path = parent_dir / filename_without_extension

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 = tmp_path / "dummy_video.mp4"
eberrigan marked this conversation as resolved.
Show resolved Hide resolved
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=str(dummy_video_path), crown_name="nonexistent")

# format file path string for assert statement
path_obj = Path(dummy_video_path)
parent_dir = path_obj.parent
filename_without_extension = path_obj.stem
new_file_path = parent_dir / filename_without_extension

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 = tmp_path / "invalid_video.mp4"
eberrigan marked this conversation as resolved.
Show resolved Hide resolved

output = io.StringIO()
with redirect_stdout(output):
Series.load(h5_path=str(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 +165,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"
eberrigan marked this conversation as resolved.
Show resolved Hide resolved

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
Loading