Debian Codegraph @

tests/test_serve_cli.py

raw
1 import glob
2 import os
3 import tempfile
4 from unittest.mock import MagicMock, patch
5
6 from click.testing import CliRunner
7
8 from rdflib_endpoint.__main__ import cli
9
10 runner = CliRunner()
11
12 out_formats = ["ttl", "nt", "xml", "jsonld", "trig"]
13
14
15 def test_convert():
16 for out_format in out_formats:
17 with tempfile.NamedTemporaryFile(delete=True) as tmp_file:
18 out_file = str(f"{tmp_file}.{out_format}")
19 result = runner.invoke(
20 cli,
21 ["convert", "tests/resources/test2.ttl", "--output", out_file],
22 )
23 assert result.exit_code == 0
24 with open(out_file) as file:
25 content = file.read()
26 assert len(content) > 1
27
28 # Fix issue with python creating unnecessary temp files on disk
29 for f in glob.glob("<tempfile._TemporaryFileWrapper*"):
30 os.remove(f)
31
32
33 def test_convert_oxigraph():
34 with tempfile.NamedTemporaryFile(delete=True) as tmp_file:
35 result = runner.invoke(
36 cli,
37 [
38 "convert",
39 "--store",
40 "oxigraph",
41 "tests/resources/test2.ttl",
42 "--output",
43 str(tmp_file),
44 ],
45 )
46 assert result.exit_code == 0
47 with open(str(tmp_file)) as file:
48 content = file.read()
49 assert len(content) > 1
50 # Fix issue with python creating unnecessary temp files on disk
51 for f in glob.glob("<tempfile._TemporaryFileWrapper*"):
52 os.remove(f)
53
54
55 # NOTE: Needs to run last tests, for some reason patching uvicorn as a side effects on follow up tests
56
57
58 @patch("rdflib_endpoint.__main__.uvicorn.run")
59 def test_serve(mock_run: MagicMock) -> None:
60 """Test serve, mock uvicorn.run to prevent API hanging"""
61 mock_run.return_value = None
62 result = runner.invoke(
63 cli,
64 [
65 "serve",
66 "tests/resources/test.nq",
67 "tests/resources/test2.ttl",
68 "tests/resources/another.jsonld",
69 ],
70 )
71 assert result.exit_code == 0
72
73
74 @patch("rdflib_endpoint.__main__.uvicorn.run")
75 def test_serve_oxigraph(mock_run: MagicMock) -> None:
76 """Test serve oxigraph, mock uvicorn.run to prevent API hanging"""
77 mock_run.return_value = None
78 result = runner.invoke(
79 cli,
80 [
81 "serve",
82 "--store",
83 "oxigraph",
84 "tests/resources/test.nq",
85 "tests/resources/test2.ttl",
86 "tests/resources/another.jsonld",
87 ],
88 )
89 assert result.exit_code == 0