Mercurial > repos > guerler > springsuite
comparison planemo/lib/python3.7/site-packages/prov/tests/test_rdf.py @ 1:56ad4e20f292 draft
"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author | guerler |
---|---|
date | Fri, 31 Jul 2020 00:32:28 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:d30785e31577 | 1:56ad4e20f292 |
---|---|
1 from __future__ import (absolute_import, division, print_function, | |
2 unicode_literals) | |
3 | |
4 import unittest | |
5 from prov.model import ProvDocument | |
6 from prov.tests.utility import RoundTripTestCase | |
7 from prov.tests.test_model import (TestStatementsBase, | |
8 TestAttributesBase, TestQualifiedNamesBase) | |
9 import os | |
10 from glob import glob | |
11 import logging | |
12 logger = logging.getLogger(__name__) | |
13 | |
14 from prov.tests import examples | |
15 import prov.model as pm | |
16 | |
17 import rdflib as rl | |
18 from rdflib.compare import graph_diff | |
19 from io import BytesIO, StringIO | |
20 | |
21 | |
22 def find_diff(g_rdf, g0_rdf): | |
23 graphs_equal = True | |
24 in_both, in_first, in_second = graph_diff(g_rdf, g0_rdf) | |
25 g1 = sorted(in_first.serialize(format='nt').splitlines())[1:] | |
26 g2 = sorted(in_second.serialize(format='nt').splitlines())[1:] | |
27 # Compare literals | |
28 if len(g1) != len(g2): | |
29 graphs_equal = False | |
30 matching_indices = [[], []] | |
31 for idx in range(len(g1)): | |
32 g1_stmt = list(rl.ConjunctiveGraph().parse(BytesIO(g1[idx]), | |
33 format='nt'))[0] | |
34 match_found = False | |
35 for idx2 in range(len(g2)): | |
36 if idx2 in matching_indices[1]: | |
37 continue | |
38 g2_stmt = list(rl.ConjunctiveGraph().parse(BytesIO(g2[idx2]), | |
39 format='nt'))[0] | |
40 try: | |
41 all_match = all([g1_stmt[i].eq(g2_stmt[i]) for i in range(3)]) | |
42 except TypeError as e: | |
43 #logger.info(e, g1_stmt, g2_stmt) | |
44 all_match = False | |
45 if all_match: | |
46 matching_indices[0].append(idx) | |
47 matching_indices[1].append(idx2) | |
48 match_found = True | |
49 break | |
50 if not match_found: | |
51 graphs_equal = False | |
52 in_first2 = rl.ConjunctiveGraph() | |
53 for idx in range(len(g1)): | |
54 if idx in matching_indices[0]: | |
55 in_both.parse(BytesIO(g1[idx]), format='nt') | |
56 else: | |
57 in_first2.parse(BytesIO(g1[idx]), format='nt') | |
58 in_second2 = rl.ConjunctiveGraph() | |
59 for idx in range(len(g2)): | |
60 if not idx in matching_indices[1]: | |
61 in_second2.parse(BytesIO(g2[idx]), format='nt') | |
62 #logger.info(in_first2) | |
63 #logger.info(in_second2) | |
64 return graphs_equal, in_both, in_first2, in_second2 | |
65 | |
66 | |
67 class TestExamplesBase(object): | |
68 """This is the base class for testing support for all the examples provided | |
69 in prov.tests.examples. | |
70 It is not runnable and needs to be included in a subclass of | |
71 RoundTripTestCase. | |
72 """ | |
73 def test_all_examples(self): | |
74 counter = 0 | |
75 for name, graph in examples.tests: | |
76 if name in ['datatypes']: | |
77 logger.info('%d. Skipping the %s example', counter, name) | |
78 continue | |
79 counter += 1 | |
80 logger.info('%d. Testing the %s example', counter, name) | |
81 g = graph() | |
82 self.do_tests(g) | |
83 | |
84 | |
85 class TestJSONExamplesBase(object): | |
86 """This is the base class for testing support for all the examples provided | |
87 in prov.tests.examples. | |
88 It is not runnable and needs to be included in a subclass of | |
89 RoundTripTestCase. | |
90 """ | |
91 def test_all_examples(self): | |
92 counter = 0 | |
93 for name, graph in examples.tests: | |
94 if name in ['datatypes']: | |
95 logger.info('%d. Skipping the %s example', counter, name) | |
96 continue | |
97 counter += 1 | |
98 logger.info('%d. Testing the %s example', counter, name) | |
99 g = graph() | |
100 self.do_tests(g) | |
101 | |
102 | |
103 class TestStatementsBase2(TestStatementsBase): | |
104 @unittest.expectedFailure | |
105 def test_scruffy_end_1(self): | |
106 TestStatementsBase.test_scruffy_end_1() | |
107 @unittest.expectedFailure | |
108 def test_scruffy_end_2(self): | |
109 TestStatementsBase.test_scruffy_end_2() | |
110 @unittest.expectedFailure | |
111 def test_scruffy_end_3(self): | |
112 TestStatementsBase.test_scruffy_end_3() | |
113 @unittest.expectedFailure | |
114 def test_scruffy_end_4(self): | |
115 TestStatementsBase.test_scruffy_end_4() | |
116 @unittest.expectedFailure | |
117 def test_scruffy_generation_1(self): | |
118 TestStatementsBase.test_scruffy_generation_1() | |
119 @unittest.expectedFailure | |
120 def test_scruffy_generation_2(self): | |
121 TestStatementsBase.test_scruffy_generation_2() | |
122 @unittest.expectedFailure | |
123 def test_scruffy_invalidation_1(self): | |
124 TestStatementsBase.test_scruffy_invalidation_1() | |
125 @unittest.expectedFailure | |
126 def test_scruffy_invalidation_2(self): | |
127 TestStatementsBase.test_scruffy_invalidation_2() | |
128 @unittest.expectedFailure | |
129 def test_scruffy_start_1(self): | |
130 TestStatementsBase.test_scruffy_start_1() | |
131 @unittest.expectedFailure | |
132 def test_scruffy_start_2(self): | |
133 TestStatementsBase.test_scruffy_start_2() | |
134 @unittest.expectedFailure | |
135 def test_scruffy_start_3(self): | |
136 TestStatementsBase.test_scruffy_start_3() | |
137 @unittest.expectedFailure | |
138 def test_scruffy_start_4(self): | |
139 TestStatementsBase.test_scruffy_start_4() | |
140 @unittest.expectedFailure | |
141 def test_scruffy_usage_1(self): | |
142 TestStatementsBase.test_scruffy_usage_1() | |
143 @unittest.expectedFailure | |
144 def test_scruffy_usage_2(self): | |
145 TestStatementsBase.test_scruffy_usage_2() | |
146 | |
147 | |
148 class TestAttributesBase2(TestAttributesBase): | |
149 @unittest.expectedFailure | |
150 def test_entity_with_multiple_attribute(self): | |
151 TestAttributesBase.test_entity_with_multiple_attribute() | |
152 @unittest.expectedFailure | |
153 def test_entity_with_multiple_value_attribute(self): | |
154 TestAttributesBase.test_entity_with_multiple_value_attribute() | |
155 @unittest.expectedFailure | |
156 def test_entity_with_one_type_attribute_8(self): | |
157 TestAttributesBase.test_entity_with_one_type_attribute_8() | |
158 | |
159 | |
160 class AllTestsBase(TestExamplesBase, | |
161 TestStatementsBase2, | |
162 TestQualifiedNamesBase, | |
163 TestAttributesBase2 | |
164 ): | |
165 """This is a test to include all available tests. | |
166 """ | |
167 pass | |
168 | |
169 | |
170 class TestRDFSerializer(unittest.TestCase): | |
171 def test_decoding_unicode_value(self): | |
172 unicode_char = u'\u2019' | |
173 rdf_content = u''' | |
174 @prefix ex: <http://www.example.org/> . | |
175 @prefix prov: <http://www.w3.org/ns/prov#> . | |
176 @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . | |
177 @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . | |
178 @prefix xml: <http://www.w3.org/XML/1998/namespace> . | |
179 @prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | |
180 | |
181 ex:unicode_char a prov:Entity ; | |
182 rdfs:label "%s"^^xsd:string . | |
183 ''' % unicode_char | |
184 prov_doc = ProvDocument.deserialize(content=rdf_content, | |
185 format='rdf', rdf_format='turtle') | |
186 e1 = prov_doc.get_record('ex:unicode_char')[0] | |
187 self.assertIn(unicode_char, e1.get_attribute('prov:label')) | |
188 | |
189 def test_json_to_ttl_match(self): | |
190 json_files = sorted( | |
191 glob(os.path.join(os.path.dirname(__file__), 'json', '*.json'))) | |
192 | |
193 # invalid round trip files | |
194 skip = list(range(352, 380)) | |
195 | |
196 # invalid literal set representation e.g., set((1, True)) | |
197 skip_match = [5, 6, 7, 8, 15, 27, 28, 29, 75, 76, 77, 78, 79, 80, 260, | |
198 261, 262, 263, 264, | |
199 306, 313, 315, 317, 322, 323, 324, 325, 330, 332, 344, | |
200 346, 382, 389, 395, 397, | |
201 ] | |
202 errors = [] | |
203 for idx, fname in enumerate(json_files): | |
204 _, ttl_file = os.path.split(fname) | |
205 ttl_file = os.path.join(os.path.dirname(__file__), 'rdf', | |
206 ttl_file.replace('json', 'ttl')) | |
207 try: | |
208 g = pm.ProvDocument.deserialize(fname) | |
209 if len(g.bundles) == 0: | |
210 format = 'turtle' | |
211 else: | |
212 format = 'trig' | |
213 if format == 'trig': | |
214 ttl_file = ttl_file.replace('ttl', 'trig') | |
215 | |
216 with open(ttl_file, 'rb') as fp: | |
217 g_rdf = rl.ConjunctiveGraph().parse(fp, format=format) | |
218 g0_rdf = rl.ConjunctiveGraph().parse( | |
219 StringIO(g.serialize(format='rdf', rdf_format=format)), | |
220 format=format) | |
221 if idx not in skip_match: | |
222 match, _, in_first, in_second = find_diff(g_rdf, g0_rdf) | |
223 self.assertTrue(match) | |
224 else: | |
225 logger.info('Skipping match: %s' % fname) | |
226 if idx in skip: | |
227 logger.info('Skipping deserialization: %s' % fname) | |
228 continue | |
229 g1 = pm.ProvDocument.deserialize( | |
230 content=g.serialize(format='rdf', rdf_format=format), | |
231 format='rdf', rdf_format=format) | |
232 except Exception as e: | |
233 #logger.info(e) | |
234 errors.append((e, idx, fname, in_first, in_second)) | |
235 self.assertFalse(errors) | |
236 | |
237 | |
238 class RoundTripRDFTests(RoundTripTestCase, AllTestsBase): | |
239 FORMAT = 'rdf' | |
240 | |
241 if __name__ == "__main__": | |
242 suite = unittest.TestSuite() | |
243 for method in dir(TestRDFSerializer): | |
244 if method.startswith("test"): | |
245 suite.addTest(TestRDFSerializer(method)) | |
246 unittest.TextTestRunner().run(suite) |