diff --git a/src/ga4gh/cat_vrs/recipes.py b/src/ga4gh/cat_vrs/recipes.py index 52374e7..bc3e62a 100644 --- a/src/ga4gh/cat_vrs/recipes.py +++ b/src/ga4gh/cat_vrs/recipes.py @@ -180,7 +180,8 @@ def validate_constraints(cls, v: list[Constraint]) -> list[Constraint]: ): def_loc_constr_found = True - for r in constraint.relations: + relations = constraint.relations or [] + for r in relations: if r.primaryCoding and ( r.primaryCoding.code.root == Relation.LIFTOVER_TO.value and r.primaryCoding.system diff --git a/tests/validation/test_cat_vrs_models.py b/tests/validation/test_cat_vrs_models.py index 87c5e4a..7b7f3e7 100644 --- a/tests/validation/test_cat_vrs_models.py +++ b/tests/validation/test_cat_vrs_models.py @@ -366,7 +366,11 @@ def test_canonical_allele(defining_loc_constr, members_and_name): recipes.CanonicalAllele(**valid_params) -def test_categorical_cnv(members_and_name, defining_loc_constr, copy_change_constr): +def test_categorical_cnv( + members_and_name: dict, + defining_loc_constr: models.DefiningLocationConstraint, + copy_change_constr: models.CopyChangeConstraint, +): """Test the CategoricalCnv validator""" # Valid CategoricalCnv with CopyChangeConstraint valid_params = deepcopy(members_and_name) @@ -384,6 +388,20 @@ def test_categorical_cnv(members_and_name, defining_loc_constr, copy_change_cons ] assert recipes.CategoricalCnv(**valid_params) + # Invalid CategoricalCnv: relations unset on DLC + invalid_params = deepcopy(members_and_name) + invalid_dlc = deepcopy(defining_loc_constr) + invalid_dlc.relations = None + invalid_params["constraints"] = [ + models.Constraint(root=invalid_dlc), + models.Constraint(root=models.CopyCountConstraint(copies=[1, 2])), + ] + with pytest.raises( + ValueError, + match="`DefiningLocationConstraint` found, but must contain at least one relation where `primaryCoding.code` is 'liftover_to' and `primaryCoding.system` is 'ga4gh-gks-term:allele-relation'.", + ): + recipes.CategoricalCnv(**invalid_params) + # Invalid CategoricalCnv: No DefiningLocationConstraint invalid_params = deepcopy(members_and_name) invalid_params["constraints"] = [