diff --git a/cg/cli/add.py b/cg/cli/add.py index ce0c8cf505..86a097a0bf 100644 --- a/cg/cli/add.py +++ b/cg/cli/add.py @@ -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 ) diff --git a/tests/cli/add/test_cli_add_relationship.py b/tests/cli/add/test_cli_add_relationship.py index c11a33c3bb..bc0b960964 100644 --- a/tests/cli/add/test_cli_add_relationship.py +++ b/tests/cli/add/test_cli_add_relationship.py @@ -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 @@ -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