Add more tests for graph_diff · RDFLib/rdflib@a51580f (original) (raw)
1
``
`-
from test.utils import GraphHelper
`
2
``
`-
from typing import TYPE_CHECKING, Set
`
``
1
`+
from dataclasses import dataclass, field
`
``
2
`+
from test.utils import (
`
``
3
`+
COLLAPSED_BNODE,
`
``
4
`+
BNodeHandling,
`
``
5
`+
GHQuad,
`
``
6
`+
GHTriple,
`
``
7
`+
GraphHelper,
`
``
8
`+
MarksType,
`
``
9
`+
MarkType,
`
``
10
`+
)
`
``
11
`+
from typing import TYPE_CHECKING, Collection, Set, Tuple, Type, Union, cast
`
3
12
``
4
13
`import pytest
`
``
14
`+
from _pytest.mark.structures import ParameterSet
`
5
15
``
6
16
`import rdflib
`
7
17
`from rdflib import Graph
`
8
18
`from rdflib.compare import graph_diff
`
9
``
`-
from rdflib.namespace import FOAF, RDF
`
``
19
`+
from rdflib.graph import ConjunctiveGraph, Dataset
`
``
20
`+
from rdflib.namespace import FOAF, RDF, Namespace
`
10
21
`from rdflib.term import BNode, Literal
`
11
22
``
12
23
`if TYPE_CHECKING:
`
`@@ -15,7 +26,7 @@
`
15
26
`"""Test for graph_diff - much more extensive testing
`
16
27
`would certainly be possible"""
`
17
28
``
18
``
`-
_TripleSetT = Set["_TripleType"]
`
``
29
`+
_TripleSetType = Set["_TripleType"]
`
19
30
``
20
31
``
21
32
`class TestDiff:
`
`@@ -48,7 +59,7 @@ def test_subsets(self) -> None:
`
48
59
`` triples only in g0
, and that there are triples that occur in both
``
49
60
`` g0
and g1
, and that there are triples only in g1
.
``
50
61
` """
`
51
``
`-
g0_ts: _TripleSetT = set()
`
``
62
`+
g0_ts: _TripleSetType = set()
`
52
63
`bnode = BNode()
`
53
64
`g0_ts.update(
`
54
65
` {
`
`@@ -59,7 +70,7 @@ def test_subsets(self) -> None:
`
59
70
`g0 = Graph()
`
60
71
`g0 += g0_ts
`
61
72
``
62
``
`-
g1_ts: _TripleSetT = set()
`
``
73
`+
g1_ts: _TripleSetType = set()
`
63
74
`bnode = BNode()
`
64
75
`g1_ts.update(
`
65
76
` {
`
`@@ -76,3 +87,154 @@ def test_subsets(self) -> None:
`
76
87
`assert in_first == set()
`
77
88
`assert len(in_second) > 0
`
78
89
`assert len(in_both) > 0
`
``
90
+
``
91
+
``
92
`+
_ElementSetType = Union[Collection[GHTriple], Collection[GHQuad]]
`
``
93
+
``
94
`+
_ElementSetTypeOrStr = Union[_ElementSetType, str]
`
``
95
+
``
96
+
``
97
`+
@dataclass
`
``
98
`+
class GraphDiffCase:
`
``
99
`+
graph_type: Type[Graph]
`
``
100
`+
format: str
`
``
101
`+
lhs: str
`
``
102
`+
rhs: str
`
``
103
`+
expected_result: Tuple[
`
``
104
`+
_ElementSetTypeOrStr, _ElementSetTypeOrStr, _ElementSetTypeOrStr
`
``
105
`+
]
`
``
106
`+
marks: MarkType = field(default_factory=lambda: cast(MarksType, list()))
`
``
107
+
``
108
`+
def as_element_set(self, value: _ElementSetTypeOrStr) -> _ElementSetType:
`
``
109
`+
if isinstance(value, str):
`
``
110
`+
graph = self.graph_type()
`
``
111
`+
graph.parse(data=value, format=self.format)
`
``
112
`+
if isinstance(graph, ConjunctiveGraph):
`
``
113
`+
return GraphHelper.quad_set(graph, BNodeHandling.COLLAPSE)
`
``
114
`+
else:
`
``
115
`+
return GraphHelper.triple_set(graph, BNodeHandling.COLLAPSE)
`
``
116
`+
return value
`
``
117
+
``
118
`+
def expected_in_both_set(self) -> _ElementSetType:
`
``
119
`+
return self.as_element_set(self.expected_result[0])
`
``
120
+
``
121
`+
def expected_in_lhs_set(self) -> _ElementSetType:
`
``
122
`+
return self.as_element_set(self.expected_result[1])
`
``
123
+
``
124
`+
def expected_in_rhs_set(self) -> _ElementSetType:
`
``
125
`+
return self.as_element_set(self.expected_result[2])
`
``
126
+
``
127
`+
def as_params(self) -> ParameterSet:
`
``
128
`+
return pytest.param(self, marks=self.marks)
`
``
129
+
``
130
+
``
131
`+
EGSCHEME = Namespace("example:")
`
``
132
+
``
133
+
``
134
`+
@pytest.mark.parametrize(
`
``
135
`+
"test_case",
`
``
136
`+
[
`
``
137
`+
GraphDiffCase(
`
``
138
`+
Graph,
`
``
139
`+
format="turtle",
`
``
140
`+
lhs="""
`
``
141
`+
@prefix eg: example: .
`
``
142
`+
_:a _:b _:c .
`
``
143
`+
eg:o0 eg:p0 eg:s0 .
`
``
144
`+
eg:o1 eg:p1 eg:s1 .
`
``
145
`+
""",
`
``
146
`+
rhs="""
`
``
147
`+
@prefix eg: example: .
`
``
148
`+
eg:o0 eg:p0 eg:s0 .
`
``
149
`+
eg:o1 eg:p1 eg:s1 .
`
``
150
`+
""",
`
``
151
`+
expected_result=(
`
``
152
`+
"""
`
``
153
`+
@prefix eg: example: .
`
``
154
`+
eg:o0 eg:p0 eg:s0 .
`
``
155
`+
eg:o1 eg:p1 eg:s1 .
`
``
156
`+
""",
`
``
157
`+
{(COLLAPSED_BNODE, COLLAPSED_BNODE, COLLAPSED_BNODE)},
`
``
158
`+
"",
`
``
159
`+
),
`
``
160
`+
),
`
``
161
`+
GraphDiffCase(
`
``
162
`+
Graph,
`
``
163
`+
format="turtle",
`
``
164
`+
lhs="""
`
``
165
`+
@prefix eg: example: .
`
``
166
`+
eg:o0 eg:p0 eg:s0 .
`
``
167
`+
eg:o1 eg:p1 eg:s1 .
`
``
168
`+
""",
`
``
169
`+
rhs="""
`
``
170
`+
@prefix eg: example: .
`
``
171
`+
eg:o0 eg:p0 eg:s0 .
`
``
172
`+
eg:o1 eg:p1 eg:s1 .
`
``
173
`+
""",
`
``
174
`+
expected_result=(
`
``
175
`+
"""
`
``
176
`+
@prefix eg: example: .
`
``
177
`+
eg:o0 eg:p0 eg:s0 .
`
``
178
`+
eg:o1 eg:p1 eg:s1 .
`
``
179
`+
""",
`
``
180
`+
"",
`
``
181
`+
"",
`
``
182
`+
),
`
``
183
`+
),
`
``
184
`+
GraphDiffCase(
`
``
185
`+
Dataset,
`
``
186
`+
format="trig",
`
``
187
`+
lhs="""
`
``
188
`+
@prefix eg: example: .
`
``
189
`+
eg:o0 eg:p0 eg:s0 .
`
``
190
`+
eg:o1 eg:p1 eg:s1 .
`
``
191
`+
""",
`
``
192
`+
rhs="""
`
``
193
`+
@prefix eg: example: .
`
``
194
`+
eg:o0 eg:p0 eg:s0 .
`
``
195
`+
eg:o1 eg:p1 eg:s1 .
`
``
196
`+
""",
`
``
197
`+
expected_result=(
`
``
198
`+
"""
`
``
199
`+
@prefix eg: example: .
`
``
200
`+
eg:o0 eg:p0 eg:s0 .
`
``
201
`+
eg:o1 eg:p1 eg:s1 .
`
``
202
`+
""",
`
``
203
`+
"",
`
``
204
`+
"",
`
``
205
`+
),
`
``
206
`+
marks=pytest.mark.xfail(
`
``
207
`+
reason="quads are not supported", raises=ValueError
`
``
208
`+
),
`
``
209
`+
).as_params(),
`
``
210
`+
],
`
``
211
`+
)
`
``
212
`+
def test_assert_sets_equal(test_case: GraphDiffCase):
`
``
213
`+
"""
`
``
214
`+
GraphHelper.sets_equals and related functions work correctly in both
`
``
215
`+
positive and negative cases.
`
``
216
`+
"""
`
``
217
`+
lhs_graph: Graph = test_case.graph_type()
`
``
218
`+
lhs_graph.parse(data=test_case.lhs, format=test_case.format)
`
``
219
+
``
220
`+
rhs_graph: Graph = test_case.graph_type()
`
``
221
`+
rhs_graph.parse(data=test_case.rhs, format=test_case.format)
`
``
222
+
``
223
`+
in_both, in_lhs, in_rhs = graph_diff(lhs_graph, rhs_graph)
`
``
224
`+
in_both_set = GraphHelper.triple_or_quad_set(in_both, BNodeHandling.COLLAPSE)
`
``
225
`+
in_lhs_set = GraphHelper.triple_or_quad_set(in_lhs, BNodeHandling.COLLAPSE)
`
``
226
`+
in_rhs_set = GraphHelper.triple_or_quad_set(in_rhs, BNodeHandling.COLLAPSE)
`
``
227
+
``
228
`+
assert test_case.expected_in_both_set() == in_both_set
`
``
229
`+
assert test_case.expected_in_lhs_set() == in_lhs_set
`
``
230
`+
assert test_case.expected_in_rhs_set() == in_rhs_set
`
``
231
+
``
232
`+
Diff should be symetric
`
``
233
`+
in_rboth, in_rlhs, in_rrhs = graph_diff(rhs_graph, lhs_graph)
`
``
234
`+
in_rboth_set = GraphHelper.triple_or_quad_set(in_rboth, BNodeHandling.COLLAPSE)
`
``
235
`+
in_rlhs_set = GraphHelper.triple_or_quad_set(in_rlhs, BNodeHandling.COLLAPSE)
`
``
236
`+
in_rrhs_set = GraphHelper.triple_or_quad_set(in_rrhs, BNodeHandling.COLLAPSE)
`
``
237
+
``
238
`+
assert test_case.expected_in_both_set() == in_rboth_set
`
``
239
`+
assert test_case.expected_in_rhs_set() == in_rlhs_set
`
``
240
`+
assert test_case.expected_in_lhs_set() == in_rrhs_set
`