22
33import pytest
44
5- from linkml_runtime .dumpers import yaml_dumper
65from linkml_runtime .utils .ruleutils import get_range_as_disjunction , subclass_to_rules
76from linkml_runtime .utils .schemaview import SchemaView
87from tests .test_utils import INPUT_DIR
@@ -14,26 +13,104 @@ def rules_schema():
1413 return SchemaView (Path (INPUT_DIR ) / "rules-example.yaml" )
1514
1615
17- def test_disjunction (rules_schema ):
18- """Test get_range_as_disjunction with schema view fixture ."""
16+ def test_disjunction_analyte_slot (rules_schema ):
17+ """Test get_range_as_disjunction for analyte slot with any_of ."""
1918 analyte = rules_schema .induced_slot ("analyte" , "Sample" )
20- # print(analyte)
21- # print(analyte.any_of)
2219 disj = get_range_as_disjunction (analyte )
23- # print(disj)
24- assert sorted (disj ) == sorted ({"MissingValueEnum" , "AnalyteEnum" })
20+ assert disj == {"MissingValueEnum" , "AnalyteEnum" }
2521
26- # Test all slots for debugging
27- for s in rules_schema .all_slots ().values ():
28- disj = get_range_as_disjunction (s )
29- print (f"{ s .name } DISJ: { disj } " )
3022
23+ @pytest .mark .parametrize (
24+ "slot_name,expected_disjunction" ,
25+ [
26+ ("vital_status" , {"MissingValueEnum" , "VitalStatusEnum" }), # any_of with two enums
27+ ("primary_address" , {"Address" }), # explicit range to class
28+ ("age" , {"int" }), # int range
29+ ("encodes" , {"SeqFeature" }), # class range
30+ ("id" , None ), # no explicit range specified
31+ ("name" , None ), # no explicit range specified
32+ ("telephone" , None ), # no explicit range specified
33+ ],
34+ )
35+ def test_disjunction_various_slots (rules_schema , slot_name , expected_disjunction ):
36+ """Test get_range_as_disjunction for various slot types."""
37+ slot = rules_schema .get_slot (slot_name )
38+ disj = get_range_as_disjunction (slot )
39+ assert disj == expected_disjunction
3140
32- def test_roll_up ( rules_schema ):
33- """Test subclass_to_rules with schema view fixture."""
34- c = rules_schema . get_class ( "ProteinCodingGene" )
41+
42+ def test_roll_up_protein_coding_gene ( rules_schema ):
43+ """Test subclass_to_rules for ProteinCodingGene to SeqFeature."""
3544 rules = subclass_to_rules (rules_schema , "ProteinCodingGene" , "SeqFeature" )
45+
46+ # Should generate one rule
47+ assert len (rules ) == 1
48+
49+ rule = rules [0 ]
50+
51+ # Check preconditions - uses type designator slot
52+ assert rule .preconditions is not None
53+ assert rule .preconditions .slot_conditions is not None
54+ assert "type" in rule .preconditions .slot_conditions
55+
56+ # Check the type slot condition
57+ type_condition = rule .preconditions .slot_conditions ["type" ]
58+ assert type_condition .equals_string == "ProteinCodingGene"
59+
60+ # Check postconditions - should have slot assignments
61+ assert rule .postconditions is not None
62+ assert rule .postconditions .slot_conditions is not None
63+ # Should include slots from the class
64+ assert "encodes" in rule .postconditions .slot_conditions
65+ assert "id" in rule .postconditions .slot_conditions
66+
67+
68+ def test_roll_up_noncoding_gene (rules_schema ):
69+ """Test subclass_to_rules for NoncodingGene to SeqFeature."""
70+ rules = subclass_to_rules (rules_schema , "NoncodingGene" , "SeqFeature" )
71+
72+ # Should generate one rule
73+ assert len (rules ) == 1
74+
3675 rule = rules [0 ]
37- print (f"IF: { rule .preconditions } " )
38- print (f"THEN: { rule .postconditions } " )
39- print (yaml_dumper .dumps (rule ))
76+
77+ # Check preconditions - uses type designator slot
78+ assert rule .preconditions is not None
79+ assert rule .preconditions .slot_conditions is not None
80+ assert "type" in rule .preconditions .slot_conditions
81+
82+ # Check the type slot condition
83+ type_condition = rule .preconditions .slot_conditions ["type" ]
84+ assert type_condition .equals_string == "NoncodingGene"
85+
86+ # Check postconditions - should have slot assignments
87+ assert rule .postconditions is not None
88+ assert rule .postconditions .slot_conditions is not None
89+ assert "encodes" in rule .postconditions .slot_conditions
90+ assert "id" in rule .postconditions .slot_conditions
91+
92+
93+ def test_roll_up_genomic_sample (rules_schema ):
94+ """Test subclass_to_rules for GenomicSample to Sample."""
95+ rules = subclass_to_rules (rules_schema , "GenomicSample" , "Sample" )
96+
97+ # Should generate one rule
98+ assert len (rules ) == 1
99+
100+ rule = rules [0 ]
101+
102+ # Check preconditions - uses type designator slot
103+ assert rule .preconditions is not None
104+ assert rule .preconditions .slot_conditions is not None
105+ assert "type" in rule .preconditions .slot_conditions
106+
107+ # Check the type slot condition
108+ type_condition = rule .preconditions .slot_conditions ["type" ]
109+ assert type_condition .equals_string == "GenomicSample"
110+
111+ # Check postconditions - should have slot assignments
112+ assert rule .postconditions is not None
113+ assert rule .postconditions .slot_conditions is not None
114+ # Should include slots from the class
115+ assert "id" in rule .postconditions .slot_conditions
116+ assert "analyte" in rule .postconditions .slot_conditions
0 commit comments