|
| 1 | +import enum |
| 2 | +import itertools |
| 3 | +import logging |
| 4 | +from dataclasses import dataclass, field |
| 5 | +from pathlib import Path |
| 6 | +from test.testutils import pytest_mark_filter |
| 7 | +from typing import Any, Callable, Dict, Set, Union |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from rdflib import Graph |
| 12 | +from rdflib.namespace import Namespace |
| 13 | +from rdflib.plugins.stores.berkeleydb import has_bsddb |
| 14 | +from rdflib.store import Store |
| 15 | +from rdflib.term import IdentifiedNode, URIRef |
| 16 | + |
| 17 | + |
| 18 | +class StoreTrait(enum.Enum): |
| 19 | + WRAPPER = enum.auto() |
| 20 | + DISK_BACKED = enum.auto() |
| 21 | + |
| 22 | + |
| 23 | +@dataclass |
| 24 | +class StoreInfo: |
| 25 | + name: str |
| 26 | + traits: Set[StoreTrait] = field(default_factory=set) |
| 27 | + |
| 28 | + def make_graph( |
| 29 | + self, tmp_path: Path, identifier: Union[IdentifiedNode, str, None] = None |
| 30 | + ) -> Graph: |
| 31 | + graph = Graph(store=self.name, bind_namespaces="none", identifier=identifier) |
| 32 | + if StoreTrait.DISK_BACKED in self.traits: |
| 33 | + use_path = tmp_path / f"{self.name}.storedata" |
| 34 | + use_path.mkdir(exist_ok=True, parents=True) |
| 35 | + logging.debug("use_path = %s", use_path) |
| 36 | + graph.open(f"{use_path}", create=True) |
| 37 | + return graph |
| 38 | + |
| 39 | + |
| 40 | +def make_store_info_dict(*result_format: StoreInfo) -> Dict[str, StoreInfo]: |
| 41 | + result = {} |
| 42 | + for item in result_format: |
| 43 | + result[item.name] = item |
| 44 | + return result |
| 45 | + |
| 46 | + |
| 47 | +store_info_dict = make_store_info_dict( |
| 48 | + StoreInfo("Memory"), |
| 49 | + StoreInfo("SimpleMemory"), |
| 50 | + StoreInfo("SPARQLStore"), |
| 51 | + StoreInfo("SPARQLUpdateStore"), |
| 52 | + *((StoreInfo("BerkeleyDB", {StoreTrait.DISK_BACKED}),) if has_bsddb else ()), |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture( |
| 57 | + scope="module", |
| 58 | + params=store_info_dict.keys(), |
| 59 | +) |
| 60 | +def store_name(request) -> str: |
| 61 | + assert isinstance(request.param, str) |
| 62 | + return request.param |
| 63 | + |
| 64 | + |
| 65 | +GraphMutator = Callable[[Graph], Any] |
| 66 | + |
| 67 | +EGNSSUB = Namespace("http://example.com/sub/") |
| 68 | +EGNSSUB_V0 = EGNSSUB["v0"] |
| 69 | +EGNSSUB_V1 = EGNSSUB["v1"] |
| 70 | +EGNSSUB_V2 = EGNSSUB["v2"] |
| 71 | + |
| 72 | +NamespaceBindings = Dict[str, URIRef] |
| 73 | + |
| 74 | + |
| 75 | +def make_graph(tmp_path: Path, store_name: str) -> Graph: |
| 76 | + logging.info("store_info_dict.keys() = %s", store_info_dict.keys()) |
| 77 | + store_info = store_info_dict[store_name] |
| 78 | + return store_info.make_graph(tmp_path) |
| 79 | + |
| 80 | + |
| 81 | +def check_ns(graph: Graph, expected_bindings: NamespaceBindings) -> None: |
| 82 | + actual_graph_bindings = list(graph.namespaces()) |
| 83 | + logging.info("actual_bindings = %s", actual_graph_bindings) |
| 84 | + assert list(expected_bindings.items()) == actual_graph_bindings |
| 85 | + store: Store = graph.store |
| 86 | + actual_store_bindings = list(store.namespaces()) |
| 87 | + assert list(expected_bindings.items()) == actual_store_bindings |
| 88 | + for prefix, uri in expected_bindings.items(): |
| 89 | + assert store.prefix(uri) == prefix |
| 90 | + assert store.namespace(prefix) == uri |
| 91 | + |
| 92 | + |
| 93 | +@pytest.mark.parametrize( |
| 94 | + ["override", "replace"], itertools.product([True, False], [True, False]) |
| 95 | +) |
| 96 | +def test_simple_bind( |
| 97 | + tmp_path: Path, store_name: str, override: bool, replace: bool |
| 98 | +) -> None: |
| 99 | + """ |
| 100 | + URI binds to a prefix regardless of override and replace values. |
| 101 | + """ |
| 102 | + graph = make_graph(tmp_path, store_name) |
| 103 | + graph.bind("egsub", EGNSSUB_V0, override=override, replace=replace) |
| 104 | + check_ns(graph, {"egsub": EGNSSUB_V0}) |
| 105 | + |
| 106 | + |
| 107 | +@pytest.mark.parametrize( |
| 108 | + ["override", "replace"], itertools.product([True, False], [True, False]) |
| 109 | +) |
| 110 | +def test_bind_two_bind( |
| 111 | + tmp_path: Path, store_name: str, override: bool, replace: bool |
| 112 | +) -> None: |
| 113 | + """ |
| 114 | + A second prefix with a different URI binds correctly regardless of override |
| 115 | + and replace values. |
| 116 | + """ |
| 117 | + graph = make_graph(tmp_path, store_name) |
| 118 | + graph.bind("egsub", EGNSSUB_V0) |
| 119 | + graph.bind("egsubv1", EGNSSUB_V1, override=override, replace=replace) |
| 120 | + check_ns(graph, {"egsub": EGNSSUB_V0, "egsubv1": EGNSSUB_V1}) |
| 121 | + |
| 122 | + |
| 123 | +@pytest.mark.parametrize("replace", [True, False]) |
| 124 | +def test_rebind_uri_override(tmp_path: Path, store_name: str, replace: bool) -> None: |
| 125 | + """ |
| 126 | + URI binds to new prefix if override is `True` regardless of the value of `replace`. |
| 127 | + """ |
| 128 | + graph = make_graph(tmp_path, store_name) |
| 129 | + graph.bind("egsub", EGNSSUB_V0) |
| 130 | + graph.bind("egsubv0", EGNSSUB_V0, override=True, replace=replace) |
| 131 | + check_ns(graph, {"egsubv0": EGNSSUB_V0}) |
| 132 | + |
| 133 | + |
| 134 | +@pytest.mark.parametrize("replace", [True, False]) |
| 135 | +def test_rebind_uri_no_override(tmp_path: Path, store_name: str, replace: bool) -> None: |
| 136 | + """ |
| 137 | + URI does not bind to new prefix if override is `False` regardless of the value of `replace`. |
| 138 | + """ |
| 139 | + graph = make_graph(tmp_path, store_name) |
| 140 | + graph.bind("egsub", EGNSSUB_V0) |
| 141 | + graph.bind("egsubv0", EGNSSUB_V0, override=False, replace=replace) |
| 142 | + check_ns(graph, {"egsub": EGNSSUB_V0}) |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.parametrize( |
| 146 | + ["store_name", "override"], |
| 147 | + pytest_mark_filter( |
| 148 | + itertools.product(store_info_dict.keys(), [True, False]), |
| 149 | + { |
| 150 | + ("SPARQLStore", False): ( |
| 151 | + pytest.mark.xfail( |
| 152 | + raises=AssertionError, |
| 153 | + reason=""" |
| 154 | + SPARQLStore's namespace bindings work on a fundementally different way than |
| 155 | + the other stores, which is both simpler, but requires some additional work |
| 156 | + to make it behave like the other stores. |
| 157 | + """, |
| 158 | + ), |
| 159 | + ), |
| 160 | + ("SPARQLUpdateStore", False): ( |
| 161 | + pytest.mark.xfail( |
| 162 | + raises=AssertionError, |
| 163 | + reason=""" |
| 164 | + SPARQLStore's namespace bindings work on a fundementally different way than |
| 165 | + the other stores, which is both simpler, but requires some additional work |
| 166 | + to make it behave like the other stores. |
| 167 | + """, |
| 168 | + ), |
| 169 | + ), |
| 170 | + }, |
| 171 | + ), |
| 172 | +) |
| 173 | +def test_rebind_prefix_replace(tmp_path: Path, store_name: str, override: bool) -> None: |
| 174 | + """ |
| 175 | + If `replace` is `True`, |
| 176 | + Prefix binds to a new URI and `override` is `True`, |
| 177 | + but does not bind to a new URI if `override` is `False`. |
| 178 | +
|
| 179 | + NOTE re logic, this is mainly just taking what most stores does and saying |
| 180 | + that is the right thing, not sure it really makes sense. This method is |
| 181 | + quite complicated and it is unlcear which of replace and override should |
| 182 | + take precedence in this case, once we sorted it out we should clarify in |
| 183 | + the documentation. |
| 184 | + """ |
| 185 | + graph = make_graph(tmp_path, store_name) |
| 186 | + graph.bind("egsub", EGNSSUB_V0) |
| 187 | + if override: |
| 188 | + graph.bind("egsub", EGNSSUB_V1, override=override, replace=True) |
| 189 | + check_ns(graph, {"egsub": EGNSSUB_V1}) |
| 190 | + else: |
| 191 | + graph.bind("egsub", EGNSSUB_V1, override=override, replace=True) |
| 192 | + check_ns(graph, {"egsub": EGNSSUB_V0}) |
| 193 | + |
| 194 | + |
| 195 | +@pytest.mark.parametrize( |
| 196 | + ["reuse_override", "reuse_replace"], itertools.product([True, False], [True, False]) |
| 197 | +) |
| 198 | +def test_rebind_prefix_reuse_uri_replace( |
| 199 | + tmp_path: Path, store_name: str, reuse_override: bool, reuse_replace: bool |
| 200 | +) -> None: |
| 201 | + """ |
| 202 | + Prefix binds to a new URI and the old URI can be reused with a new prefix |
| 203 | + regardless of the value of override or replace used when reusing the old |
| 204 | + URI. |
| 205 | + """ |
| 206 | + graph = make_graph(tmp_path, store_name) |
| 207 | + graph.bind("egsub", EGNSSUB_V0) |
| 208 | + graph.bind("egsub", EGNSSUB_V1, override=True, replace=True) |
| 209 | + graph.bind("egsubv0", EGNSSUB_V0, override=reuse_override, replace=reuse_replace) |
| 210 | + check_ns(graph, {"egsub": EGNSSUB_V1, "egsubv0": EGNSSUB_V0}) |
0 commit comments