-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.py
147 lines (122 loc) · 4.35 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
"""
Tests for the XSLT conversion. Checks for compliance of each of the cases
in the tests folder
"""
import unittest
from rdflib import Graph, BNode, URIRef
import lxml.etree as ET
import io
import re
def convert_tei_to_ontolex(tei_file_name, xsl_filename="Stylesheet/TEI2Ontolex.xsl"):
dom = ET.parse(tei_file_name)
xslt = ET.parse(xsl_filename)
transform = ET.XSLT(xslt)
newdom = transform(dom)
g = Graph()
with io.BytesIO(ET.tostring(newdom)) as out:
g.parse(out)
return g
def report(missing, overgen):
s = ""
if len(missing) > 0:
s += "\nMissing:\n"
s += "\n".join(str(m) for m in missing)
s += "\n"
if len(overgen) > 0:
s += "Incorrect:\n"
s += "\n".join(str(m) for m in overgen)
return s
def test_graph(g):
res = set()
for s,p,o in g:
if isinstance(s, BNode):
s = "[]"
elif isinstance(s, URIRef):
s = re.sub("file:.*#", "#", s.n3())
else:
s = s.n3()
p = p.n3()
if isinstance(o, BNode):
o = "[]"
elif isinstance(o, URIRef):
o = re.sub("file:.*#", "#", o.n3())
else:
o = o.n3()
res.add((s,p,o))
return res
def compare_rdf_graphs(expected_graph, actual_graph, test_case):
g1 = test_graph(expected_graph)
g2 = test_graph(actual_graph)
missing = g1 - g2
overgen = g2 - g1
test_case.assertTrue(len(missing) + len(overgen) == 0,
report(missing, overgen))
class TestTEI2OntoLex(unittest.TestCase):
def test1(self):
expected = Graph()
with open("tests/test1.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test1.xml")
compare_rdf_graphs(expected, actual, self)
def test2(self):
expected = Graph()
with open("tests/test2.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test2.xml")
compare_rdf_graphs(expected, actual, self)
def test3(self):
expected = Graph()
with open("tests/test3.ttl", encoding="utf-8") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test3.xml")
compare_rdf_graphs(expected, actual, self)
def test4(self):
expected = Graph()
with open("tests/test4.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test4.xml")
compare_rdf_graphs(expected, actual, self)
def test5(self):
expected = Graph()
with open("tests/test5.ttl", encoding="utf-8") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test5.xml")
compare_rdf_graphs(expected, actual, self)
def test6(self):
expected = Graph()
with open("tests/test6.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test6.xml")
compare_rdf_graphs(expected, actual, self)
def test7(self):
expected = Graph()
with open("tests/test7.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test7.xml")
compare_rdf_graphs(expected, actual, self)
def test8(self):
expected = Graph()
with open("tests/test8.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test8.xml")
compare_rdf_graphs(expected, actual, self)
def test9(self):
expected = Graph()
with open("tests/test9.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test9.xml")
compare_rdf_graphs(expected, actual, self)
def test10(self):
expected = Graph()
with open("tests/test10.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test10.xml")
compare_rdf_graphs(expected, actual, self)
def test12(self):
expected = Graph()
with open("tests/test12.ttl") as ttl:
expected.parse(ttl, format="turtle")
actual = convert_tei_to_ontolex("tests/test12.xml")
compare_rdf_graphs(expected, actual, self)
if __name__ == "__main__":
unittest.main()