2020-06-05 16:46:37 -07:00
|
|
|
from .fixtures import (
|
|
|
|
|
app_client,
|
|
|
|
|
make_app_client,
|
|
|
|
|
TestClient as _TestClient,
|
|
|
|
|
EXPECTED_PLUGINS,
|
|
|
|
|
)
|
2021-08-12 18:01:57 -07:00
|
|
|
from datasette.app import SETTINGS
|
2020-06-05 16:55:08 -07:00
|
|
|
from datasette.plugins import DEFAULT_PLUGINS
|
2020-04-02 12:30:53 -07:00
|
|
|
from datasette.cli import cli, serve
|
2020-10-28 20:38:15 -07:00
|
|
|
from datasette.version import __version__
|
2022-03-15 11:01:57 -07:00
|
|
|
from datasette.utils import tilde_encode
|
2020-12-22 12:04:18 -08:00
|
|
|
from datasette.utils.sqlite import sqlite3
|
2019-05-11 14:36:57 -07:00
|
|
|
from click.testing import CliRunner
|
2020-04-02 12:30:53 -07:00
|
|
|
import io
|
2019-05-11 14:36:57 -07:00
|
|
|
import json
|
2020-04-02 12:30:53 -07:00
|
|
|
import pathlib
|
2020-06-13 10:55:41 -07:00
|
|
|
import pytest
|
2020-08-11 16:54:52 -07:00
|
|
|
import sys
|
2020-04-02 12:30:53 -07:00
|
|
|
import textwrap
|
2020-08-11 15:31:47 -07:00
|
|
|
from unittest import mock
|
2019-05-11 14:36:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_inspect_cli(app_client):
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(cli, ["inspect", "fixtures.db"])
|
|
|
|
|
data = json.loads(result.output)
|
|
|
|
|
assert ["fixtures"] == list(data.keys())
|
|
|
|
|
database = data["fixtures"]
|
|
|
|
|
assert "fixtures.db" == database["file"]
|
|
|
|
|
assert isinstance(database["hash"], str)
|
|
|
|
|
assert 64 == len(database["hash"])
|
|
|
|
|
for table_name, expected_count in {
|
|
|
|
|
"Table With Space In Name": 0,
|
2019-05-11 14:45:59 -07:00
|
|
|
"facetable": 15,
|
2019-05-11 14:36:57 -07:00
|
|
|
}.items():
|
|
|
|
|
assert expected_count == database["tables"][table_name]["count"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_inspect_cli_writes_to_file(app_client):
|
|
|
|
|
runner = CliRunner()
|
2019-05-11 14:45:59 -07:00
|
|
|
result = runner.invoke(
|
|
|
|
|
cli, ["inspect", "fixtures.db", "--inspect-file", "foo.json"]
|
|
|
|
|
)
|
2019-05-11 14:36:57 -07:00
|
|
|
assert 0 == result.exit_code, result.output
|
2021-03-11 17:15:49 +01:00
|
|
|
with open("foo.json") as fp:
|
|
|
|
|
data = json.load(fp)
|
2019-05-11 14:36:57 -07:00
|
|
|
assert ["fixtures"] == list(data.keys())
|
2019-05-11 15:55:30 -07:00
|
|
|
|
|
|
|
|
|
2019-05-16 07:30:32 -07:00
|
|
|
def test_serve_with_inspect_file_prepopulates_table_counts_cache():
|
|
|
|
|
inspect_data = {"fixtures": {"tables": {"hithere": {"count": 44}}}}
|
2020-06-07 14:14:10 -07:00
|
|
|
with make_app_client(inspect_data=inspect_data, is_immutable=True) as client:
|
2019-05-16 07:30:32 -07:00
|
|
|
assert inspect_data == client.ds.inspect_data
|
|
|
|
|
db = client.ds.databases["fixtures"]
|
|
|
|
|
assert {"hithere": 44} == db.cached_table_counts
|
|
|
|
|
|
|
|
|
|
|
2020-11-29 12:13:16 -08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"spatialite_paths,should_suggest_load_extension",
|
|
|
|
|
(
|
|
|
|
|
([], False),
|
|
|
|
|
(["/tmp"], True),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
def test_spatialite_error_if_attempt_to_open_spatialite(
|
2022-09-05 17:40:19 -07:00
|
|
|
spatialite_paths, should_suggest_load_extension
|
2020-11-29 12:13:16 -08:00
|
|
|
):
|
|
|
|
|
with mock.patch("datasette.utils.SPATIALITE_PATHS", spatialite_paths):
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli, ["serve", str(pathlib.Path(__file__).parent / "spatialite.db")]
|
|
|
|
|
)
|
|
|
|
|
assert result.exit_code != 0
|
|
|
|
|
assert "It looks like you're trying to load a SpatiaLite" in result.output
|
|
|
|
|
suggestion = "--load-extension=spatialite"
|
|
|
|
|
if should_suggest_load_extension:
|
|
|
|
|
assert suggestion in result.output
|
|
|
|
|
else:
|
|
|
|
|
assert suggestion not in result.output
|
2020-04-02 12:30:53 -07:00
|
|
|
|
|
|
|
|
|
2020-10-19 15:37:31 -07:00
|
|
|
@mock.patch("datasette.utils.SPATIALITE_PATHS", ["/does/not/exist"])
|
|
|
|
|
def test_spatialite_error_if_cannot_find_load_extension_spatialite():
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(
|
2020-10-19 17:33:59 -07:00
|
|
|
cli,
|
|
|
|
|
[
|
|
|
|
|
"serve",
|
|
|
|
|
str(pathlib.Path(__file__).parent / "spatialite.db"),
|
|
|
|
|
"--load-extension",
|
|
|
|
|
"spatialite",
|
|
|
|
|
],
|
2020-10-19 15:37:31 -07:00
|
|
|
)
|
|
|
|
|
assert result.exit_code != 0
|
|
|
|
|
assert "Could not find SpatiaLite extension" in result.output
|
|
|
|
|
|
|
|
|
|
|
2020-06-05 16:46:37 -07:00
|
|
|
def test_plugins_cli(app_client):
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result1 = runner.invoke(cli, ["plugins"])
|
2024-01-31 15:21:40 -08:00
|
|
|
actual_plugins = sorted(
|
|
|
|
|
[p for p in json.loads(result1.output) if p["name"] != "TrackEventPlugin"],
|
|
|
|
|
key=lambda p: p["name"],
|
|
|
|
|
)
|
|
|
|
|
assert actual_plugins == EXPECTED_PLUGINS
|
2020-06-05 16:46:37 -07:00
|
|
|
# Try with --all
|
|
|
|
|
result2 = runner.invoke(cli, ["plugins", "--all"])
|
|
|
|
|
names = [p["name"] for p in json.loads(result2.output)]
|
|
|
|
|
# Should have all the EXPECTED_PLUGINS
|
2020-12-23 18:04:32 +01:00
|
|
|
assert set(names).issuperset({p["name"] for p in EXPECTED_PLUGINS})
|
2020-06-05 16:46:37 -07:00
|
|
|
# And the following too:
|
2020-06-05 16:55:08 -07:00
|
|
|
assert set(names).issuperset(DEFAULT_PLUGINS)
|
2023-08-09 15:04:16 -07:00
|
|
|
# --requirements should be empty because there are no installed non-plugins-dir plugins
|
|
|
|
|
result3 = runner.invoke(cli, ["plugins", "--requirements"])
|
|
|
|
|
assert result3.output == ""
|
2020-06-05 16:46:37 -07:00
|
|
|
|
|
|
|
|
|
2020-04-02 12:30:53 -07:00
|
|
|
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,
|
2021-07-10 16:37:30 -07:00
|
|
|
uds=None,
|
2020-04-02 12:30:53 -07:00
|
|
|
reload=False,
|
|
|
|
|
cors=False,
|
|
|
|
|
sqlite_extensions=[],
|
|
|
|
|
inspect_file=None,
|
|
|
|
|
template_dir=None,
|
|
|
|
|
plugins_dir=None,
|
|
|
|
|
static=[],
|
|
|
|
|
memory=False,
|
|
|
|
|
config=[],
|
2020-11-24 12:01:47 -08:00
|
|
|
settings=[],
|
2020-05-31 15:42:08 -07:00
|
|
|
secret=None,
|
2020-05-31 18:03:17 -07:00
|
|
|
root=False,
|
2022-12-12 21:00:40 -08:00
|
|
|
token=None,
|
2023-08-28 13:14:48 -07:00
|
|
|
actor=None,
|
2020-04-02 12:30:53 -07:00
|
|
|
version_note=None,
|
2020-08-11 17:24:40 -07:00
|
|
|
get=None,
|
2021-08-12 18:01:57 -07:00
|
|
|
help_settings=False,
|
2020-09-11 11:37:55 -07:00
|
|
|
pdb=False,
|
2021-02-18 14:09:12 -08:00
|
|
|
crossdb=False,
|
2022-05-17 12:59:28 -07:00
|
|
|
nolock=False,
|
2020-09-22 08:37:59 -07:00
|
|
|
open_browser=False,
|
2020-12-09 11:45:45 -08:00
|
|
|
create=False,
|
2021-02-11 16:52:16 -08:00
|
|
|
ssl_keyfile=None,
|
|
|
|
|
ssl_certfile=None,
|
2020-04-02 12:30:53 -07:00
|
|
|
return_instance=True,
|
2023-08-28 20:24:23 -07:00
|
|
|
internal=None,
|
2020-04-02 12:30:53 -07:00
|
|
|
)
|
2020-10-09 09:11:24 -07:00
|
|
|
client = _TestClient(ds)
|
2020-04-02 12:30:53 -07:00
|
|
|
response = client.get("/-/metadata.json")
|
|
|
|
|
assert {"title": "Hello from YAML"} == response.json
|
2020-08-11 15:31:47 -07:00
|
|
|
|
|
|
|
|
|
2020-08-11 16:54:52 -07:00
|
|
|
@mock.patch("datasette.cli.run_module")
|
|
|
|
|
def test_install(run_module):
|
2020-08-11 15:31:47 -07:00
|
|
|
runner = CliRunner()
|
|
|
|
|
runner.invoke(cli, ["install", "datasette-mock-plugin", "datasette-mock-plugin2"])
|
2020-08-11 16:54:52 -07:00
|
|
|
run_module.assert_called_once_with("pip", run_name="__main__")
|
|
|
|
|
assert sys.argv == [
|
|
|
|
|
"pip",
|
|
|
|
|
"install",
|
|
|
|
|
"datasette-mock-plugin",
|
|
|
|
|
"datasette-mock-plugin2",
|
|
|
|
|
]
|
2020-08-11 15:31:47 -07:00
|
|
|
|
|
|
|
|
|
2020-08-19 10:20:41 -07:00
|
|
|
@pytest.mark.parametrize("flag", ["-U", "--upgrade"])
|
|
|
|
|
@mock.patch("datasette.cli.run_module")
|
|
|
|
|
def test_install_upgrade(run_module, flag):
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
runner.invoke(cli, ["install", flag, "datasette"])
|
|
|
|
|
run_module.assert_called_once_with("pip", run_name="__main__")
|
|
|
|
|
assert sys.argv == ["pip", "install", "--upgrade", "datasette"]
|
|
|
|
|
|
|
|
|
|
|
2023-03-06 14:27:30 -08:00
|
|
|
@mock.patch("datasette.cli.run_module")
|
2023-03-08 12:33:23 -08:00
|
|
|
def test_install_requirements(run_module, tmpdir):
|
|
|
|
|
path = tmpdir.join("requirements.txt")
|
|
|
|
|
path.write("datasette-mock-plugin\ndatasette-plugin-2")
|
2023-03-06 14:27:30 -08:00
|
|
|
runner = CliRunner()
|
2023-03-08 12:33:23 -08:00
|
|
|
runner.invoke(cli, ["install", "-r", str(path)])
|
2023-03-06 14:27:30 -08:00
|
|
|
run_module.assert_called_once_with("pip", run_name="__main__")
|
2023-03-08 12:33:23 -08:00
|
|
|
assert sys.argv == ["pip", "install", "-r", str(path)]
|
2023-03-06 14:27:30 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_install_error_if_no_packages():
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(cli, ["install"])
|
|
|
|
|
assert result.exit_code == 2
|
|
|
|
|
assert "Error: Please specify at least one package to install" in result.output
|
|
|
|
|
|
|
|
|
|
|
2020-08-11 16:54:52 -07:00
|
|
|
@mock.patch("datasette.cli.run_module")
|
|
|
|
|
def test_uninstall(run_module):
|
2020-08-11 15:31:47 -07:00
|
|
|
runner = CliRunner()
|
|
|
|
|
runner.invoke(cli, ["uninstall", "datasette-mock-plugin", "-y"])
|
2020-08-11 16:54:52 -07:00
|
|
|
run_module.assert_called_once_with("pip", run_name="__main__")
|
|
|
|
|
assert sys.argv == ["pip", "uninstall", "datasette-mock-plugin", "-y"]
|
2020-10-28 20:38:15 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_version():
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(cli, ["--version"])
|
2020-11-15 15:24:22 -08:00
|
|
|
assert result.output == f"cli, version {__version__}\n"
|
2020-11-24 12:01:47 -08:00
|
|
|
|
|
|
|
|
|
2021-02-18 10:05:27 -08:00
|
|
|
@pytest.mark.parametrize("invalid_port", ["-1", "0.5", "dog", "65536"])
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_serve_invalid_ports(invalid_port):
|
2021-02-18 10:05:27 -08:00
|
|
|
runner = CliRunner(mix_stderr=False)
|
|
|
|
|
result = runner.invoke(cli, ["--port", invalid_port])
|
|
|
|
|
assert result.exit_code == 2
|
|
|
|
|
assert "Invalid value for '-p'" in result.stderr
|
|
|
|
|
|
|
|
|
|
|
2023-08-28 13:06:14 -07:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"args",
|
|
|
|
|
(
|
|
|
|
|
["--setting", "default_page_size", "5"],
|
|
|
|
|
["--setting", "settings.default_page_size", "5"],
|
|
|
|
|
["-s", "settings.default_page_size", "5"],
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
def test_setting(args):
|
2020-11-24 12:01:47 -08:00
|
|
|
runner = CliRunner()
|
2023-08-28 13:06:14 -07:00
|
|
|
result = runner.invoke(cli, ["--get", "/-/settings.json"] + args)
|
2020-11-24 12:01:47 -08:00
|
|
|
assert result.exit_code == 0, result.output
|
2023-08-28 13:06:14 -07:00
|
|
|
settings = json.loads(result.output)
|
|
|
|
|
assert settings["default_page_size"] == 5
|
2020-11-24 12:01:47 -08:00
|
|
|
|
|
|
|
|
|
2023-09-13 14:06:25 -07:00
|
|
|
def test_plugin_s_overwrite():
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
plugins_dir = str(pathlib.Path(__file__).parent / "plugins")
|
|
|
|
|
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli,
|
|
|
|
|
[
|
|
|
|
|
"--plugins-dir",
|
|
|
|
|
plugins_dir,
|
|
|
|
|
"--get",
|
|
|
|
|
"/_memory.json?sql=select+prepare_connection_args()",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
assert (
|
|
|
|
|
json.loads(result.output).get("rows")[0].get("prepare_connection_args()")
|
|
|
|
|
== 'database=_memory, datasette.plugin_config("name-of-plugin")=None'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli,
|
|
|
|
|
[
|
|
|
|
|
"--plugins-dir",
|
|
|
|
|
plugins_dir,
|
|
|
|
|
"--get",
|
|
|
|
|
"/_memory.json?sql=select+prepare_connection_args()",
|
|
|
|
|
"-s",
|
|
|
|
|
"plugins.name-of-plugin",
|
|
|
|
|
"OVERRIDE",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
assert (
|
|
|
|
|
json.loads(result.output).get("rows")[0].get("prepare_connection_args()")
|
|
|
|
|
== 'database=_memory, datasette.plugin_config("name-of-plugin")=OVERRIDE'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_setting_type_validation():
|
2020-11-24 12:01:47 -08:00
|
|
|
runner = CliRunner(mix_stderr=False)
|
|
|
|
|
result = runner.invoke(cli, ["--setting", "default_page_size", "dog"])
|
|
|
|
|
assert result.exit_code == 2
|
2023-08-28 13:06:14 -07:00
|
|
|
assert '"settings.default_page_size" should be an integer' in result.stderr
|
2020-11-24 12:01:47 -08:00
|
|
|
|
|
|
|
|
|
2023-01-04 16:51:11 -08:00
|
|
|
@pytest.mark.parametrize("default_allow_sql", (True, False))
|
|
|
|
|
def test_setting_default_allow_sql(default_allow_sql):
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli,
|
|
|
|
|
[
|
|
|
|
|
"--setting",
|
|
|
|
|
"default_allow_sql",
|
|
|
|
|
"on" if default_allow_sql else "off",
|
|
|
|
|
"--get",
|
|
|
|
|
"/_memory.json?sql=select+21&_shape=objects",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
if default_allow_sql:
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
assert json.loads(result.output)["rows"][0] == {"21": 21}
|
|
|
|
|
else:
|
|
|
|
|
assert result.exit_code == 1, result.output
|
|
|
|
|
# This isn't JSON at the moment, maybe it should be though
|
|
|
|
|
assert "Forbidden" in result.output
|
|
|
|
|
|
|
|
|
|
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_sql_errors_logged_to_stderr():
|
2020-12-04 21:21:11 -08:00
|
|
|
runner = CliRunner(mix_stderr=False)
|
2021-01-28 14:48:56 -08:00
|
|
|
result = runner.invoke(cli, ["--get", "/_memory.json?sql=select+blah"])
|
2020-12-04 21:21:11 -08:00
|
|
|
assert result.exit_code == 1
|
|
|
|
|
assert "sql = 'select blah', params = {}: no such column: blah\n" in result.stderr
|
2020-12-09 11:45:45 -08:00
|
|
|
|
|
|
|
|
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_serve_create(tmpdir):
|
2020-12-09 11:45:45 -08:00
|
|
|
runner = CliRunner()
|
|
|
|
|
db_path = tmpdir / "does_not_exist_yet.db"
|
|
|
|
|
assert not db_path.exists()
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli, [str(db_path), "--create", "--get", "/-/databases.json"]
|
|
|
|
|
)
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
databases = json.loads(result.output)
|
|
|
|
|
assert {
|
|
|
|
|
"name": "does_not_exist_yet",
|
|
|
|
|
"is_mutable": True,
|
|
|
|
|
"is_memory": False,
|
|
|
|
|
"hash": None,
|
|
|
|
|
}.items() <= databases[0].items()
|
|
|
|
|
assert db_path.exists()
|
2020-12-22 12:04:18 -08:00
|
|
|
|
|
|
|
|
|
2023-08-22 19:33:26 -07:00
|
|
|
@pytest.mark.parametrize("argument", ("-c", "--config"))
|
|
|
|
|
@pytest.mark.parametrize("format_", ("json", "yaml"))
|
|
|
|
|
def test_serve_config(tmpdir, argument, format_):
|
|
|
|
|
config_path = tmpdir / "datasette.{}".format(format_)
|
|
|
|
|
config_path.write_text(
|
2024-01-30 19:55:26 -08:00
|
|
|
(
|
|
|
|
|
"settings:\n default_page_size: 5\n"
|
|
|
|
|
if format_ == "yaml"
|
|
|
|
|
else '{"settings": {"default_page_size": 5}}'
|
|
|
|
|
),
|
2023-08-22 19:33:26 -07:00
|
|
|
"utf-8",
|
|
|
|
|
)
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli,
|
|
|
|
|
[
|
|
|
|
|
argument,
|
|
|
|
|
str(config_path),
|
|
|
|
|
"--get",
|
|
|
|
|
"/-/settings.json",
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
assert json.loads(result.output)["default_page_size"] == 5
|
|
|
|
|
|
|
|
|
|
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_serve_duplicate_database_names(tmpdir):
|
2022-02-07 15:28:46 -08:00
|
|
|
"'datasette db.db nested/db.db' should attach two databases, /db and /db_2"
|
2020-12-22 12:04:18 -08:00
|
|
|
runner = CliRunner()
|
|
|
|
|
db_1_path = str(tmpdir / "db.db")
|
|
|
|
|
nested = tmpdir / "nested"
|
|
|
|
|
nested.mkdir()
|
|
|
|
|
db_2_path = str(tmpdir / "nested" / "db.db")
|
|
|
|
|
for path in (db_1_path, db_2_path):
|
|
|
|
|
sqlite3.connect(path).execute("vacuum")
|
|
|
|
|
result = runner.invoke(cli, [db_1_path, db_2_path, "--get", "/-/databases.json"])
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
databases = json.loads(result.output)
|
|
|
|
|
assert {db["name"] for db in databases} == {"db", "db_2"}
|
2021-01-24 21:13:05 -08:00
|
|
|
|
|
|
|
|
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_serve_deduplicate_same_database_path(tmpdir):
|
2022-02-07 15:28:46 -08:00
|
|
|
"'datasette db.db db.db' should only attach one database, /db"
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
db_path = str(tmpdir / "db.db")
|
|
|
|
|
sqlite3.connect(db_path).execute("vacuum")
|
|
|
|
|
result = runner.invoke(cli, [db_path, db_path, "--get", "/-/databases.json"])
|
|
|
|
|
assert result.exit_code == 0, result.output
|
|
|
|
|
databases = json.loads(result.output)
|
|
|
|
|
assert {db["name"] for db in databases} == {"db"}
|
|
|
|
|
|
|
|
|
|
|
2021-01-24 21:13:05 -08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
"filename", ["test-database (1).sqlite", "database (1).sqlite"]
|
|
|
|
|
)
|
2022-09-05 17:40:19 -07:00
|
|
|
def test_weird_database_names(tmpdir, filename):
|
2021-01-24 21:13:05 -08:00
|
|
|
# https://github.com/simonw/datasette/issues/1181
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
db_path = str(tmpdir / filename)
|
|
|
|
|
sqlite3.connect(db_path).execute("vacuum")
|
|
|
|
|
result1 = runner.invoke(cli, [db_path, "--get", "/"])
|
|
|
|
|
assert result1.exit_code == 0, result1.output
|
|
|
|
|
filename_no_stem = filename.rsplit(".", 1)[0]
|
|
|
|
|
expected_link = '<a href="/{}">{}</a>'.format(
|
2022-03-15 11:01:57 -07:00
|
|
|
tilde_encode(filename_no_stem), filename_no_stem
|
2021-01-24 21:13:05 -08:00
|
|
|
)
|
|
|
|
|
assert expected_link in result1.output
|
|
|
|
|
# Now try hitting that database page
|
|
|
|
|
result2 = runner.invoke(
|
2022-03-15 11:01:57 -07:00
|
|
|
cli, [db_path, "--get", "/{}".format(tilde_encode(filename_no_stem))]
|
2021-01-24 21:13:05 -08:00
|
|
|
)
|
|
|
|
|
assert result2.exit_code == 0, result2.output
|
2021-08-12 18:01:57 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_help_settings():
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
result = runner.invoke(cli, ["--help-settings"])
|
|
|
|
|
for setting in SETTINGS:
|
|
|
|
|
assert setting.name in result.output
|
2023-08-28 20:24:23 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_internal_db(tmpdir):
|
|
|
|
|
runner = CliRunner()
|
|
|
|
|
internal_path = tmpdir / "internal.db"
|
|
|
|
|
assert not internal_path.exists()
|
|
|
|
|
result = runner.invoke(
|
|
|
|
|
cli, ["--memory", "--internal", str(internal_path), "--get", "/"]
|
|
|
|
|
)
|
|
|
|
|
assert result.exit_code == 0
|
|
|
|
|
assert internal_path.exists()
|