Skip to content

Commit 4045761

Browse files
gouwensdbirman
andauthored
Handle lists of specimen IDs in procedures validator (#1783)
* Handle lists of specimen IDs in procedures validator * Fix to properly handle individual str as well as lists of str * refactor: simplifying code a tiny bit --------- Co-authored-by: Dan Birman <danbirman@gmail.com>
1 parent ad8770a commit 4045761

2 files changed

Lines changed: 26 additions & 1 deletion

File tree

src/aind_data_schema/core/procedures.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ def validate_subject_specimen_ids(self):
7979
# Return if no specimen procedures
8080
if self.specimen_procedures:
8181
subject_id = self.subject_id
82-
specimen_ids = [spec_proc.specimen_id for spec_proc in self.specimen_procedures]
82+
specimen_id_vars = [spec_proc.specimen_id for spec_proc in self.specimen_procedures]
83+
specimen_ids = []
84+
for spec_id_var in specimen_id_vars:
85+
if isinstance(spec_id_var, str):
86+
specimen_ids.append(spec_id_var)
87+
else:
88+
specimen_ids.extend(spec_id_var)
8389

8490
if any(not subject_specimen_id_compatibility(subject_id, spec_id) for spec_id in specimen_ids):
8591
raise ValueError("specimen_id must be an extension of the subject_id.")

tests/test_procedures.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,25 @@ def test_validate_subject_specimen_ids(self):
516516
expected_exception = "specimen_id must be an extension of the subject_id."
517517
self.assertIn(expected_exception, str(e.exception))
518518

519+
def test_validate_subject_specimen_id_list_valid(self):
520+
"""Test that specimen_id accepts a list of strings when all contain subject_id"""
521+
522+
valid_procedure = Procedures(
523+
subject_id="12345",
524+
specimen_procedures=[
525+
SpecimenProcedure(
526+
specimen_id=["12345_001", "12345_002"],
527+
procedure_type="Other",
528+
start_date=date.fromisoformat("2020-10-10"),
529+
end_date=date.fromisoformat("2020-10-11"),
530+
experimenters=["Mam Moth"],
531+
protocol_id=["10"],
532+
notes="some notes",
533+
)
534+
],
535+
)
536+
self.assertIsNotNone(valid_procedure)
537+
519538
def test_craniotomy_position_validation(self):
520539
"""Test validation for craniotomy position"""
521540

0 commit comments

Comments
 (0)