File: test_graph_store.py

package info (click to toggle)
rdflib 7.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,852 kB
  • sloc: python: 59,555; sh: 153; makefile: 83; ruby: 74; xml: 45
file content (322 lines) | stat: -rw-r--r-- 10,159 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
"""
Tests for usage of the Store interface from Graph/NamespaceManager.
"""

from __future__ import annotations

import itertools
import logging
from typing import (
    TYPE_CHECKING,
    Any,
    Callable,
    Dict,
    Iterable,
    Mapping,
    Optional,
    Sequence,
    Tuple,
    Type,
    Union,
)
from unittest.mock import patch

import pytest

import rdflib.namespace
from rdflib.graph import ConjunctiveGraph, Dataset, Graph
from rdflib.namespace import Namespace
from rdflib.plugins.sparql.sparql import Query
from rdflib.plugins.stores.memory import Memory
from rdflib.query import Result
from rdflib.store import Store
from rdflib.term import Identifier, URIRef, Variable
from test.data import SIMPLE_TRIPLE_GRAPH

if TYPE_CHECKING:
    from _pytest.mark.structures import ParameterSet

NamespaceBindings = Dict[str, URIRef]


def check_ns(graph: Graph, expected_bindings: NamespaceBindings) -> None:
    actual_graph_bindings = list(graph.namespaces())
    logging.info("actual_bindings = %s", actual_graph_bindings)
    assert list(expected_bindings.items()) == actual_graph_bindings
    store: Store = graph.store
    actual_store_bindings = list(store.namespaces())
    assert list(expected_bindings.items()) == actual_store_bindings
    for prefix, uri in expected_bindings.items():
        assert store.prefix(uri) == prefix
        assert store.namespace(prefix) == uri


class MemoryWithoutBindOverride(Memory):
    def bind(self, prefix, namespace) -> None:  # type: ignore[override]
        return super().bind(prefix, namespace, False)


class GraphWithoutBindOverrideFix(Graph):
    def bind(self, prefix, namespace, override=True, replace=False) -> None:
        old_value = rdflib.namespace._with_bind_override_fix
        rdflib.namespace._with_bind_override_fix = False
        try:
            return super().bind(prefix, namespace, override, replace)
        finally:
            rdflib.namespace._with_bind_override_fix = old_value


GraphFactory = Callable[[], Graph]
GraphOperation = Callable[[Graph], None]
GraphOperations = Sequence[GraphOperation]

EGNS = Namespace("http://example.com/namespace#")
EGNS_V0 = EGNS["v0"]
EGNS_V1 = EGNS["v1"]
EGNS_V2 = EGNS["v2"]


def make_graph_store_bind_cases(
    store_type: Type[Store] = Memory,
    graph_type: Type[Graph] = Graph,
) -> Iterable[Union[Tuple[Any, ...], ParameterSet]]:
    """
    Generate test cases for test_graph_store_bind.
    """

    def graph_factory():
        return graph_type(bind_namespaces="none", store=store_type())

    id_prefix = f"{store_type.__name__}-{graph_type.__name__}"

    def _p(
        graph_factory: GraphFactory,
        ops: GraphOperations,
        expected_bindings: NamespaceBindings,
        expected_bindings_overrides: Optional[
            Dict[Tuple[Type[Graph], Type[Store]], NamespaceBindings]
        ] = None,
        *,
        id: Optional[str] = None,
    ):
        if expected_bindings_overrides is not None:
            expected_bindings = expected_bindings_overrides.get(
                (graph_type, store_type), expected_bindings
            )
        if id is not None:
            return pytest.param(graph_factory, ops, expected_bindings, id=id)
        else:
            return (graph_factory, ops, expected_bindings)

    yield from [
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
            ],
            {"eg": EGNS_V0},
            id=f"{id_prefix}-default-args",
        ),
        # reused-prefix
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("eg", EGNS_V1, override=False),
            ],
            {"eg": EGNS_V0, "eg1": EGNS_V1},
            id=f"{id_prefix}-reused-prefix-override-false-replace-false",
        ),
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("eg", EGNS_V1),
            ],
            {"eg": EGNS_V0, "eg1": EGNS_V1},
            id=f"{id_prefix}-reused-prefix-override-true-replace-false",
        ),
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("eg", EGNS_V1, override=False, replace=True),
            ],
            {"eg": EGNS_V0},
            {(GraphWithoutBindOverrideFix, Memory): {"eg": EGNS_V1}},
            id=f"{id_prefix}-reused-prefix-override-false-replace-true",
        ),
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("eg", EGNS_V1, replace=True),
            ],
            {"eg": EGNS_V1},
            {(Graph, MemoryWithoutBindOverride): {"eg": EGNS_V0}},
            id=f"{id_prefix}-reused-prefix-override-true-replace-true",
        ),
        # reused-namespace
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("egv0", EGNS_V0, override=False),
            ],
            {"eg": EGNS_V0},
            id=f"{id_prefix}-reused-namespace-override-false-replace-false",
        ),
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("egv0", EGNS_V0),
            ],
            {"egv0": EGNS_V0},
            {(Graph, MemoryWithoutBindOverride): {"eg": EGNS_V0}},
            id=f"{id_prefix}-reused-namespace-override-true-replace-false",
        ),
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("egv0", EGNS_V0, override=False, replace=True),
            ],
            {"eg": EGNS_V0},
            id=f"{id_prefix}-reused-namespace-override-false-replace-true",
        ),
        _p(
            graph_factory,
            [
                lambda g: g.bind("eg", EGNS_V0),
                lambda g: g.bind("egv0", EGNS_V0, replace=True),
            ],
            {"egv0": EGNS_V0},
            {(Graph, MemoryWithoutBindOverride): {"eg": EGNS_V0}},
            id=f"{id_prefix}-reused-namespace-override-true-replace-true",
        ),
    ]


@pytest.mark.parametrize(
    ["graph_factory", "ops", "expected_bindings"],
    itertools.chain(
        make_graph_store_bind_cases(),
        make_graph_store_bind_cases(store_type=MemoryWithoutBindOverride),
        make_graph_store_bind_cases(graph_type=GraphWithoutBindOverrideFix),
    ),
)
def test_graph_store_bind(
    graph_factory: GraphFactory,
    ops: GraphOperations,
    expected_bindings: NamespaceBindings,
) -> None:
    """
    The expected sequence of graph operations results in the expected
    namespace bindings.
    """
    graph = graph_factory()
    for op in ops:
        op(graph)
    check_ns(graph, expected_bindings)


@pytest.mark.parametrize(
    ("graph_factory", "query_graph"),
    [
        (Graph, lambda graph: graph.identifier),
        (ConjunctiveGraph, "__UNION__"),
        (Dataset, lambda graph: graph.default_context.identifier),
        (lambda store: Dataset(store=store, default_union=True), "__UNION__"),
    ],
)
def test_query_query_graph(
    graph_factory: Callable[[Store], Graph],
    query_graph: Union[str, Callable[[Graph], str]],
) -> None:
    """
    The `Graph.query` method passes the correct `queryGraph` argument
    to stores that have implemented a `Store.query` method.
    """

    mock_result = Result("SELECT")
    mock_result.vars = [Variable("s"), Variable("p"), Variable("o")]
    mock_result.bindings = [
        {
            Variable("s"): URIRef("http://example.org/subject"),
            Variable("p"): URIRef("http://example.org/predicate"),
            Variable("o"): URIRef("http://example.org/object"),
        },
    ]

    query_string = r"FAKE QUERY, NOT USED"
    store = Memory()
    graph = graph_factory(store)

    if callable(query_graph):
        query_graph = query_graph(graph)

    def mock_query(
        query: Union[Query, str],
        initNs: Mapping[str, Any],  # noqa: N803
        initBindings: Mapping[str, Identifier],  # noqa: N803
        queryGraph: str,  # noqa: N803
        **kwargs,
    ) -> Result:
        assert query_string == query
        assert dict(store.namespaces()) == initNs
        assert {} == initBindings
        assert query_graph == queryGraph
        assert {} == kwargs
        return mock_result

    with patch.object(store, "query", wraps=mock_query) as wrapped_query:
        actual_result = graph.query(query_string)
        assert actual_result.type == "SELECT"
        assert list(actual_result) == list(
            SIMPLE_TRIPLE_GRAPH.triples((None, None, None))
        )
        assert wrapped_query.call_count == 1


@pytest.mark.parametrize(
    ("graph_factory", "query_graph"),
    [
        (Graph, lambda graph: graph.identifier),
        (ConjunctiveGraph, "__UNION__"),
        (Dataset, lambda graph: graph.default_context.identifier),
        (lambda store: Dataset(store=store, default_union=True), "__UNION__"),
    ],
)
def test_update_query_graph(
    graph_factory: Callable[[Store], Graph],
    query_graph: Union[str, Callable[[Graph], str]],
) -> None:
    """
    The `Graph.update` method passes the correct `queryGraph` argument
    to stores that have implemented a `Store.update` method.
    """

    update_string = r"FAKE UPDATE, NOT USED"
    store = Memory()
    graph = graph_factory(store)

    if callable(query_graph):
        query_graph = query_graph(graph)

    def mock_update(
        query: Union[Query, str],
        initNs: Mapping[str, Any],  # noqa: N803
        initBindings: Mapping[str, Identifier],  # noqa: N803
        queryGraph: str,  # noqa: N803
        **kwargs,
    ) -> None:
        assert update_string == query
        assert dict(store.namespaces()) == initNs
        assert {} == initBindings
        assert query_graph == queryGraph
        assert {} == kwargs

    with patch.object(store, "update", wraps=mock_update) as wrapped_update:
        graph.update(update_string)
        assert wrapped_update.call_count == 1