Suggest --load-extension=spatialite, closes #1115

pull/1120/head
Simon Willison 2020-11-29 12:13:16 -08:00
rodzic 242bc89fdf
commit 09033c08be
2 zmienionych plików z 32 dodań i 9 usunięć

Wyświetl plik

@ -16,6 +16,7 @@ from .app import Datasette, DEFAULT_SETTINGS, SETTINGS, pm
from .utils import (
StartupError,
check_connection,
find_spatialite,
parse_metadata,
ConnectionProblem,
SpatialiteConnectionProblem,
@ -537,10 +538,17 @@ async def check_databases(ds):
try:
await database.execute_fn(check_connection)
except SpatialiteConnectionProblem:
suggestion = ""
try:
find_spatialite()
suggestion = "\n\nTry adding the --load-extension=spatialite option."
except SpatialiteNotFound:
pass
raise click.UsageError(
"It looks like you're trying to load a SpatiaLite"
" database without first loading the SpatiaLite module."
"\n\nRead more: https://docs.datasette.io/en/stable/spatialite.html"
+ " database without first loading the SpatiaLite module."
+ suggestion
+ "\n\nRead more: https://docs.datasette.io/en/stable/spatialite.html"
)
except ConnectionProblem as e:
raise click.UsageError(

Wyświetl plik

@ -59,13 +59,28 @@ def test_serve_with_inspect_file_prepopulates_table_counts_cache():
assert {"hithere": 44} == db.cached_table_counts
def test_spatialite_error_if_attempt_to_open_spatialite():
runner = CliRunner()
result = runner.invoke(
cli, ["serve", str(pathlib.Path(__file__).parent / "spatialite.db")]
)
assert result.exit_code != 0
assert "trying to load a SpatiaLite database" in result.output
@pytest.mark.parametrize(
"spatialite_paths,should_suggest_load_extension",
(
([], False),
(["/tmp"], True),
),
)
def test_spatialite_error_if_attempt_to_open_spatialite(
spatialite_paths, should_suggest_load_extension
):
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
@mock.patch("datasette.utils.SPATIALITE_PATHS", ["/does/not/exist"])