mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
First working -d based Datasette Library
Refs #417 First proof-of-concept for Datasette Library. Run like this: datasette -d ~/Library Uses a new plugin hook - available_databases() BUT... I don't think this is quite the way I want to go.
This commit is contained in:
parent
9c46f2f21f
commit
947645d847
15 changed files with 201 additions and 18 deletions
|
|
@ -321,6 +321,8 @@ METADATA = {
|
|||
|
||||
PLUGIN1 = """
|
||||
from datasette import hookimpl
|
||||
from datasette.database import Database
|
||||
from datasette.utils import sqlite3
|
||||
import base64
|
||||
import pint
|
||||
import json
|
||||
|
|
@ -397,6 +399,20 @@ def extra_template_vars(template, database, table, view_name, request, datasette
|
|||
"extra_serve_options": datasette.extra_serve_options,
|
||||
}, default=lambda b: b.decode("utf8"))
|
||||
}
|
||||
|
||||
|
||||
class SpecialDatabase(Database):
|
||||
def connect(self):
|
||||
db = sqlite3.connect(":memory:")
|
||||
db.executescript("CREATE TABLE foo (id integer primary key, bar text)")
|
||||
db.executescript("INSERT INTO foo (id, bar) VALUES (1, 'hello')")
|
||||
return db
|
||||
|
||||
@hookimpl
|
||||
def available_databases(datasette):
|
||||
return [
|
||||
("special", SpecialDatabase(datasette, name="special")),
|
||||
]
|
||||
"""
|
||||
|
||||
PLUGIN2 = """
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ def test_homepage(app_client):
|
|||
response = app_client.get("/.json")
|
||||
assert response.status == 200
|
||||
assert "application/json; charset=utf-8" == response.headers["content-type"]
|
||||
assert response.json.keys() == {"fixtures": 0}.keys()
|
||||
assert {"fixtures", "special"} == set(response.json.keys())
|
||||
d = response.json["fixtures"]
|
||||
assert d["name"] == "fixtures"
|
||||
assert d["tables_count"] == 24
|
||||
|
|
@ -518,19 +518,45 @@ def test_no_files_uses_memory_database(app_client_no_files):
|
|||
assert response.status == 200
|
||||
assert {
|
||||
":memory:": {
|
||||
"name": ":memory:",
|
||||
"hash": None,
|
||||
"comment": None,
|
||||
"color": "f7935d",
|
||||
"path": "/:memory:",
|
||||
"tables_and_views_truncated": [],
|
||||
"tables_and_views_more": False,
|
||||
"tables_count": 0,
|
||||
"table_rows_sum": 0,
|
||||
"show_table_row_counts": False,
|
||||
"hidden_table_rows_sum": 0,
|
||||
"hidden_tables_count": 0,
|
||||
"name": ":memory:",
|
||||
"show_table_row_counts": False,
|
||||
"path": "/:memory:",
|
||||
"table_rows_sum": 0,
|
||||
"tables_count": 0,
|
||||
"tables_and_views_more": False,
|
||||
"tables_and_views_truncated": [],
|
||||
"views_count": 0,
|
||||
}
|
||||
},
|
||||
"special": {
|
||||
"name": "special",
|
||||
"hash": None,
|
||||
"comment": None,
|
||||
"color": "0bd650",
|
||||
"path": "/special",
|
||||
"tables_and_views_truncated": [
|
||||
{
|
||||
"name": "foo",
|
||||
"columns": ["id", "bar"],
|
||||
"primary_keys": ["id"],
|
||||
"count": 1,
|
||||
"hidden": False,
|
||||
"fts_table": None,
|
||||
"num_relationships_for_sorting": 0,
|
||||
}
|
||||
],
|
||||
"tables_and_views_more": False,
|
||||
"tables_count": 1,
|
||||
"table_rows_sum": 1,
|
||||
"show_table_row_counts": True,
|
||||
"hidden_table_rows_sum": 0,
|
||||
"hidden_tables_count": 0,
|
||||
"views_count": 0,
|
||||
},
|
||||
} == response.json
|
||||
# Try that SQL query
|
||||
response = app_client_no_files.get(
|
||||
|
|
@ -1170,8 +1196,10 @@ def test_unit_filters(app_client):
|
|||
def test_databases_json(app_client_two_attached_databases_one_immutable):
|
||||
response = app_client_two_attached_databases_one_immutable.get("/-/databases.json")
|
||||
databases = response.json
|
||||
assert 2 == len(databases)
|
||||
extra_database, fixtures_database = databases
|
||||
assert 3 == len(databases)
|
||||
by_name = {database["name"]: database for database in databases}
|
||||
extra_database = by_name["extra_database"]
|
||||
fixtures_database = by_name["fixtures"]
|
||||
assert "extra_database" == extra_database["name"]
|
||||
assert None == extra_database["hash"]
|
||||
assert True == extra_database["is_mutable"]
|
||||
|
|
|
|||
|
|
@ -9,7 +9,6 @@ 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)
|
||||
|
|
@ -28,7 +27,7 @@ def test_inspect_cli_writes_to_file(app_client):
|
|||
)
|
||||
assert 0 == result.exit_code, result.output
|
||||
data = json.load(open("foo.json"))
|
||||
assert ["fixtures"] == list(data.keys())
|
||||
assert {"fixtures", "special"} == set(data.keys())
|
||||
|
||||
|
||||
def test_serve_with_inspect_file_prepopulates_table_counts_cache():
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ def test_homepage(app_client_two_attached_databases):
|
|||
assert [
|
||||
{"href": "/extra_database", "text": "extra_database"},
|
||||
{"href": "/fixtures", "text": "fixtures"},
|
||||
{"href": "/special", "text": "special"},
|
||||
] == [{"href": a["href"], "text": a.text.strip()} for a in soup.select("h2 a")]
|
||||
# The first attached database should show count text and attached tables
|
||||
h2 = soup.select("h2")[0]
|
||||
|
|
|
|||
|
|
@ -188,7 +188,7 @@ def test_plugins_extra_body_script(app_client, path, expected_extra_body_script)
|
|||
|
||||
def test_plugins_asgi_wrapper(app_client):
|
||||
response = app_client.get("/fixtures")
|
||||
assert "fixtures" == response.headers["x-databases"]
|
||||
assert "fixtures, special" == response.headers["x-databases"]
|
||||
|
||||
|
||||
def test_plugins_extra_template_vars(restore_working_directory):
|
||||
|
|
@ -228,3 +228,19 @@ def test_extra_serve_options_available_on_datasette(restore_working_directory):
|
|||
Soup(response.body, "html.parser").select("pre.extra_template_vars")[0].text
|
||||
)
|
||||
assert {"foo": "bar"} == extra_template_vars["extra_serve_options"]
|
||||
|
||||
|
||||
def test_plugins_available_databases(app_client):
|
||||
response = app_client.get("/-/databases.json")
|
||||
assert 200 == response.status
|
||||
assert {
|
||||
"name": "special",
|
||||
"path": None,
|
||||
"size": 0,
|
||||
"is_mutable": False,
|
||||
"is_memory": False,
|
||||
"hash": None,
|
||||
} in response.json
|
||||
assert [{"id": 1, "bar": "hello"}] == app_client.get(
|
||||
"/special/foo.json?_shape=array"
|
||||
).json
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue