|
1 | | -import os |
2 | | -import unittest |
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
3 | 4 |
|
4 | 5 | from linkml_runtime.dumpers import yaml_dumper |
5 | | -from linkml_runtime.loaders.yaml_loader import YAMLLoader |
6 | 6 | from linkml_runtime.utils.ruleutils import get_range_as_disjunction, subclass_to_rules |
7 | 7 | from linkml_runtime.utils.schemaview import SchemaView |
8 | 8 | from tests.test_utils import INPUT_DIR |
9 | 9 |
|
10 | | -SCHEMA = os.path.join(INPUT_DIR, "rules-example.yaml") |
11 | | - |
12 | | -yaml_loader = YAMLLoader() |
13 | | - |
14 | | - |
15 | | -class RuleUtilsTestCase(unittest.TestCase): |
16 | | - def test_disjunction(self): |
17 | | - # no import schema |
18 | | - view = SchemaView(SCHEMA) |
19 | | - analyte = view.induced_slot("analyte", "Sample") |
20 | | - # print(analyte) |
21 | | - # print(analyte.any_of) |
22 | | - disj = get_range_as_disjunction(analyte) |
23 | | - # print(disj) |
24 | | - self.assertCountEqual(disj, {"MissingValueEnum", "AnalyteEnum"}) |
25 | | - for s in view.all_slots().values(): |
26 | | - disj = get_range_as_disjunction(s) |
27 | | - print(f"{s.name} DISJ: {disj}") |
28 | | - |
29 | | - def test_roll_up(self): |
30 | | - # no import schema |
31 | | - view = SchemaView(SCHEMA) |
32 | | - c = view.get_class("ProteinCodingGene") |
33 | | - rules = subclass_to_rules(view, "ProteinCodingGene", "SeqFeature") |
34 | | - rule = rules[0] |
35 | | - print(f"IF: {rule.preconditions}") |
36 | | - print(f"THEN: {rule.postconditions}") |
37 | | - print(yaml_dumper.dumps(rule)) |
38 | | - |
39 | | - |
40 | | -if __name__ == "__main__": |
41 | | - unittest.main() |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def rules_schema(): |
| 13 | + """SchemaView loaded with rules example schema.""" |
| 14 | + return SchemaView(Path(INPUT_DIR) / "rules-example.yaml") |
| 15 | + |
| 16 | + |
| 17 | +def test_disjunction(rules_schema): |
| 18 | + """Test get_range_as_disjunction with schema view fixture.""" |
| 19 | + analyte = rules_schema.induced_slot("analyte", "Sample") |
| 20 | + # print(analyte) |
| 21 | + # print(analyte.any_of) |
| 22 | + disj = get_range_as_disjunction(analyte) |
| 23 | + # print(disj) |
| 24 | + assert sorted(disj) == sorted({"MissingValueEnum", "AnalyteEnum"}) |
| 25 | + |
| 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}") |
| 30 | + |
| 31 | + |
| 32 | +def test_roll_up(rules_schema): |
| 33 | + """Test subclass_to_rules with schema view fixture.""" |
| 34 | + c = rules_schema.get_class("ProteinCodingGene") |
| 35 | + rules = subclass_to_rules(rules_schema, "ProteinCodingGene", "SeqFeature") |
| 36 | + rule = rules[0] |
| 37 | + print(f"IF: {rule.preconditions}") |
| 38 | + print(f"THEN: {rule.postconditions}") |
| 39 | + print(yaml_dumper.dumps(rule)) |
0 commit comments