"datasette inspect foo.db" now just calculates table counts

Refs #462

* inspect command now just outputs table counts
* test_inspect.py is now only tests for that CLI command
* Updated some relevant documentation
* Removed docs for /-/inspect since that is about to change
This commit is contained in:
Simon Willison 2019-05-11 14:36:57 -07:00
commit c0d1b4c322
4 changed files with 58 additions and 130 deletions

View file

@ -1,92 +1,28 @@
from datasette.app import Datasette
from datasette.utils import sqlite3
import os
import pytest
import tempfile
from .fixtures import app_client
from datasette.cli import cli
from click.testing import CliRunner
import json
TABLES = """
CREATE TABLE "election_results" (
"county" INTEGER,
"party" INTEGER,
"office" INTEGER,
"votes" INTEGER,
FOREIGN KEY (county) REFERENCES county(id),
FOREIGN KEY (party) REFERENCES party(id),
FOREIGN KEY (office) REFERENCES office(id)
);
CREATE VIRTUAL TABLE "election_results_fts" USING FTS4 ("county", "party");
CREATE TABLE "county" (
"id" INTEGER PRIMARY KEY ,
"name" TEXT
);
CREATE TABLE "party" (
"id" INTEGER PRIMARY KEY ,
"name" TEXT
);
CREATE TABLE "office" (
"id" INTEGER PRIMARY KEY ,
"name" TEXT
);
"""
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,
"facetable": 15
}.items():
assert expected_count == database["tables"][table_name]["count"]
@pytest.fixture(scope="session")
def ds_instance():
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, "fixtures.db")
conn = sqlite3.connect(filepath)
conn.executescript(TABLES)
yield Datasette([filepath])
def test_inspect_hidden_tables(ds_instance):
info = ds_instance.inspect()
tables = info["fixtures"]["tables"]
expected_hidden = (
"election_results_fts",
"election_results_fts_content",
"election_results_fts_docsize",
"election_results_fts_segdir",
"election_results_fts_segments",
"election_results_fts_stat",
)
expected_visible = ("election_results", "county", "party", "office")
assert sorted(expected_hidden) == sorted(
[table for table in tables if tables[table]["hidden"]]
)
assert sorted(expected_visible) == sorted(
[table for table in tables if not tables[table]["hidden"]]
)
def test_inspect_foreign_keys(ds_instance):
info = ds_instance.inspect()
tables = info["fixtures"]["tables"]
for table_name in ("county", "party", "office"):
assert 0 == tables[table_name]["count"]
foreign_keys = tables[table_name]["foreign_keys"]
assert [] == foreign_keys["outgoing"]
assert [
{
"column": "id",
"other_column": table_name,
"other_table": "election_results",
}
] == foreign_keys["incoming"]
election_results = tables["election_results"]
assert 0 == election_results["count"]
assert sorted(
[
{"column": "county", "other_column": "id", "other_table": "county"},
{"column": "party", "other_column": "id", "other_table": "party"},
{"column": "office", "other_column": "id", "other_table": "office"},
],
key=lambda d: d["column"],
) == sorted(election_results["foreign_keys"]["outgoing"], key=lambda d: d["column"])
assert [] == election_results["foreign_keys"]["incoming"]
def test_inspect_cli_writes_to_file(app_client):
runner = CliRunner()
result = runner.invoke(cli, ["inspect", "fixtures.db", "--inspect-file", "foo.json"])
assert 0 == result.exit_code, result.output
data = json.load(open("foo.json"))
assert ["fixtures"] == list(data.keys())