test_owl2yams.py 2.41 KB
Newer Older
Noé Gaumont's avatar
Noé Gaumont committed
1
2
import unittest
import pathlib
3
from rdflib import Graph
4
from typing import Optional
5
6
from owl2yams import owl_model_to_yams
from yams.serialize import serialize_to_python
Noé Gaumont's avatar
Noé Gaumont committed
7
8
9
10
11

TEST_DATA = pathlib.Path(__file__).parent / "data"


class Owl2YamsTC(unittest.TestCase):
12
    def _load_owl_and_yams(
13
14
15
16
        self,
        owl_model_file_name: str,
        yams_schema_file_name: Optional[str] = None,
        check_cw_uri: bool = False,
17
18
    ):
        data_input = TEST_DATA / owl_model_file_name
19
        owl_model = Graph()
20
        owl_model.parse(str(data_input), format="turtle")
21
22
23

        schema = owl_model_to_yams(owl_model, "TEST")

24
25
26
        if not check_cw_uri:
            schema.del_entity_type("CWUri")

27
28
29
30
31
32
33
        if yams_schema_file_name is not None:
            expected_output = TEST_DATA / yams_schema_file_name
            with open(expected_output) as f:
                expected_yams_schema = f.read()
            self.assertEquals(serialize_to_python(schema), expected_yams_schema)
        return schema

Fabien Amarger's avatar
Fabien Amarger committed
34
35
    def test_transform_class(self):
        self._load_owl_and_yams("test_class.owl", "test_class.yams")
Noé Gaumont's avatar
Noé Gaumont committed
36

37
38
39
    def test_transform_attribute(self):
        self._load_owl_and_yams("test_attribute.owl", "test_attribute.yams")

Fabien Amarger's avatar
Fabien Amarger committed
40
41
42
    def test_transform_relation(self):
        self._load_owl_and_yams("test_relation.owl", "test_relation.yams")

43
44
45
    def test_transform_hierarchy(self):
        self._load_owl_and_yams("test_hierarchy.owl", "test_hierarchy.yams")

46
47
48
49
50
51
    def test_transform_relation_without_domain_range(self):
        self._load_owl_and_yams(
            "test_relation_without_domain_range.owl",
            "test_relation_without_domain_range.yams",
        )

52
53
54
55
56
57
    def test_transform_attribute_owl_thing(self):
        self._load_owl_and_yams(
            "test_attribute_owl_thing.owl",
            "test_attribute_owl_thing.yams",
        )

58
59
60
61
    def test_raise_multi_class(self):
        with self.assertRaises(ValueError):
            self._load_owl_and_yams("test_multi_class.owl")

62
63
64
65
    def test_raise_multi_attribute(self):
        with self.assertRaises(ValueError):
            self._load_owl_and_yams("test_multi_attribute.owl")

66
67
68
69
    def test_raise_multi_relation(self):
        with self.assertRaises(ValueError):
            self._load_owl_and_yams("test_multi_relation.owl")

70
71
72
    def test_cwuri(self):
        self._load_owl_and_yams("test_cwuri.owl", "test_cwuri.yams", check_cw_uri=True)

Noé Gaumont's avatar
Noé Gaumont committed
73
74
75

if __name__ == "__main__":
    unittest.main()