Skip to content

Commit

Permalink
Add xfail tests for a couple of issues
Browse files Browse the repository at this point in the history
This includes xfails for the following issues:

- RDFLib#1216
- RDFLib#1655
- RDFLib#1649

Also:

- Add graph variant test scaffolding. Multiple files representing the
  same graph can now easily be tested to be isomorphic by just adding
  them in `test/variants`.
- Add more things to `testutils.GraphHelper`, including some methods that does
  asserts with better messages. Also include some tests for GraphHelper.
- Add some extra files to test_roundtrip, set the default identifier
  when parsing, and change verbose flag to rather be based on debug
  logging.
- move one test from `test/test_issue247.py` to variants.
- Fix problems with `.editorconfig` which prevents it from working
  properly.
  • Loading branch information
aucampia committed Jan 12, 2022
1 parent d957533 commit 82d74c1
Show file tree
Hide file tree
Showing 26 changed files with 752 additions and 59 deletions.
6 changes: 3 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ insert_final_newline = true
trim_trailing_whitespace = true

# Leave line endings as-is in Markdown and ReStructuredText files
[*.{md, rst}]
[*.{md,rst}]
charset = utf-8
trim_trailing_whitespace = false

# Matches multiple files with brace expansion notation
# Set default charset
[*.{js, py, pyi, toml, yml, yaml}]
[*.{js,py,pyi,toml,yml,yaml}]
charset = utf-8

[*.{yaml, yml}]
[*.{yaml,yml,json}]
indent_style = space
indent_size = 2

Expand Down
3 changes: 3 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from .earl import EarlReporter
import pytest

pytest_plugins = [EarlReporter.__module__]

pytest.register_assert_rewrite("test.testutils")
23 changes: 0 additions & 23 deletions test/test_issue247.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,8 @@
</rdf:RDF>"""

passxml = """\
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<rdf:Description rdf:about="http://example.org/">
<dc:description rdf:parseType="Literal">
<p xmlns="http://www.w3.org/1999/xhtml"></p>
</dc:description>
</rdf:Description>
</rdf:RDF>"""


class TestXMLLiteralwithLangAttr(unittest.TestCase):
def test_successful_parse_of_literal_without_xmllang_attr(self):
"""
Test parse of Literal without xmllang attr passes
Parsing an RDF/XML document fails with a KeyError when
it contains a XML Literal with a xml:lang attribute:
"""
g = rdflib.Graph()
g.parse(data=passxml, format="xml")

def test_failing_parse_of_literal_with_xmllang_attr(self):
"""
Show parse of Literal with xmllang attr fails
Expand Down
37 changes: 37 additions & 0 deletions test/test_nt_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from rdflib.plugins.parsers import ntriples
from urllib.request import urlopen
from pathlib import Path
import pytest

from test import TEST_DIR

Expand All @@ -18,6 +19,42 @@ def nt_file(fn):
return os.path.join(NT_PATH, fn)


class TestNT:
@pytest.mark.parametrize(
"quoted, unquoted",
[
(r"\n", "\n"),
(r"\r", "\r"),
pytest.param(
r"\\r",
r"\r",
marks=pytest.mark.xfail(
reason="unquote is broken", raises=AssertionError
),
),
(r"\\\r", "\\\r"),
(r"\u000D", "\r"),
pytest.param(
r"\\u000D",
r"\u000D",
marks=pytest.mark.xfail(
reason="unquote is broken", raises=AssertionError
),
),
(r"\U0000000D", "\r"),
pytest.param(
r"\\U0000000D",
r"\U0000000D",
marks=pytest.mark.xfail(
reason="unquote is broken", raises=AssertionError
),
),
],
)
def test_unquote_specials(self, quoted: str, unquoted: str) -> None:
assert unquoted == ntriples.unquote(quoted)


class NTTestCase(unittest.TestCase):
def testIssue859(self):
graphA = Graph()
Expand Down
123 changes: 99 additions & 24 deletions test/test_roundtrip.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from json.decoder import JSONDecodeError
import logging
import os.path
from pathlib import Path
from test.testutils import GraphHelper
from typing import Callable, Collection, Iterable, List, Optional, Set, Tuple, Union
from xml.sax import SAXParseException

Expand All @@ -10,7 +10,12 @@

import rdflib
import rdflib.compare
from rdflib.plugins.parsers.notation3 import BadSyntax
from rdflib.term import URIRef
from rdflib.util import guess_format
from test.testutils import GraphHelper

logger = logging.getLogger(__name__)

"""
Test round-tripping by all serializers/parser that are registered.
Expand All @@ -34,7 +39,8 @@
"""

NT_DATA_DIR = Path(__file__).parent / "nt"
TEST_DIR = Path(__file__).parent
NT_DATA_DIR = TEST_DIR / "nt"
INVALID_NT_FILES = {
# illegal literal as subject
"literals-01.nt",
Expand Down Expand Up @@ -116,6 +122,70 @@
reason="rdflib.compare.isomorphic does not work for quoted graphs.",
raises=AssertionError,
),
("ntriples", "special_chars.ttl"): pytest.mark.xfail(
reason="issue with unescaping in nt",
raises=AssertionError,
),
("nt", "special_chars.ttl"): pytest.mark.xfail(
reason="issue with unescaping in nt",
raises=AssertionError,
),
("nt11", "special_chars.ttl"): pytest.mark.xfail(
reason="broken escaping",
raises=AssertionError,
),
("nquads", "special_chars.ttl"): pytest.mark.xfail(
reason="broken escaping",
raises=AssertionError,
),
("hext", "special_chars.ttl"): pytest.mark.xfail(
reason="missing escaping.",
raises=JSONDecodeError,
),
("xml", "special_chars.ttl"): pytest.mark.xfail(
reason="missing escaping: PCDATA invalid Char value 12 and 8",
raises=SAXParseException,
),
("trix", "special_chars.ttl"): pytest.mark.xfail(
reason="missing escaping: PCDATA invalid Char value 12 and 8",
raises=SAXParseException,
),
("hext", "special_chars.nt"): pytest.mark.xfail(
reason="missing escaping.",
raises=JSONDecodeError,
),
("xml", "special_chars.nt"): pytest.mark.xfail(
reason="missing escaping: PCDATA invalid Char value 12 and 8",
raises=SAXParseException,
),
("trix", "special_chars.nt"): pytest.mark.xfail(
reason="missing escaping: PCDATA invalid Char value 12 and 8",
raises=SAXParseException,
),
("hext", "xml_literal.rdf"): pytest.mark.xfail(
reason="missing escaping.",
raises=JSONDecodeError,
),
("n3", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("ttl", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("trig", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("turtle", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="missing 'rdf:' prefix",
raises=BadSyntax,
),
("hext", "rdf_prefix.jsonld"): pytest.mark.xfail(
reason="context being dropped",
raises=AssertionError,
),
}

# This is for files which can only be represented properly in one format
Expand All @@ -140,38 +210,28 @@ def collect_files(
return result


def roundtrip(infmt: str, testfmt: str, source: Path, verbose: bool = False) -> None:

def roundtrip(infmt: str, testfmt: str, source: Path) -> None:
g1 = rdflib.ConjunctiveGraph()

g1.parse(source, format=infmt)

s = g1.serialize(format=testfmt)

if verbose:
print("S:")
print(s, flush=True)
if logger.isEnabledFor(logging.DEBUG):
logger.debug("serailized = \n%s", s)

g2 = rdflib.ConjunctiveGraph()
g2.parse(data=s, format=testfmt)

if verbose:
if logger.isEnabledFor(logging.DEBUG):
both, first, second = rdflib.compare.graph_diff(g1, g2)
print("Diff:")
print("%d triples in both" % len(both))
print("G1 Only:")
for t in sorted(first):
print(t)
logger.debug("Items in both:\n%s", GraphHelper.format_graph_set(both))
logger.debug("Items in G1 Only:\n%s", GraphHelper.format_graph_set(first))
logger.debug("Items in G2 Only:\n%s", GraphHelper.format_graph_set(second))

print("--------------------")
print("G2 Only")
for t in sorted(second):
print(t)
GraphHelper.assert_isomorphic(g1, g2)

assert rdflib.compare.isomorphic(g1, g2)

if verbose:
print("Ok!")
if logger.isEnabledFor(logging.DEBUG):
logger.debug("OK")


_formats: Optional[Set[str]] = None
Expand All @@ -190,10 +250,12 @@ def get_formats() -> Set[str]:
return _formats


def make_cases(files: Collection[Tuple[Path, str]]) -> Iterable[ParameterSet]:
def make_cases(
files: Collection[Tuple[Path, str]], hext_okay: bool = False
) -> Iterable[ParameterSet]:
formats = get_formats()
for testfmt in formats:
if testfmt == "hext":
if not hext_okay and testfmt == "hext":
continue
logging.debug("testfmt = %s", testfmt)
for f, infmt in files:
Expand Down Expand Up @@ -230,3 +292,16 @@ def test_nt(checker: Callable[[str, str, Path], None], args: Tuple[str, str, Pat
@pytest.mark.parametrize("checker, args", make_cases(collect_files(N3_DATA_DIR)))
def test_n3(checker: Callable[[str, str, Path], None], args: Tuple[str, str, Path]):
checker(*args)


EXTRA_FILES = [
(TEST_DIR / "variants" / "special_chars.ttl", "turtle"),
(TEST_DIR / "variants" / "special_chars.nt", "ntriples"),
(TEST_DIR / "variants" / "xml_literal.rdf", "xml"),
(TEST_DIR / "variants" / "rdf_prefix.jsonld", "json-ld"),
]


@pytest.mark.parametrize("checker, args", make_cases(EXTRA_FILES, hext_okay=True))
def test_extra(checker: Callable[[str, str, Path], None], args: Tuple[str, str, Path]):
checker(*args)
Loading

0 comments on commit 82d74c1

Please sign in to comment.