Skip to content

Commit 0233a10

Browse files
committed
Add unit tests for loading photoscans from database (#198)
1 parent bbf1667 commit 0233a10

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

src/openlifu/db/database.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,11 @@ def get_photoscan_absolute_filepaths_info(self, subject_id, session_id, photosca
631631
def load_photoscan(self, subject_id, session_id, photoscan_id, load_data = False):
632632
"""Returns a photoscan object and optionally, also returns the loaded model and texture
633633
data as Tuple[vtkPolyData, vtkImageData] if load_data = True."""
634+
634635
photoscan_metadata_filepath = self.get_photoscan_metadata_filepath(subject_id, session_id, photoscan_id)
636+
if not photoscan_metadata_filepath.exists() or not photoscan_metadata_filepath.is_file():
637+
raise FileNotFoundError(f"Photoscan file not found for photoscan {photoscan_id}, session {session_id}")
638+
635639
photoscan = Photoscan.from_file(photoscan_metadata_filepath)
636640

637641
if load_data:

tests/test_database.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import pytest
1010
from helpers import dataclasses_are_equal
11+
from vtk import vtkImageData, vtkPolyData
1112

1213
from openlifu import Point, Solution
1314
from openlifu.db import Session, Subject
@@ -497,6 +498,19 @@ def test_write_photoscan(example_database:Database, example_session: Session, tm
497498
with pytest.raises(FileNotFoundError, match="does not exist"):
498499
example_database.write_photoscan(example_session.subject_id, example_session.id, photoscan, model_data_path, bogus_texture_file, on_conflict=OnConflictOpts.OVERWRITE)
499500

501+
def test_load_photoscan(example_database:Database, example_session:Session):
502+
with pytest.raises(FileNotFoundError,match="Photoscan file not found"):
503+
example_database.load_photoscan(example_session.subject_id, example_session.id, "bogus_photoscan_id")
504+
505+
example_photoscan = example_database.load_photoscan(example_session.subject_id, example_session.id, "example_photoscan")
506+
assert example_photoscan.name == "ExamplePhotoscan"
507+
508+
example_photoscan, (model_data, texture_data) = example_database.load_photoscan(example_session.subject_id, example_session.id, "example_photoscan", load_data=True)
509+
assert model_data is not None
510+
assert texture_data is not None
511+
assert isinstance(model_data, vtkPolyData)
512+
assert isinstance(texture_data,vtkImageData)
513+
500514
def test_session_created_date():
501515
"""Test that created date is recent when a session is created."""
502516
tolerance = timedelta(seconds=2) # Allow for minor timing discrepancies

tests/test_photoscans.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_load_photoscan(example_database:Database, tmp_path:Path):
3535
subject_id = "example_subject"
3636
session_id = "example_session"
3737
photoscan_id = "example_photoscan"
38-
photoscan_info = example_database.get_photoscan_info(subject_id, session_id, photoscan_id)
38+
photoscan_info = example_database.get_photoscan_absolute_filepaths_info(subject_id, session_id, photoscan_id)
3939
[model, texture] = load_data_from_filepaths(photoscan_info["model_abspath"], photoscan_info["texture_abspath"])
4040
assert model is not None
4141
assert texture is not None

0 commit comments

Comments
 (0)