5
5
import sys
6
6
from contextlib import ExitStack
7
7
from pathlib import Path
8
+
from test.utils.exceptions import ExceptionChecker
8
9
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Set, Tuple, Type, Union
9
10
10
11
import pytest
@@ -484,3 +485,115 @@ def check() -> None:
484
485
check()
485
486
# Run a second time to check caching
486
487
check()
488
+
489
+
490
+
def make_test_nsm() -> NamespaceManager:
491
+
namespaces = [
492
+
("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"),
493
+
("", "http://example.org/"),
494
+
(
495
+
# Because of <https://github.com/RDFLib/rdflib/issues/2077> this
496
+
# will have no effect on the namespace manager.
497
+
"eg",
498
+
"http://example.org/",
499
+
),
500
+
]
501
+
graph = Graph(bind_namespaces="none")
502
+
for prefix, namespace in namespaces:
503
+
graph.bind(prefix, namespace, override=False)
504
+
505
+
return graph.namespace_manager
506
+
507
+
508
+
@pytest.fixture(scope="session")
509
+
def test_nsm_session() -> NamespaceManager:
510
+
return make_test_nsm()
511
+
512
+
513
+
@pytest.fixture(scope="function")
514
+
def test_nsm_function() -> NamespaceManager:
515
+
return make_test_nsm()
516
+
517
+
518
+
@pytest.mark.parametrize(
519
+
["curie", "expected_result"],
520
+
[
521
+
("rdf:type", "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"),
522
+
(":foo", "http://example.org/foo"),
523
+
("too_small", ExceptionChecker(ValueError, "Malformed curie argument")),
524
+
(
525
+
"egdo:bar",
526
+
ExceptionChecker(ValueError, 'Prefix "egdo" not bound to any namespace'),
527
+
),
528
+
pytest.param(
529
+
"eg:foo",
530
+
"http://example.org/foo",
531
+
marks=pytest.mark.xfail(
532
+
raises=ValueError,
533
+
reason="This is failing because of https://github.com/RDFLib/rdflib/issues/2077",
534
+
),
535
+
),
536
+
],
537
+
)
538
+
def test_expand_curie(
539
+
test_nsm_session: NamespaceManager,
540
+
curie: str,
541
+
expected_result: Union[ExceptionChecker, str],
542
+
) -> None:
543
+
nsm = test_nsm_session
544
+
with ExitStack() as xstack:
545
+
if isinstance(expected_result, ExceptionChecker):
546
+
xstack.enter_context(expected_result)
547
+
result = nsm.expand_curie(curie)
548
+
549
+
if not isinstance(expected_result, ExceptionChecker):
550
+
assert URIRef(expected_result) == result
551
+
552
+
553
+
@pytest.mark.parametrize(
554
+
["uri", "generate", "expected_result"],
555
+
[
556
+
("http://www.w3.org/1999/02/22-rdf-syntax-ns#type", None, "rdf:type"),
557
+
("http://example.org/foo", None, ":foo"),
558
+
("http://example.com/a#chair", None, "ns1:chair"),
559
+
("http://example.com/a#chair", True, "ns1:chair"),
560
+
(
561
+
"http://example.com/a#chair",
562
+
False,
563
+
ExceptionChecker(
564
+
KeyError, "No known prefix for http://example.com/a# and generate=False"
565
+
),
566
+
),
567
+
("http://example.com/b#chair", None, "ns1:chair"),
568
+
("http://example.com/c", None, "ns1:c"),
569
+
("", None, ExceptionChecker(ValueError, "Can't split ''")),
570
+
(
571
+
"http://example.com/",
572
+
None,
573
+
ExceptionChecker(ValueError, "Can't split 'http://example.com/'"),
574
+
),
575
+
],
576
+
)
577
+
def test_generate_curie(
578
+
test_nsm_function: NamespaceManager,
579
+
uri: str,
580
+
generate: Optional[bool],
581
+
expected_result: Union[ExceptionChecker, str],
582
+
) -> None:
583
+
"""
584
+
.. note::
585
+
586
+
This is using the function scoped nsm fixture because curie has side
587
+
effects and will modify the namespace manager.
588
+
"""
589
+
nsm = test_nsm_function
590
+
with ExitStack() as xstack:
591
+
if isinstance(expected_result, ExceptionChecker):
592
+
xstack.enter_context(expected_result)
593
+
if generate is None:
594
+
result = nsm.curie(uri)
595
+
else:
596
+
result = nsm.curie(uri, generate=generate)
597
+
598
+
if not isinstance(expected_result, ExceptionChecker):
599
+
assert expected_result == result
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4