Skip to content

Commit

Permalink
3714 Validate sample sex in cg add relationship Command (#3863)
Browse files Browse the repository at this point in the history
Added
A check to check sex for father and mother for the CLI command add relationship
Added a test to test if sex is incorrect, mother having sex = male, etc.

Changed
Command add relationship will now be sex sensitive, and mother needs to have sex female, father needs to have sex male.

Fixed
This will fix situations when prod needs to add a relationship manually.
  • Loading branch information
RasmusBurge-CG authored Nov 11, 2024
1 parent a705dde commit 219029b
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
8 changes: 8 additions & 0 deletions cg/cli/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,12 +337,20 @@ def link_sample_to_case(
LOG.error(f"{mother_id}: mother not found")
raise click.Abort

if mother.sex != Sex.FEMALE:
LOG.error(f"{mother_id}: mother is not {Sex.FEMALE}")
raise click.Abort

if father_id:
father: Sample = status_db.get_sample_by_internal_id(internal_id=father_id)
if father is None:
LOG.error(f"{father_id}: father not found")
raise click.Abort

if father.sex != Sex.MALE:
LOG.error(f"{father_id}: father is not {Sex.MALE}")
raise click.Abort

new_record: CaseSample = status_db.relate_sample(
case=case_obj, sample=sample, status=status, mother=mother, father=father
)
Expand Down
73 changes: 73 additions & 0 deletions tests/cli/add/test_cli_add_relationship.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from click.testing import CliRunner

from cg.cli.add import add
from cg.constants import EXIT_FAIL
from cg.constants.subject import Sex
from cg.models.cg_config import CGConfig
from cg.store.models import CaseSample
Expand Down Expand Up @@ -257,3 +258,75 @@ def test_add_relationship_bad_father(
# THEN it should not be added
assert result.exit_code == 1
assert disk_store._get_query(table=CaseSample).count() == 0


def test_add_relationship_mother_not_female(
cli_runner: CliRunner, base_context: CGConfig, helpers: StoreHelpers
):
"""Test to add a relationship where the mother is not female"""
# GIVEN a database with a sample, a case, and a male sample labeled as mother
disk_store: Store = base_context.status_db
sample: Sample = helpers.add_sample(disk_store)
sample_id: str = sample.internal_id

male_mother: Sample = helpers.add_sample(disk_store, sex=Sex.MALE, name="mother")
male_mother_id: str = male_mother.internal_id

case: Case = helpers.add_case(disk_store)
case_id: str = case.internal_id
status = "affected"

# WHEN adding a relationship with a male mother
result = cli_runner.invoke(
add,
[
"relationship",
case_id,
sample_id,
"-s",
status,
"-m",
male_mother_id,
],
obj=base_context,
)

# THEN it should fail because the mother is not female
assert result.exit_code == EXIT_FAIL
assert disk_store._get_query(table=CaseSample).count() == 0


def test_add_relationship_father_not_male(
cli_runner: CliRunner, base_context: CGConfig, helpers: StoreHelpers
):
"""Test to add a relationship where the father is not male"""
# GIVEN a database with a sample, a case, and a female sample labeled as father
disk_store: Store = base_context.status_db
sample: Sample = helpers.add_sample(disk_store)
sample_id: str = sample.internal_id

female_father: Sample = helpers.add_sample(disk_store, sex=Sex.FEMALE, name="father")
female_father_id: str = female_father.internal_id

case: Case = helpers.add_case(disk_store)
case_id: str = case.internal_id
status = "affected"

# WHEN adding a relationship with a female father
result = cli_runner.invoke(
add,
[
"relationship",
case_id,
sample_id,
"-s",
status,
"-f",
female_father_id,
],
obj=base_context,
)

# THEN it should fail because the father is not male
assert result.exit_code == EXIT_FAIL
assert disk_store._get_query(table=CaseSample).count() == 0

0 comments on commit 219029b

Please sign in to comment.