22
33import pytest
44from ga4gh .core .models import iriReference
5- from ga4gh .va_spec .base import CohortAlleleFrequencyStudyResult
6- from ga4gh .vrs .models import Allele , LiteralSequenceExpression
7- from tests . conftest import return_cafs
5+ from ga4gh .va_spec .base import CohortAlleleFrequencyStudyResult , StudyGroup
6+ from ga4gh .vrs .models import Allele , LiteralSequenceExpression , sequenceString
7+ from sqlalchemy . exc import IntegrityError
88
99from anyvlm .storage .postgres import PostgresObjectStore
1010
@@ -17,37 +17,48 @@ def caf_allele(caf_iri: CohortAlleleFrequencyStudyResult):
1717 """
1818 caf_allele = caf_iri .model_copy (deep = True )
1919 caf_allele .focusAllele = Allele (
20- id = caf_iri .focusAllele .root ,
20+ id = caf_iri .focusAllele .root , # type: ignore
2121 location = iriReference ("locations.json#/1" ),
22- state = LiteralSequenceExpression (sequence = "A" ),
22+ state = LiteralSequenceExpression (sequence = sequenceString ( "A" ) ),
2323 )
2424 return caf_allele
2525
2626
27+ @pytest .fixture
28+ def caf_empty_cohort (caf_iri : CohortAlleleFrequencyStudyResult ):
29+ """Create test fixture for CAF object that uses empty cohort"""
30+ caf = caf_iri .model_copy (deep = True )
31+ caf .cohort = StudyGroup () # type: ignore
32+ return caf
33+
34+
2735@pytest .mark .parametrize ("caf_fixture_name" , ["caf_iri" , "caf_allele" ])
28- def test_add_allele_frequency (
36+ def test_add_allele_frequencies (
2937 request , caf_fixture_name : str , postgres_storage : PostgresObjectStore
3038):
31- """Test that add_allele_frequency method works correctly"""
39+ """Test that add_allele_frequencies method works correctly"""
3240 caf = request .getfixturevalue (caf_fixture_name )
41+ try :
42+ postgres_storage .add_allele_frequencies (caf )
43+ except Exception as e : # noqa: BLE001
44+ pytest .fail (f"add_allele_frequencies raised an exception: { e } " )
3345
34- def insert_and_verify_one_row ():
35- postgres_storage .add_allele_frequency (caf )
46+ caf = CohortAlleleFrequencyStudyResult (
47+ focusAllele = iriReference ("ga4gh:VA.J3Hi64dkKFKdnKIwB2419Qz3STDB2sJq" ),
48+ focusAlleleCount = 1 ,
49+ locusAlleleCount = 6164 ,
50+ focusAlleleFrequency = 0.000162232 ,
51+ qualityMeasures = {"qcFilters" : ["LowQual" , "NO_HQ_GENOTYPES" ]},
52+ cohort = StudyGroup (name = "rare disease" ), # type: ignore
53+ ) # type: ignore
3654
37- rows = return_cafs (postgres_storage )
38- assert len (rows ) == 1
55+ postgres_storage .add_allele_frequencies (caf )
3956
40- db_record = rows [0 ]
4157
42- assert db_record .vrs_id == "ga4gh:VA.J3Hi64dkKFKdnKIwB2419Qz3STDB2sJq"
43- assert db_record .cohort == "rare disease"
44- assert db_record .an == 6164
45- assert db_record .ac_het == 1
46- assert db_record .ac_hom == 0
47- assert db_record .filter == ["LowQual" , "NO_HQ_GENOTYPES" ]
48-
49- # first insert
50- insert_and_verify_one_row ()
51-
52- # second insert
53- insert_and_verify_one_row ()
58+ def test_add_allele_frequencies_failures (
59+ postgres_storage : PostgresObjectStore ,
60+ caf_empty_cohort : CohortAlleleFrequencyStudyResult ,
61+ ):
62+ """Test that add_allele_frequencies method fails correctly on bad input"""
63+ with pytest .raises (IntegrityError , match = 'null value in column "cohort"' ):
64+ postgres_storage .add_allele_frequencies (caf_empty_cohort )
0 commit comments