|
| 1 | +import tempfile |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import rdflib |
| 6 | +from rdflib.plugins.serializers.xmlwriter import XMLWriter |
| 7 | + |
| 8 | +EXNS = rdflib.Namespace("https://example.org/ns/") |
| 9 | +TRIXNS = rdflib.Namespace("http://www.w3.org/2004/03/trix/trix-1/") |
| 10 | + |
| 11 | + |
| 12 | +def test_xmlwriter_namespaces(): |
| 13 | + |
| 14 | + g = rdflib.Graph() |
| 15 | + |
| 16 | + with tempfile.TemporaryFile() as fp: |
| 17 | + |
| 18 | + xmlwr = XMLWriter(fp, g.namespace_manager, extra_ns={"": TRIXNS, "ex": EXNS}) |
| 19 | + |
| 20 | + xmlwr.namespaces() |
| 21 | + |
| 22 | + fp.seek(0) |
| 23 | + |
| 24 | + assert fp.readlines() == [ |
| 25 | + b'<?xml version="1.0" encoding="utf-8"?>\n', |
| 26 | + b' xmlns:owl="http://www.w3.org/2002/07/owl#"\n', |
| 27 | + b' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n', |
| 28 | + b' xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"\n', |
| 29 | + b' xmlns:xsd="http://www.w3.org/2001/XMLSchema#"\n', |
| 30 | + b' xmlns:xml="http://www.w3.org/XML/1998/namespace"\n', |
| 31 | + b' xmlns="http://www.w3.org/2004/03/trix/trix-1/"\n', |
| 32 | + b' xmlns:ex="https://example.org/ns/"\n', |
| 33 | + ] |
| 34 | + |
| 35 | + |
| 36 | +def test_xmlwriter_decl(): |
| 37 | + |
| 38 | + g = rdflib.Graph() |
| 39 | + |
| 40 | + with tempfile.TemporaryFile() as fp: |
| 41 | + |
| 42 | + xmlwr = XMLWriter(fp, g.namespace_manager, decl=0, extra_ns={"": TRIXNS}) |
| 43 | + |
| 44 | + xmlwr.namespaces() |
| 45 | + |
| 46 | + fp.seek(0) |
| 47 | + assert fp.readlines() == [ |
| 48 | + b"\n", |
| 49 | + b' xmlns:owl="http://www.w3.org/2002/07/owl#"\n', |
| 50 | + b' xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n', |
| 51 | + b' xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"\n', |
| 52 | + b' xmlns:xsd="http://www.w3.org/2001/XMLSchema#"\n', |
| 53 | + b' xmlns:xml="http://www.w3.org/XML/1998/namespace"\n', |
| 54 | + b' xmlns="http://www.w3.org/2004/03/trix/trix-1/"\n', |
| 55 | + ] |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.parametrize( |
| 59 | + "uri", |
| 60 | + [ |
| 61 | + # NS bound to “ex”, so “ex:foo” |
| 62 | + ["https://example.org/ns/foo", "ex:foo"], |
| 63 | + # NS bound to "", so “graph” |
| 64 | + ["http://www.w3.org/2004/03/trix/trix-1/graph", "graph"], |
| 65 | + # NS not in extra_ns, use ns<int> idiom |
| 66 | + ["https://example.org/foo", "ns1:foo"], |
| 67 | + ], |
| 68 | +) |
| 69 | +def test_xmlwriter_qname(uri): |
| 70 | + |
| 71 | + g = rdflib.Graph() |
| 72 | + g.bind("", TRIXNS) |
| 73 | + g.bind("ex", EXNS) |
| 74 | + |
| 75 | + fp = tempfile.TemporaryFile() |
| 76 | + |
| 77 | + xmlwr = XMLWriter(fp, g.namespace_manager, extra_ns={"": TRIXNS, "ex": EXNS}) |
| 78 | + |
| 79 | + assert xmlwr.qname(uri[0]) == uri[1] |
0 commit comments