Support for :memory: databases

If you start Datasette with no files, it will connect to :memory: instead.

When starting it with files you can add --memory to also get a :memory: database.
This commit is contained in:
Simon Willison 2019-03-14 16:42:38 -07:00
commit 9743e1d91b
6 changed files with 90 additions and 33 deletions

View file

@ -65,6 +65,14 @@ def app_client():
yield from make_app_client()
@pytest.fixture(scope="session")
def app_client_no_files():
ds = Datasette([])
client = TestClient(ds.app().test_client)
client.ds = ds
yield client
@pytest.fixture(scope='session')
def app_client_shorter_time_limit():
yield from make_app_client(20)

View file

@ -1,5 +1,6 @@
from .fixtures import ( # noqa
app_client,
app_client_no_files,
app_client_shorter_time_limit,
app_client_larger_cache_size,
app_client_returned_rows_matches_page_size,
@ -368,6 +369,31 @@ def test_database_page(app_client):
}] == data['tables']
def test_no_files_uses_memory_database(app_client_no_files):
response = app_client_no_files.get("/.json")
assert response.status == 200
assert {
":memory:": {
"hash": "000",
"hidden_table_rows_sum": 0,
"hidden_tables_count": 0,
"name": ":memory:",
"path": ":memory:-000",
"table_rows_sum": 0,
"tables_count": 0,
"tables_more": False,
"tables_truncated": [],
"views_count": 0,
}
} == response.json
# Try that SQL query
response = app_client_no_files.get(
"/:memory:-0.json?sql=select+sqlite_version()&_shape=array"
)
assert 1 == len(response.json)
assert ["sqlite_version()"] == list(response.json[0].keys())
def test_database_page_for_database_with_dot_in_name(app_client_with_dot):
response = app_client_with_dot.get("/fixtures.dot.json")
assert 200 == response.status