Skip to content

Commit 1c2fbfe

Browse files
Touch up delete_session function (#414)
1 parent dc89a81 commit 1c2fbfe

2 files changed

Lines changed: 13 additions & 8 deletions

File tree

src/openlifu/db/database.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ def write_session(self, subject:Subject, session:Session, on_conflict=OnConflict
220220
self.logger.info(f"Added session with ID {session_id} for subject {subject.id} to the database.")
221221

222222
def delete_session(self, subject_id: str, session_id: str, on_conflict: OnConflictOpts = OnConflictOpts.ERROR):
223+
"""Delete a session and its associated data from the database.
223224
225+
Args:
226+
subject_id: ID of the subject the session belongs to
227+
session_id: ID of the session to delete
228+
on_conflict: Behavior when session doesn't exist ('error' or 'skip')
229+
"""
224230
# Check if the session ID exists in the database for this subject
225231
session_ids = self.get_session_ids(subject_id)
226232

@@ -235,14 +241,13 @@ def delete_session(self, subject_id: str, session_id: str, on_conflict: OnConfli
235241

236242
# Delete the session file
237243
session_filename = self.get_session_filename(subject_id, session_id)
238-
if Path.is_file(session_filename):
244+
if session_filename.parent.is_dir():
239245
shutil.rmtree(session_filename.parent)
240246

241-
if session_id in session_ids:
242-
session_ids.remove(session_id)
243-
self.write_session_ids(subject_id, session_ids)
247+
session_ids.remove(session_id)
248+
self.write_session_ids(subject_id, session_ids)
244249

245-
self.logger.info(f"Removed Session with ID {session_id} from the database.")
250+
self.logger.info(f"Removed session with ID {session_id} from the database.")
246251

247252
def write_run(self, run:Run, session:Session = None, protocol:Protocol = None, on_conflict=OnConflictOpts.ERROR):
248253
"""Write a run with a snapshot of session and a snapshot of protocol if provided

tests/test_database.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,14 @@ def test_delete_session(example_database: Database, example_subject: Subject):
347347

348348
# Error option
349349
with pytest.raises(ValueError, match="does not exist in the database"):
350-
example_database.delete_session(subject_id, "non_existent_protocol", on_conflict=OnConflictOpts.ERROR)
350+
example_database.delete_session(subject_id, "non_existent_session", on_conflict=OnConflictOpts.ERROR)
351351

352352
# Skip option
353-
example_database.delete_session(subject_id, "non_existent_protocol", on_conflict=OnConflictOpts.SKIP)
353+
example_database.delete_session(subject_id, "non_existent_session", on_conflict=OnConflictOpts.SKIP)
354354

355355
# Invalid option
356356
with pytest.raises(ValueError, match="Invalid"):
357-
example_database.delete_session(subject_id, "non_existent_protocol", on_conflict=OnConflictOpts.OVERWRITE)
357+
example_database.delete_session(subject_id, "non_existent_session", on_conflict=OnConflictOpts.OVERWRITE)
358358

359359
def test_write_run(example_database: Database, tmp_path:Path):
360360
subject_id = "example_subject"

0 commit comments

Comments
 (0)