Skip to content

Commit 2742f16

Browse files
committed
Add check for invalid photoscan id when writing session with transducer tracking result (OpenwaterHealth#182)
1 parent a45e560 commit 2742f16

2 files changed

Lines changed: 20 additions & 2 deletions

File tree

src/openlifu/db/database.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,15 @@ def write_session(self, subject:Subject, session:Session, on_conflict=OnConflict
166166
f"The virtual_fit_results of session {session.id} provides no transforms for target {target_id}."
167167
)
168168

169+
# Validate the transducer tracking result's photoscan_id
170+
if session.transducer_tracking_results:
171+
for result in session.transducer_tracking_results:
172+
if result.photoscan_id not in self.get_photoscan_ids(subject.id, session.id):
173+
raise ValueError(
174+
f"Photoscan id {result.photoscan_id} provided in the transducer_tracking_results has not "
175+
"been associated with this session."
176+
)
177+
169178
# Check if the session already exists in the database
170179
session_ids = self.get_session_ids(subject.id)
171180

tests/test_database.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,12 +310,21 @@ def test_write_session_associated_object_structure_created(example_database: Dat
310310
assert example_database.get_solutions_filename(example_subject.id, session.id).is_file()
311311
assert example_database.get_runs_filename(example_subject.id, session.id).is_file()
312312

313-
def test_write_session_with_transducer_tracking_results(example_database: Database, example_subject: Subject, example_transducer_tracking_result):
313+
def test_write_session_with_invalid_photoscan_id(example_database: Database, example_subject: Subject, example_transducer_tracking_result):
314314
""" Test that when there is a transducer tracking result class associated with a session, the session
315315
is correctly written to file."""
316316
session = Session(name="bleh", id='a_session',subject_id=example_subject.id)
317317
session.transducer_tracking_results = [example_transducer_tracking_result]
318-
example_database.write_session(example_subject, session)
318+
example_transducer_tracking_result.photoscan_id = "bogus_photoscan"
319+
with pytest.raises(ValueError, match="been associated with this session"):
320+
example_database.write_session(example_subject, session)
321+
322+
def test_write_session_with_transducer_tracking_results(example_database: Database, example_subject: Subject, example_transducer_tracking_result):
323+
""" Test that when you write a session with a transducer tracking result associated with an
324+
invalid photoscan, an error is raised."""
325+
session = Session(name="bleh", id='example_session',subject_id=example_subject.id)
326+
session.transducer_tracking_results = [example_transducer_tracking_result]
327+
example_database.write_session(example_subject, session, on_conflict = OnConflictOpts.OVERWRITE)
319328

320329
def test_write_run(example_database: Database, tmp_path:Path):
321330
subject_id = "example_subject"

0 commit comments

Comments
 (0)