|
16 | 16 | Iterator, |
17 | 17 | Mapping, |
18 | 18 | Optional, |
| 19 | + Pattern, |
19 | 20 | Sequence, |
20 | 21 | Set, |
21 | 22 | TextIO, |
|
29 | 30 | from pyparsing import ParseException |
30 | 31 |
|
31 | 32 | from rdflib.graph import Graph |
| 33 | +from rdflib.namespace import Namespace |
32 | 34 | from rdflib.query import Result, ResultRow |
33 | | -from rdflib.term import Identifier, Literal, Node, Variable |
| 35 | +from rdflib.term import BNode, Identifier, Literal, Node, Variable |
34 | 36 |
|
35 | 37 | BindingsType = Sequence[Mapping[Variable, Identifier]] |
36 | 38 | ParseOutcomeType = Union[BindingsType, Type[Exception]] |
@@ -75,6 +77,43 @@ def test_select_result_parse( |
75 | 77 | assert parse_outcome == parsed_result.bindings |
76 | 78 |
|
77 | 79 |
|
| 80 | +EGSCHEME = Namespace("example:") |
| 81 | + |
| 82 | + |
| 83 | +@pytest.mark.parametrize( |
| 84 | + ("node", "format", "expected_result"), |
| 85 | + [ |
| 86 | + (BNode(), "csv", re.compile(r"^_:.*$")), |
| 87 | + (BNode("a"), "csv", "_:a"), |
| 88 | + (Literal("x11"), "csv", "x11"), |
| 89 | + ], |
| 90 | +) |
| 91 | +def test_xsv_serialize( |
| 92 | + node: Identifier, format: str, expected_result: Union[Pattern[str], str] |
| 93 | +) -> None: |
| 94 | + graph = Graph() |
| 95 | + graph.add((EGSCHEME.checkSubject, EGSCHEME.checkPredicate, node)) |
| 96 | + result = graph.query( |
| 97 | + f""" |
| 98 | + PREFIX egscheme: <{EGSCHEME}> |
| 99 | + SELECT ?o {{ |
| 100 | + egscheme:checkSubject egscheme:checkPredicate ?o |
| 101 | + }} |
| 102 | + """ |
| 103 | + ) |
| 104 | + assert len(result.bindings) == 1 |
| 105 | + with BytesIO() as bio: |
| 106 | + result.serialize(bio, format=format) |
| 107 | + result_text = bio.getvalue().decode("utf-8") |
| 108 | + result_lines = result_text.splitlines() |
| 109 | + assert len(result_lines) == 2 |
| 110 | + logging.debug("result_lines[1] = %r", result_lines[1]) |
| 111 | + if isinstance(expected_result, str): |
| 112 | + assert expected_result == result_lines[1] |
| 113 | + else: |
| 114 | + assert expected_result.match(result_lines[1]) |
| 115 | + |
| 116 | + |
78 | 117 | @pytest.fixture(scope="module") |
79 | 118 | def select_result(rdfs_graph: Graph) -> Result: |
80 | 119 | query = """ |
|
0 commit comments