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 87 88 89
|
import glob
import os
import tempfile
from unittest.mock import MagicMock, patch
from click.testing import CliRunner
from rdflib_endpoint.__main__ import cli
runner = CliRunner()
out_formats = ["ttl", "nt", "xml", "jsonld", "trig"]
def test_convert():
for out_format in out_formats:
with tempfile.NamedTemporaryFile(delete=True) as tmp_file:
out_file = str(f"{tmp_file}.{out_format}")
result = runner.invoke(
cli,
["convert", "tests/resources/test2.ttl", "--output", out_file],
)
assert result.exit_code == 0
with open(out_file) as file:
content = file.read()
assert len(content) > 1
# Fix issue with python creating unnecessary temp files on disk
for f in glob.glob("<tempfile._TemporaryFileWrapper*"):
os.remove(f)
def test_convert_oxigraph():
with tempfile.NamedTemporaryFile(delete=True) as tmp_file:
result = runner.invoke(
cli,
[
"convert",
"--store",
"oxigraph",
"tests/resources/test2.ttl",
"--output",
str(tmp_file),
],
)
assert result.exit_code == 0
with open(str(tmp_file)) as file:
content = file.read()
assert len(content) > 1
# Fix issue with python creating unnecessary temp files on disk
for f in glob.glob("<tempfile._TemporaryFileWrapper*"):
os.remove(f)
# NOTE: Needs to run last tests, for some reason patching uvicorn as a side effects on follow up tests
@patch("rdflib_endpoint.__main__.uvicorn.run")
def test_serve(mock_run: MagicMock) -> None:
"""Test serve, mock uvicorn.run to prevent API hanging"""
mock_run.return_value = None
result = runner.invoke(
cli,
[
"serve",
"tests/resources/test.nq",
"tests/resources/test2.ttl",
"tests/resources/another.jsonld",
],
)
assert result.exit_code == 0
@patch("rdflib_endpoint.__main__.uvicorn.run")
def test_serve_oxigraph(mock_run: MagicMock) -> None:
"""Test serve oxigraph, mock uvicorn.run to prevent API hanging"""
mock_run.return_value = None
result = runner.invoke(
cli,
[
"serve",
"--store",
"oxigraph",
"tests/resources/test.nq",
"tests/resources/test2.ttl",
"tests/resources/another.jsonld",
],
)
assert result.exit_code == 0
|