summaryrefslogtreecommitdiff
path: root/test/test_misc/test_parse_file_guess_format.py
blob: b9e2c065887888bc09178185c813cc7e7e9159d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# NOTE: The config below enables strict mode for mypy.
# mypy: no_ignore_errors
# mypy: warn_unused_configs, disallow_any_generics
# mypy: disallow_subclassing_any, disallow_untyped_calls
# mypy: disallow_untyped_defs, disallow_incomplete_defs
# mypy: check_untyped_defs, disallow_untyped_decorators
# mypy: no_implicit_optional, warn_redundant_casts, warn_unused_ignores
# mypy: warn_return_any, no_implicit_reexport, strict_equality

import logging
import os
from pathlib import Path
from shutil import copyfile
from tempfile import TemporaryDirectory
from test.data import TEST_DATA_DIR

import pytest

from rdflib import Graph
from rdflib.exceptions import ParserError
from rdflib.util import guess_format


class TestFileParserGuessFormat:
    def test_guess_format(self) -> None:
        assert guess_format("example.trix") == "trix"
        assert guess_format("local-file.jsonld") == "json-ld"
        assert guess_format("local-file.json-ld") == "json-ld"
        assert guess_format("/some/place/on/disk/example.json") == "json-ld"
        assert guess_format("../../relative/place/on/disk/example.json") == "json-ld"
        assert guess_format("example.rdf") == "xml"
        assert guess_format("example.nt") == "nt"
        assert guess_format("example.nq") == "nquads"
        assert guess_format("example.nquads") == "nquads"
        assert guess_format("example.n3") == "n3"
        assert guess_format("example.docx", None) is None
        assert guess_format("example", None) is None
        assert guess_format("example.mkv", None) is None

    def test_jsonld(self) -> None:
        g = Graph()
        assert isinstance(g.parse("test/jsonld/1.1/manifest.jsonld"), Graph)
        assert isinstance(g.parse("test/jsonld/file_ending_test_01.json"), Graph)
        assert isinstance(g.parse("test/jsonld/file_ending_test_01.json-ld"), Graph)
        assert isinstance(g.parse("test/jsonld/file_ending_test_01.jsonld"), Graph)

    def test_ttl(self) -> None:
        g = Graph()
        assert isinstance(
            g.parse(
                os.path.join(
                    TEST_DATA_DIR, "suites", "w3c", "turtle", "IRI_subject.ttl"
                )
            ),
            Graph,
        )

    def test_n3(self) -> None:
        g = Graph()
        assert isinstance(
            g.parse(os.path.join(TEST_DATA_DIR, "example-lots_of_graphs.n3")), Graph
        )

    def test_warning(self) -> None:
        g = Graph()
        graph_logger = logging.getLogger("rdflib")

        with TemporaryDirectory() as tmpdirname:
            newpath = Path(tmpdirname).joinpath("no_file_ext")
            copyfile(
                os.path.join(
                    TEST_DATA_DIR,
                    "suites",
                    "w3c",
                    "rdf-xml",
                    "datatypes",
                    "test001.rdf",
                ),
                str(newpath),
            )
            with pytest.raises(ParserError, match=r"Could not guess RDF format"):
                with pytest.warns(
                    UserWarning,
                    match="does not look like a valid URI, trying to serialize this will break.",
                ) as logwarning:
                    g.parse(str(newpath))