|
1 | 1 | import unittest |
| 2 | +from contextlib import ExitStack |
| 3 | +from multiprocessing.sharedctypes import Value |
| 4 | +from typing import Any, Optional, Type, Union |
2 | 5 | from unittest.case import expectedFailure |
3 | 6 | from warnings import warn |
4 | 7 |
|
5 | 8 | import pytest |
6 | 9 |
|
7 | 10 | from rdflib import DCTERMS |
8 | | -from rdflib.graph import Graph |
| 11 | +from rdflib.graph import BNode, Graph, Literal |
9 | 12 | from rdflib.namespace import ( |
10 | 13 | FOAF, |
11 | 14 | OWL, |
@@ -260,3 +263,68 @@ def test_contains_method(self): |
260 | 263 |
|
261 | 264 | ref = URIRef("http://www.w3.org/2002/07/owl#real") |
262 | 265 | assert ref in OWL, "OWL does not include owl:real" |
| 266 | + |
| 267 | + def test_expand_curie_exception_messages(self) -> None: |
| 268 | + g = Graph() |
| 269 | + |
| 270 | + with pytest.raises(TypeError) as e: |
| 271 | + assert g.namespace_manager.expand_curie(URIRef("urn:example")) == None |
| 272 | + assert str(e.value) == "Argument must be a string, not URIRef." |
| 273 | + |
| 274 | + with pytest.raises(TypeError) as e: |
| 275 | + assert g.namespace_manager.expand_curie(Literal("rdf:type")) == None |
| 276 | + assert str(e.value) == "Argument must be a string, not Literal." |
| 277 | + |
| 278 | + with pytest.raises(TypeError) as e: |
| 279 | + assert g.namespace_manager.expand_curie(BNode()) == None |
| 280 | + assert str(e.value) == "Argument must be a string, not BNode." |
| 281 | + |
| 282 | + with pytest.raises(TypeError) as e: |
| 283 | + assert g.namespace_manager.expand_curie(Graph()) == None |
| 284 | + assert str(e.value) == "Argument must be a string, not Graph." |
| 285 | + |
| 286 | + @pytest.mark.parametrize( |
| 287 | + ["curie", "expected_result"], |
| 288 | + [ |
| 289 | + ("ex:tarek", URIRef("urn:example:tarek")), |
| 290 | + ("ex:", URIRef(f"urn:example:")), |
| 291 | + ("ex:a", URIRef(f"urn:example:a")), |
| 292 | + ("ex:a:b", URIRef(f"urn:example:a:b")), |
| 293 | + ("ex:a:b:c", URIRef(f"urn:example:a:b:c")), |
| 294 | + ("ex", ValueError), |
| 295 | + ("em:tarek", ValueError), |
| 296 | + ("em:", ValueError), |
| 297 | + ("em", ValueError), |
| 298 | + (":", ValueError), |
| 299 | + (":type", ValueError), |
| 300 | + ("í", ValueError), |
| 301 | + (" :", ValueError), |
| 302 | + ("", ValueError), |
| 303 | + ("\n", ValueError), |
| 304 | + (None, TypeError), |
| 305 | + (3, TypeError), |
| 306 | + (URIRef("urn:example:"), TypeError), |
| 307 | + (BNode(), TypeError), |
| 308 | + (Literal("rdf:type"), TypeError), |
| 309 | + ], |
| 310 | + ) |
| 311 | + def test_expand_curie( |
| 312 | + self, curie: Any, expected_result: Union[Type[Exception], URIRef, None] |
| 313 | + ) -> None: |
| 314 | + g = Graph(bind_namespaces="none") |
| 315 | + nsm = g.namespace_manager |
| 316 | + nsm.bind("ex", "urn:example:") |
| 317 | + result: Optional[URIRef] = None |
| 318 | + catcher: Optional[pytest.ExceptionInfo[Exception]] = None |
| 319 | + with ExitStack() as xstack: |
| 320 | + if isinstance(expected_result, type) and issubclass( |
| 321 | + expected_result, Exception |
| 322 | + ): |
| 323 | + catcher = xstack.enter_context(pytest.raises(expected_result)) |
| 324 | + result = g.namespace_manager.expand_curie(curie) |
| 325 | + |
| 326 | + if catcher is not None: |
| 327 | + assert result is None |
| 328 | + assert catcher.value is not None |
| 329 | + else: |
| 330 | + assert expected_result == result |
0 commit comments