Skip to content

Commit 5f963b1

Browse files
authored
Merge pull request #428 from vladistan/convert-test-schema-as-dict-to-pytest
Convert test schema as dict to pytest
2 parents e2c3232 + 1f99539 commit 5f963b1

File tree

1 file changed

+55
-53
lines changed

1 file changed

+55
-53
lines changed
Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,76 @@
11
import logging
22
import os
3-
import unittest
3+
4+
import pytest
45

56
from linkml_runtime.linkml_model.meta import ClassDefinition
6-
from linkml_runtime.loaders.yaml_loader import YAMLLoader
77
from linkml_runtime.utils.schema_as_dict import schema_as_dict, schema_as_yaml_dump
88
from linkml_runtime.utils.schema_builder import ClassDefinition, SchemaBuilder, SlotDefinition
99
from linkml_runtime.utils.schemaview import SchemaView
1010
from tests.test_utils import INPUT_DIR, OUTPUT_DIR
1111

1212
logger = logging.getLogger(__name__)
1313

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")
1714

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+
1920

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")
2025

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"])
4726

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"]
5252

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)
6153

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+
"""
6558

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)
6867

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()
7171

72+
# Convert the schema to a dict
73+
obj = schema_as_dict(view.schema)
7274

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

Comments
 (0)