--get now calls startup() plugin hooks, closes #934

pull/936/head
Simon Willison 2020-08-15 13:38:15 -07:00
rodzic 7702ea6021
commit 45414f8412
4 zmienionych plików z 59 dodań i 21 usunięć

Wyświetl plik

@ -416,12 +416,6 @@ def serve(
ds = Datasette(files, **kwargs)
if get:
client = TestClient(ds.app())
response = client.get(get)
click.echo(response.text)
return
if return_instance:
# Private utility mechanism for writing unit tests
return ds
@ -432,6 +426,12 @@ def serve(
# Run async sanity checks - but only if we're not under pytest
asyncio.get_event_loop().run_until_complete(check_databases(ds))
if get:
client = TestClient(ds.app())
response = client.get(get)
click.echo(response.text)
return
# Start the server
if root:
print("http://{}:{}/-/auth-token?token={}".format(host, port, ds._root_token))

Wyświetl plik

@ -25,9 +25,11 @@ def pytest_unconfigure(config):
def pytest_collection_modifyitems(items):
# Ensure test_black.py and test_inspect.py run first before any asyncio code kicks in
# Ensure test_cli.py and test_black.py and test_inspect.py run first before any asyncio code kicks in
move_to_front(items, "test_cli")
move_to_front(items, "test_black")
move_to_front(items, "test_inspect_cli")
move_to_front(items, "test_serve_with_get")
move_to_front(items, "test_inspect_cli_writes_to_file")
move_to_front(items, "test_spatialite_error_if_attempt_to_open_spatialite")
move_to_front(items, "test_package")

Wyświetl plik

@ -50,20 +50,6 @@ def test_serve_with_inspect_file_prepopulates_table_counts_cache():
assert {"hithere": 44} == db.cached_table_counts
def test_serve_with_get():
runner = CliRunner()
result = runner.invoke(
cli,
["serve", "--memory", "--get", "/:memory:.json?sql=select+sqlite_version()"],
)
assert 0 == result.exit_code, result.output
assert {
"database": ":memory:",
"truncated": False,
"columns": ["sqlite_version()"],
}.items() <= json.loads(result.output).items()
def test_spatialite_error_if_attempt_to_open_spatialite():
runner = CliRunner()
result = runner.invoke(

Wyświetl plik

@ -0,0 +1,50 @@
from datasette.cli import cli, serve
from datasette.plugins import pm
from click.testing import CliRunner
import textwrap
import json
def test_serve_with_get(tmp_path_factory):
plugins_dir = tmp_path_factory.mktemp("plugins_for_serve_with_get")
(plugins_dir / "init_for_serve_with_get.py").write_text(
textwrap.dedent(
"""
from datasette import hookimpl
@hookimpl
def startup(datasette):
open("{}", "w").write("hello")
""".format(
str(plugins_dir / "hello.txt")
),
),
"utf-8",
)
runner = CliRunner()
result = runner.invoke(
cli,
[
"serve",
"--memory",
"--plugins-dir",
str(plugins_dir),
"--get",
"/:memory:.json?sql=select+sqlite_version()",
],
)
assert 0 == result.exit_code, result.output
assert {
"database": ":memory:",
"truncated": False,
"columns": ["sqlite_version()"],
}.items() <= json.loads(result.output).items()
# The plugin should have created hello.txt
assert (plugins_dir / "hello.txt").read_text() == "hello"
# Annoyingly that new test plugin stays resident - we need
# to manually unregister it to avoid conflict with other tests
to_unregister = [
p for p in pm.get_plugins() if p.__name__ == "init_for_serve_with_get.py"
][0]
pm.unregister(to_unregister)