mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Improved .add_database() method design
Closes #1155 - _internal now has a sensible name Closes #509 - Support opening multiple databases with the same stem
This commit is contained in:
parent
270de6527b
commit
8919f99c2f
5 changed files with 86 additions and 46 deletions
|
|
@ -8,6 +8,7 @@ import asyncio
|
|||
from datasette.plugins import DEFAULT_PLUGINS
|
||||
from datasette.cli import cli, serve
|
||||
from datasette.version import __version__
|
||||
from datasette.utils.sqlite import sqlite3
|
||||
from click.testing import CliRunner
|
||||
import io
|
||||
import json
|
||||
|
|
@ -240,3 +241,17 @@ def test_serve_create(ensure_eventloop, tmpdir):
|
|||
"hash": None,
|
||||
}.items() <= databases[0].items()
|
||||
assert db_path.exists()
|
||||
|
||||
|
||||
def test_serve_duplicate_database_names(ensure_eventloop, tmpdir):
|
||||
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"}
|
||||
|
|
|
|||
|
|
@ -439,7 +439,7 @@ async def test_execute_write_fn_connection_exception(tmpdir, app_client):
|
|||
path = str(tmpdir / "immutable.db")
|
||||
sqlite3.connect(path).execute("vacuum")
|
||||
db = Database(app_client.ds, path=path, is_mutable=False)
|
||||
app_client.ds.add_database("immutable-db", db)
|
||||
app_client.ds.add_database(db, name="immutable-db")
|
||||
|
||||
def write_fn(conn):
|
||||
assert False
|
||||
|
|
@ -469,10 +469,10 @@ def test_is_mutable(app_client):
|
|||
@pytest.mark.asyncio
|
||||
async def test_database_memory_name(app_client):
|
||||
ds = app_client.ds
|
||||
foo1 = Database(ds, memory_name="foo")
|
||||
foo2 = Database(ds, memory_name="foo")
|
||||
bar1 = Database(ds, memory_name="bar")
|
||||
bar2 = Database(ds, memory_name="bar")
|
||||
foo1 = ds.add_database(Database(ds, memory_name="foo"))
|
||||
foo2 = ds.add_database(Database(ds, memory_name="foo"))
|
||||
bar1 = ds.add_database(Database(ds, memory_name="bar"))
|
||||
bar2 = ds.add_database(Database(ds, memory_name="bar"))
|
||||
for db in (foo1, foo2, bar1, bar2):
|
||||
table_names = await db.table_names()
|
||||
assert table_names == []
|
||||
|
|
@ -487,7 +487,7 @@ async def test_database_memory_name(app_client):
|
|||
@pytest.mark.asyncio
|
||||
async def test_in_memory_databases_forbid_writes(app_client):
|
||||
ds = app_client.ds
|
||||
db = Database(ds, memory_name="test")
|
||||
db = ds.add_database(Database(ds, memory_name="test"))
|
||||
with pytest.raises(sqlite3.OperationalError):
|
||||
await db.execute("create table foo (t text)")
|
||||
assert await db.table_names() == []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue