Skip to content

Commit

Permalink
serve --inspect-file=X now populates cached table counts
Browse files Browse the repository at this point in the history
Closes #462
  • Loading branch information
simonw committed May 16, 2019
1 parent 909e66d commit 21b57cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
8 changes: 7 additions & 1 deletion datasette/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ def __init__(self, ds, path=None, is_mutable=False, is_memory=False):
p = Path(path)
self.hash = inspect_hash(p)
self.cached_size = p.stat().st_size
# Maybe use self.ds.inspect_data to populate cached_table_counts
if self.ds.inspect_data and self.ds.inspect_data.get(self.name):
self.cached_table_counts = {
key: value["count"]
for key, value in self.ds.inspect_data[self.name]["tables"].items()
}

@property
def size(self):
Expand Down Expand Up @@ -310,6 +316,7 @@ def __init__(
elif memory:
self.files = (MEMORY,) + self.files
self.databases = {}
self.inspect_data = inspect_data
for file in self.files:
path = file
is_memory = False
Expand All @@ -325,7 +332,6 @@ def __init__(
self.databases[db.name] = db
self.cache_headers = cache_headers
self.cors = cors
self._inspect = inspect_data
self._metadata = metadata or {}
self.sqlite_functions = []
self.sqlite_extensions = sqlite_extensions or []
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def make_app_client(
filename="fixtures.db",
is_immutable=False,
extra_databases=None,
inspect_data=None,
):
with tempfile.TemporaryDirectory() as tmpdir:
filepath = os.path.join(tmpdir, filename)
Expand Down Expand Up @@ -71,6 +72,7 @@ def make_app_client(
metadata=METADATA,
plugins_dir=plugins_dir,
config=config,
inspect_data=inspect_data,
)
ds.sqlite_functions.append(("sleep", 1, lambda n: time.sleep(float(n))))
client = TestClient(ds.app().test_client)
Expand Down
10 changes: 9 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .fixtures import app_client
from .fixtures import app_client, make_app_client
from datasette.cli import cli
from click.testing import CliRunner
import pathlib
Expand Down Expand Up @@ -31,6 +31,14 @@ def test_inspect_cli_writes_to_file(app_client):
assert ["fixtures"] == list(data.keys())


def test_serve_with_inspect_file_prepopulates_table_counts_cache():
inspect_data = {"fixtures": {"tables": {"hithere": {"count": 44}}}}
for client in make_app_client(inspect_data=inspect_data, is_immutable=True):
assert inspect_data == client.ds.inspect_data
db = client.ds.databases["fixtures"]
assert {"hithere": 44} == db.cached_table_counts


def test_spatialite_error_if_attempt_to_open_spatialite():
runner = CliRunner()
result = runner.invoke(
Expand Down

0 comments on commit 21b57cd

Please sign in to comment.