|
1 | 1 | import logging |
2 | 2 | import os |
3 | | -import unittest |
| 3 | + |
| 4 | +import pytest |
4 | 5 |
|
5 | 6 | from linkml_runtime.linkml_model.meta import ClassDefinition |
6 | | -from linkml_runtime.loaders.yaml_loader import YAMLLoader |
7 | 7 | from linkml_runtime.utils.schema_as_dict import schema_as_dict, schema_as_yaml_dump |
8 | 8 | from linkml_runtime.utils.schema_builder import ClassDefinition, SchemaBuilder, SlotDefinition |
9 | 9 | from linkml_runtime.utils.schemaview import SchemaView |
10 | 10 | from tests.test_utils import INPUT_DIR, OUTPUT_DIR |
11 | 11 |
|
12 | 12 | logger = logging.getLogger(__name__) |
13 | 13 |
|
14 | | -SCHEMA_NO_IMPORTS = os.path.join(INPUT_DIR, "kitchen_sink_noimports.yaml") |
15 | | -SCHEMA_WITH_IMPORTS = os.path.join(INPUT_DIR, "kitchen_sink.yaml") |
16 | | -CLEAN_SCHEMA = os.path.join(OUTPUT_DIR, "kitchen_sink.clean.yaml") |
17 | 14 |
|
18 | | -yaml_loader = YAMLLoader() |
| 15 | +@pytest.fixture |
| 16 | +def schema_no_imports_path(): |
| 17 | + """Path to kitchen sink schema without imports.""" |
| 18 | + return os.path.join(INPUT_DIR, "kitchen_sink_noimports.yaml") |
| 19 | + |
19 | 20 |
|
| 21 | +@pytest.fixture |
| 22 | +def clean_output_path(): |
| 23 | + """Path for clean output schema file.""" |
| 24 | + return os.path.join(OUTPUT_DIR, "kitchen_sink.clean.yaml") |
20 | 25 |
|
21 | | -class SchemaAsDictTestCase(unittest.TestCase): |
22 | | - def test_as_dict(self): |
23 | | - """ |
24 | | - tests schema_as_dict, see https://github.com/linkml/linkml/issues/100 |
25 | | - """ |
26 | | - view = SchemaView(SCHEMA_NO_IMPORTS) |
27 | | - all_slots = view.all_slots() |
28 | | - self.assertIn("name", all_slots) |
29 | | - logger.debug(view.schema.id) |
30 | | - ystr = schema_as_yaml_dump(view.schema) |
31 | | - with open(CLEAN_SCHEMA, "w") as stream: |
32 | | - stream.write(ystr) |
33 | | - view2 = SchemaView(ystr) |
34 | | - obj = schema_as_dict(view.schema) |
35 | | - # ensure that prefixes are compacted |
36 | | - assert obj["prefixes"]["pav"] == "http://purl.org/pav/" |
37 | | - assert "@type" not in obj |
38 | | - for k in ["slots", "classes", "enums", "subsets"]: |
39 | | - elt_dict = obj[k] |
40 | | - for e_name, e in elt_dict.items(): |
41 | | - assert "name" not in e |
42 | | - if k == "enums": |
43 | | - for e in elt_dict.values(): |
44 | | - for pv in e.get("permissible_values", {}).values(): |
45 | | - assert "text" not in pv |
46 | | - self.assertIn("name", obj["slots"]) |
47 | 26 |
|
48 | | - def test_as_dict_with_attributes(self): |
49 | | - """ |
50 | | - tests schema_as_dict, see https://github.com/linkml/linkml/issues/100 |
51 | | - """ |
| 27 | +def test_as_dict(schema_no_imports_path, clean_output_path): |
| 28 | + """ |
| 29 | + tests schema_as_dict, see https://github.com/linkml/linkml/issues/100 |
| 30 | + """ |
| 31 | + view = SchemaView(schema_no_imports_path) |
| 32 | + all_slots = view.all_slots() |
| 33 | + assert "name" in all_slots |
| 34 | + logger.debug(view.schema.id) |
| 35 | + ystr = schema_as_yaml_dump(view.schema) |
| 36 | + with open(clean_output_path, "w") as stream: |
| 37 | + stream.write(ystr) |
| 38 | + view2 = SchemaView(ystr) |
| 39 | + obj = schema_as_dict(view.schema) |
| 40 | + # ensure that prefixes are compacted |
| 41 | + assert obj["prefixes"]["pav"] == "http://purl.org/pav/" |
| 42 | + assert "@type" not in obj |
| 43 | + for k in ["slots", "classes", "enums", "subsets"]: |
| 44 | + elt_dict = obj[k] |
| 45 | + for e_name, e in elt_dict.items(): |
| 46 | + assert "name" not in e |
| 47 | + if k == "enums": |
| 48 | + for e in elt_dict.values(): |
| 49 | + for pv in e.get("permissible_values", {}).values(): |
| 50 | + assert "text" not in pv |
| 51 | + assert "name" in obj["slots"] |
52 | 52 |
|
53 | | - # Create a class with an attribute named 'name' |
54 | | - cls = ClassDefinition(name="Patient") |
55 | | - slots = [ |
56 | | - SlotDefinition(name="id", range="string"), |
57 | | - SlotDefinition(name="name", range="string"), |
58 | | - ] |
59 | | - builder = SchemaBuilder() |
60 | | - builder.add_class(cls=cls, slots=slots, use_attributes=True) |
61 | 53 |
|
62 | | - # Verify that the 'name' slot exists in the schema |
63 | | - view = SchemaView(builder.schema) |
64 | | - self.assertIn("name", view.all_slots()) |
| 54 | +def test_as_dict_with_attributes(): |
| 55 | + """ |
| 56 | + tests schema_as_dict, see https://github.com/linkml/linkml/issues/100 |
| 57 | + """ |
65 | 58 |
|
66 | | - # Convert the schema to a dict |
67 | | - obj = schema_as_dict(view.schema) |
| 59 | + # Create a class with an attribute named 'name' |
| 60 | + cls = ClassDefinition(name="Patient") |
| 61 | + slots = [ |
| 62 | + SlotDefinition(name="id", range="string"), |
| 63 | + SlotDefinition(name="name", range="string"), |
| 64 | + ] |
| 65 | + builder = SchemaBuilder() |
| 66 | + builder.add_class(cls=cls, slots=slots, use_attributes=True) |
68 | 67 |
|
69 | | - # Verify that the 'name' slot still exists, as an attribute |
70 | | - self.assertIn("name", obj["classes"]["Patient"]["attributes"]) |
| 68 | + # Verify that the 'name' slot exists in the schema |
| 69 | + view = SchemaView(builder.schema) |
| 70 | + assert "name" in view.all_slots() |
71 | 71 |
|
| 72 | + # Convert the schema to a dict |
| 73 | + obj = schema_as_dict(view.schema) |
72 | 74 |
|
73 | | -if __name__ == "__main__": |
74 | | - unittest.main() |
| 75 | + # Verify that the 'name' slot still exists, as an attribute |
| 76 | + assert "name" in obj["classes"]["Patient"]["attributes"] |
0 commit comments