示例#1
0
def test_delete_child_case(cli_runner: CliRunner, base_context: CGConfig, helpers: StoreHelpers):
    """Test that the delete case will not delete a sample linked to another case as mother"""
    # GIVEN a database with a mother case and a child case with the mother as mother
    base_store: Store = base_context.status_db
    case_mother = helpers.add_case(base_store, "case_mother")
    sample_mother = helpers.add_sample(base_store, "mother")
    sample_child = helpers.add_sample(base_store, "child")
    helpers.add_relationship(store=base_store, case=case_mother, sample=sample_mother)
    case_child = helpers.add_case(base_store, "case_child")
    case_child_id = case_child.internal_id
    helpers.add_relationship(
        store=base_store, case=case_child, sample=sample_child, mother=sample_mother
    )

    assert base_store.Family.query.count() == 2
    assert base_store.FamilySample.query.count() == 2
    assert base_store.Sample.query.count() == 2

    # WHEN deleting the child case
    result = cli_runner.invoke(delete_case_command, [case_child_id, "--yes"], obj=base_context)

    # THEN the child sample is deletable
    assert result.exit_code == SUCCESS
    assert base_store.Family.query.count() == 1
    assert base_store.FamilySample.query.count() == 1
    assert base_store.Sample.query.count() == 1
示例#2
0
def test_delete_case_with_dry_run(
    cli_runner: CliRunner, base_context: CGConfig, helpers: StoreHelpers, caplog
):
    """Test that the delete case will not delete the case in dry-run mode"""
    # GIVEN a database with a case
    base_store: Store = base_context.status_db
    case_obj = helpers.add_case(base_store)
    case_id = case_obj.internal_id
    sample = helpers.add_sample(base_store)
    helpers.add_relationship(store=base_store, case=case_obj, sample=sample)
    assert base_store.Family.query.count() == 1
    assert base_store.FamilySample.query.count() == 1
    assert base_store.Sample.query.count() == 1

    # WHEN deleting a case
    caplog.set_level(logging.DEBUG)
    result = cli_runner.invoke(
        delete_case_command, [case_id, "--yes", "--dry-run"], obj=base_context
    )

    # THEN it should not have been deleted
    assert result.exit_code == SUCCESS
    assert base_store.Family.query.count() == 1
    assert base_store.FamilySample.query.count() == 1
    assert base_store.Sample.query.count() == 1
    assert "Link:" in caplog.text
    assert "Sample is linked" in caplog.text
    assert "Case:" in caplog.text
    assert " was NOT deleted due to --dry-run" in caplog.text
示例#3
0
def test_delete_case_with_father_links(
    cli_runner: CliRunner, base_context: CGConfig, helpers: StoreHelpers
):
    """Test that the delete case will not delete a sample linked to another case as father"""
    # GIVEN a database with a case
    base_store: Store = base_context.status_db
    case_obj = helpers.add_case(base_store, "first_case_linked_to_sample")
    case_id = case_obj.internal_id
    sample_father = helpers.add_sample(base_store, "father")
    sample_child = helpers.add_sample(base_store, "child")
    helpers.add_relationship(store=base_store, case=case_obj, sample=sample_father)
    case_obj2 = helpers.add_case(base_store, "second_case_linked_to_sample")
    helpers.add_relationship(
        store=base_store, case=case_obj2, sample=sample_child, father=sample_father
    )

    assert base_store.Family.query.count() == 2
    assert base_store.FamilySample.query.count() == 2
    assert base_store.Sample.query.count() == 2

    # WHEN deleting a case
    result = cli_runner.invoke(delete_case_command, [case_id, "--yes"], obj=base_context)

    # THEN the first case should be gone with its link to the sample but not the other or
    # the father sample
    assert result.exit_code == SUCCESS
    assert base_store.Family.query.count() == 1
    assert base_store.FamilySample.query.count() == 1
    assert base_store.Sample.query.count() == 2
示例#4
0
def test_families_by_subject_id(
    data_analysis: Pipeline,
    helpers: StoreHelpers,
    sample_store: Store,
):
    """Test that we get a case back for a subject id"""
    # GIVEN connected case exist
    subject_id = "a_subject_id"
    store: Store = sample_store

    rna_sample: models.Sample = helpers.add_sample(store=store,
                                                   subject_id=subject_id)
    rna_case: models.Family = helpers.add_case(store=store,
                                               data_analysis=Pipeline.MIP_RNA,
                                               name="rna_case")
    helpers.add_relationship(store=store, case=rna_case, sample=rna_sample)
    store.add_commit(rna_case)

    dna_sample: models.Sample = helpers.add_sample(store=store,
                                                   subject_id=subject_id)
    dna_case: models.Family = helpers.add_case(store=store,
                                               data_analysis=data_analysis,
                                               name="dna_case")
    helpers.add_relationship(store=store, case=dna_case, sample=dna_sample)
    store.add_commit(dna_case)

    customer: models.Customer = dna_case.customer

    # WHEN calling method families_by_subject_id
    all_cases: set[models.Family] = sample_store.families_by_subject_id(
        customer_id=customer.internal_id, subject_id=subject_id)

    dna_cases: set[models.Family] = sample_store.families_by_subject_id(
        customer_id=customer.internal_id,
        subject_id=subject_id,
        data_analyses=[data_analysis])

    # THEN we got the case as result and another data_analysis not in result
    assert dna_case in all_cases
    assert rna_case in all_cases
    assert dna_case in dna_cases
    assert rna_case not in dna_cases
def test_get_sample_families_with_family(cli_runner: CliRunner,
                                         base_context: CGConfig,
                                         disk_store: Store,
                                         helpers: StoreHelpers):
    """Test that the --families flag does show case info"""
    # GIVEN a database with a sample with related samples
    case = helpers.add_case(disk_store)
    sample = helpers.add_sample(disk_store)
    sample_id = sample.internal_id
    helpers.add_relationship(disk_store, sample=sample, case=case)
    assert disk_store.Sample.query.first().links

    # WHEN getting a sample with the --families flag
    result = cli_runner.invoke(get, ["sample", sample_id, "--families"],
                               obj=base_context)

    # THEN all related families should be listed in the output
    assert result.exit_code == 0
    for link in disk_store.Sample.query.first().links:
        assert link.family.internal_id in result.output
示例#6
0
def test_compress_fastq_cli_case_id(populated_compress_context: CGConfig,
                                    cli_runner: CliRunner,
                                    helpers: StoreHelpers, caplog):
    """Test to run the compress command with a specified case id"""
    caplog.set_level(logging.DEBUG)
    status_db: Store = populated_compress_context.status_db

    # GIVEN a context with a case that can be compressed
    case_id = "chonkywombat"

    valid_compressable_case = helpers.add_case(
        store=status_db,
        name=case_id,
        internal_id=case_id,
        data_analysis=Pipeline.MIP_DNA,
        action=None,
    )
    valid_compressable_case.created_at = dt.datetime.now() - dt.timedelta(
        days=1000)
    sample1 = helpers.add_sample(store=status_db, internal_id="ACCR9000")
    sample2 = helpers.add_sample(store=status_db, internal_id="ACCR9001")
    helpers.add_relationship(
        store=status_db,
        sample=sample1,
        case=valid_compressable_case,
    )
    helpers.add_relationship(
        store=status_db,
        sample=sample2,
        case=valid_compressable_case,
    )
    status_db.commit()

    # WHEN running the compress command
    res = cli_runner.invoke(fastq_cmd, ["--case-id", case_id],
                            obj=populated_compress_context)

    # THEN assert the program exits since no cases where found
    assert res.exit_code == 0
    # THEN assert it was communicated that no families where found
    assert f"Individuals in 1 (completed) cases where compressed" in caplog.text
示例#7
0
def fixture_analysis_store(base_store: Store, workflow_case_id: str,
                           helpers: StoreHelpers) -> Store:
    """Store to be used in tests"""
    _store = base_store

    case = helpers.add_case(_store,
                            workflow_case_id,
                            data_analysis=Pipeline.MIP_DNA)

    dna_sample = helpers.add_sample(_store,
                                    "dna_sample",
                                    is_rna=False,
                                    sequenced_at=datetime.now(),
                                    reads=10000000)
    helpers.add_relationship(_store, sample=dna_sample, case=case)

    case = helpers.add_case(_store, "rna_case", data_analysis=Pipeline.MIP_RNA)
    rna_sample = helpers.add_sample(_store,
                                    "rna_sample",
                                    is_rna=True,
                                    sequenced_at=datetime.now(),
                                    reads=10000000)
    helpers.add_relationship(_store, sample=rna_sample, case=case)

    case = helpers.add_case(_store,
                            "dna_rna_mix_case",
                            data_analysis=Pipeline.MIP_DNA)
    helpers.add_relationship(_store, sample=rna_sample, case=case)
    helpers.add_relationship(_store, sample=dna_sample, case=case)

    return _store
示例#8
0
def fixture_dna_mip_context(
    cg_context: CGConfig,
    helpers: StoreHelpers,
    mip_case_ids: dict,
    real_housekeeper_api: HousekeeperAPI,
    tb_api,
) -> CGConfig:
    _store = cg_context.status_db
    cg_context.housekeeper_api_ = real_housekeeper_api
    cg_context.trailblazer_api_ = tb_api
    mip_analysis_api = MipDNAAnalysisAPI(config=cg_context)

    # Add apptag to db
    helpers.ensure_application_version(store=_store,
                                       application_tag="WGSA",
                                       application_type="wgs")

    # Add sample, cases and relationships to db

    for case_id in mip_case_ids:
        if not _store.family(case_id):
            case_obj = helpers.add_case(
                store=_store,
                data_analysis=Pipeline.MIP_DNA,
                internal_id=case_id,
                name=mip_case_ids[case_id]["name"],
            )
            sample = helpers.add_sample(
                store=_store,
                customer_id="cust000",
                application_tag="WGSA",
                application_type="wgs",
                gender="unknown",
            )
            helpers.add_relationship(store=_store,
                                     sample=sample,
                                     case=case_obj,
                                     status="affected")
    cg_context.meta_apis["analysis_api"] = mip_analysis_api
    return cg_context
示例#9
0
def fixture_rna_store(
    base_store: Store,
    helpers: StoreHelpers,
    rna_case_id: str,
    dna_case_id: str,
) -> Store:
    """Populate store with an rna case that is connected to a dna case via sample.subject_id"""

    store: Store = base_store

    # an existing RNA case with related sample
    rna_case = helpers.ensure_case(
        store=store,
        name="rna_case",
        customer=helpers.ensure_customer(store=store),
        data_analysis=Pipeline.MIP_RNA,
        data_delivery=DataDelivery.SCOUT,
    )
    rna_case.internal_id = rna_case_id

    rna_sample_son = helpers.add_sample(store=store, name="rna_son", subject_id="son")
    rna_sample_daughter = helpers.add_sample(
        store=store, name="rna_daughter", subject_id="daughter"
    )
    rna_sample_mother = helpers.add_sample(store=store, name="rna_mother", subject_id="mother")
    rna_sample_father = helpers.add_sample(store=store, name="rna_father", subject_id="father")
    helpers.add_relationship(
        store=store,
        sample=rna_sample_son,
        case=rna_case,
        mother=rna_sample_mother,
        father=rna_sample_father,
        status="affected",
    )
    helpers.add_relationship(
        store=store,
        sample=rna_sample_daughter,
        case=rna_case,
        mother=rna_sample_mother,
        father=rna_sample_father,
        status="unaffected",
    )
    helpers.add_relationship(
        store=store, sample=rna_sample_mother, case=rna_case, status="unaffected"
    )
    helpers.add_relationship(
        store=store, sample=rna_sample_father, case=rna_case, status="affected"
    )

    for link in rna_case.links:
        link.sample.internal_id = link.sample.name

    # an existing DNA case with related sample
    dna_case = helpers.ensure_case(
        store=store,
        name="dna_case",
        customer=helpers.ensure_customer(store=store),
        data_analysis=Pipeline.MIP_DNA,
        data_delivery=DataDelivery.SCOUT,
    )
    dna_case.internal_id = dna_case_id

    dna_sample_son = helpers.add_sample(store=store, name="dna_son", subject_id="son")
    dna_sample_daughter = helpers.add_sample(
        store=store, name="dna_daughter", subject_id="daughter"
    )
    dna_sample_mother = helpers.add_sample(store=store, name="dna_mother", subject_id="mother")
    dna_sample_father = helpers.add_sample(store=store, name="dna_father", subject_id="father")
    helpers.add_relationship(
        store=store,
        sample=dna_sample_son,
        case=dna_case,
        mother=dna_sample_mother,
        father=dna_sample_father,
        status="affected",
    )
    helpers.add_relationship(
        store=store,
        sample=dna_sample_daughter,
        case=dna_case,
        mother=dna_sample_mother,
        father=dna_sample_father,
        status="unaffected",
    )
    helpers.add_relationship(
        store=store, sample=dna_sample_mother, case=dna_case, status="unaffected"
    )
    helpers.add_relationship(
        store=store, sample=dna_sample_father, case=dna_case, status="affected"
    )

    for link in dna_case.links:
        link.sample.internal_id = link.sample.name

    store.commit()
    return store