Skip to content

Commit

Permalink
Merge pull request #667 from kids-first/remove-delete-sample-orphan
Browse files Browse the repository at this point in the history
♻️ Don't delete samples with 0 biospecimens
  • Loading branch information
znatty22 authored Jun 5, 2024
2 parents 51995c3 + c97c1ab commit 6eddc7c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 21 deletions.
18 changes: 2 additions & 16 deletions dataservice/api/sample/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@

class Sample(db.Model, Base):
"""
Sample - a biologically equivalent group of specimens
Sample - a biologically distinct unit
Background:
The current Biospecimen table does not adequately model the hierarchical
relationship between specimen groups and specimens. The Sample table has
been created to fill in this gap.
The Biospecimen represents an aliquot or portion of a Sample
:param kf_id: Unique id given by the Kid's First DCC
:param external_id: Name given to sample by contributor
Expand Down Expand Up @@ -108,13 +104,3 @@ class Sample(db.Model, Base):
biospecimens = db.relationship(
Biospecimen, backref=db.backref('sample', lazy=True)
)


@event.listens_for(Biospecimen, 'after_delete')
def delete_orphans(mapper, connection, state):
"""
Delete samples with 0 child biospecimens
"""
q = (db.session.query(Sample)
.filter(~Sample.biospecimens.any()))
q.delete(synchronize_session='fetch')
10 changes: 5 additions & 5 deletions tests/sample/test_sample_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ def test_delete_sample(self):

assert Biospecimen.query.get(bs_kf_id).sample_id is None

def test_delete_sample_orphan(self):
def test_no_delete_sample_orphan(self):
"""
Test that a sample is deleted when all related biospecimens
Test that a sample is NOT deleted when all related biospecimens
are deleted
"""
# Make two samples with biospecimens
Expand All @@ -60,13 +60,13 @@ def test_delete_sample_orphan(self):
db.session.delete(ct)
db.session.commit()

# Check that orphan sample is deleted
# Check that orphan sample is not deleted
result = Sample.query.filter_by(external_id="s01").one_or_none()
assert result is None
assert result.kf_id

# Check that other sample still exists
result = Sample.query.filter_by(external_id="s02").one_or_none()
assert result
assert result.kf_id
assert len(result.biospecimens) == 1

def test_update_sample(self):
Expand Down

0 comments on commit 6eddc7c

Please sign in to comment.