Skip to content

Commit

Permalink
🔧 Auto set reverse rel for both relations
Browse files Browse the repository at this point in the history
  • Loading branch information
znatty22 committed Jul 9, 2018
1 parent 246f395 commit 0f92ff8
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
28 changes: 19 additions & 9 deletions dataservice/api/family_relationship/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from sqlalchemy import event, or_, and_
from sqlalchemy import event, or_


from dataservice.extensions import db
Expand Down Expand Up @@ -123,13 +123,23 @@ def __repr__(self):
self.participant2.kf_id)


@event.listens_for(FamilyRelationship.participant1_to_participant2_relation,
'set')
def set_reverse_relation(target, value, oldvalue, initiator):
@event.listens_for(FamilyRelationship, 'before_update')
@event.listens_for(FamilyRelationship, 'before_insert')
def set_reverse_relation(mapper, connection, target):
"""
Listen for set 'participant1_to_participant2_relation' events and
set the reverse relationship, 'participant2_to_participant1_relation'
attribute
Set the appropriate relation types on both relation fields.
Given a relation type, look up the reverse relation in the REVERSE_RELS map
If a relation type is not found in the map do nothing.
"""
target.participant2_to_participant1_relation = REVERSE_RELS.get(
value.lower(), None)
# Participant 1 to 2 relation
rel = target.participant1_to_participant2_relation
rev = REVERSE_RELS.get(rel.lower() if rel else None)
if rev:
target.participant2_to_participant1_relation = rev

# Participant 2 to 1 relation
rel = target.participant2_to_participant1_relation
rev = REVERSE_RELS.get(rel.lower() if rel else None)
if rev:
target.participant1_to_participant2_relation = rev
11 changes: 10 additions & 1 deletion tests/family_relationship/test_family_relationship_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_foreign_key_constraint(self):
with self.assertRaises(IntegrityError):
db.session.commit()

def test_case_sensitivity(self):
def test_relation_types(self):
"""
Test that relationships are created w proper label regardless of case
"""
Expand Down Expand Up @@ -220,6 +220,15 @@ def test_case_sensitivity(self):
assert 'Sibling' == r.participant1_to_participant2_relation
assert 'Sibling' == r.participant2_to_participant1_relation

# Custom values can be set for both relation types
r.participant1_to_participant2_relation = 'Aunt'
r.participant2_to_participant1_relation = 'Niece'
db.session.commit()

r = FamilyRelationship.query.get(kf_id)
assert 'Aunt' == r.participant1_to_participant2_relation
assert 'Niece' == r.participant2_to_participant1_relation

def test_not_null_constraint(self):
"""
Test that a family relationship cannot be created without required
Expand Down
8 changes: 4 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ def test_status_format(self, client, endpoint, method):
('/biospecimens', ['participant', 'sequencing_center']),
('/sequencing-experiments', ['sequencing_center']),
('/genomic-files', ['biospecimen',
'sequencing_experiment',
'read_group']),
'sequencing_experiment',
'read_group']),
('/read-groups', ['genomic_file']),
('/cavatica-tasks', ['cavatica_app']),
('/cavatica-task-genomic-files', ['cavatica_task', 'genomic_file'])
Expand Down Expand Up @@ -124,8 +124,8 @@ def test_parent_links(self, client, entities, endpoint, parents):
('/investigators', ['studies']),
('/families', ['participants']),
('/sequencing-centers', ['sequencing_experiments', 'biospecimens']),
('/participants', ['diagnoses', 'family_relationships',
'phenotypes', 'outcomes', 'biospecimens']),
('/participants', ['diagnoses', 'phenotypes', 'outcomes',
'biospecimens']),
('/biospecimens', ['genomic_files', 'diagnoses']),
('/sequencing-experiments', ['genomic_files']),
('/genomic-files', ['cavatica_task_genomic_files']),
Expand Down

0 comments on commit 0f92ff8

Please sign in to comment.