From 18705a797aa1e0863a977c764c02f972172b94b6 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Thu, 2 Apr 2020 11:51:43 -0700 Subject: [PATCH] Test for --metadata=x.yaml --- datasette/cli.py | 5 +++++ tests/test_cli.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/datasette/cli.py b/datasette/cli.py index 9328ec08..0ed5e287 100644 --- a/datasette/cli.py +++ b/datasette/cli.py @@ -327,6 +327,7 @@ def serve( config, version_note, help_config, + return_instance=False, ): """Serve up specified SQLite database files with a web UI""" if help_config: @@ -375,6 +376,10 @@ def serve( memory=memory, version_note=version_note, ) + if return_instance: + # Private utility mechanism for writing unit tests + return ds + # Run async sanity checks - but only if we're not under pytest asyncio.get_event_loop().run_until_complete(check_databases(ds)) diff --git a/tests/test_cli.py b/tests/test_cli.py index d1ab6522..ac5746c6 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1,8 +1,10 @@ -from .fixtures import app_client, make_app_client -from datasette.cli import cli +from .fixtures import app_client, make_app_client, TestClient as _TestClient +from datasette.cli import cli, serve from click.testing import CliRunner -import pathlib +import io import json +import pathlib +import textwrap def test_inspect_cli(app_client): @@ -46,3 +48,38 @@ def test_spatialite_error_if_attempt_to_open_spatialite(): ) assert result.exit_code != 0 assert "trying to load a SpatiaLite database" in result.output + + +def test_metadata_yaml(): + yaml_file = io.StringIO( + textwrap.dedent( + """ + title: Hello from YAML + """ + ) + ) + # Annoyingly we have to provide all default arguments here: + ds = serve.callback( + [], + metadata=yaml_file, + immutable=[], + host="127.0.0.1", + port=8001, + debug=False, + reload=False, + cors=False, + sqlite_extensions=[], + inspect_file=None, + template_dir=None, + plugins_dir=None, + static=[], + memory=False, + config=[], + version_note=None, + help_config=False, + return_instance=True, + ) + client = _TestClient(ds.app()) + client.ds = ds + response = client.get("/-/metadata.json") + assert {"title": "Hello from YAML"} == response.json