mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Fixed tests/fixtures.py, closes #804
This commit is contained in:
parent
56eb80a459
commit
f240970b83
2 changed files with 65 additions and 45 deletions
|
|
@ -70,11 +70,20 @@ You can also use the ``fixtures.py`` script to recreate the testing version of `
|
||||||
|
|
||||||
python tests/fixtures.py fixtures.db fixtures-metadata.json
|
python tests/fixtures.py fixtures.db fixtures-metadata.json
|
||||||
|
|
||||||
(You may need to delete ``fixtures.db`` before running this command.)
|
Or to output the plugins used by the tests, run this::
|
||||||
|
|
||||||
|
python tests/fixtures.py fixtures.db fixtures-metadata.json fixtures-plugins
|
||||||
|
Test tables written to fixtures.db
|
||||||
|
- metadata written to fixtures-metadata.json
|
||||||
|
Wrote plugin: fixtures-plugins/register_output_renderer.py
|
||||||
|
Wrote plugin: fixtures-plugins/view_name.py
|
||||||
|
Wrote plugin: fixtures-plugins/my_plugin.py
|
||||||
|
Wrote plugin: fixtures-plugins/messages_output_renderer.py
|
||||||
|
Wrote plugin: fixtures-plugins/my_plugin_2.py
|
||||||
|
|
||||||
Then run Datasette like this::
|
Then run Datasette like this::
|
||||||
|
|
||||||
datasette fixtures.db -m fixtures-metadata.json
|
datasette fixtures.db -m fixtures-metadata.json --plugins-dir=fixtures-plugins/
|
||||||
|
|
||||||
.. _contributing_documentation:
|
.. _contributing_documentation:
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ from datasette.app import Datasette
|
||||||
from datasette.utils import sqlite3, MultiParams
|
from datasette.utils import sqlite3, MultiParams
|
||||||
from asgiref.testing import ApplicationCommunicator
|
from asgiref.testing import ApplicationCommunicator
|
||||||
from asgiref.sync import async_to_sync
|
from asgiref.sync import async_to_sync
|
||||||
|
import click
|
||||||
import contextlib
|
import contextlib
|
||||||
from http.cookies import SimpleCookie
|
from http.cookies import SimpleCookie
|
||||||
import itertools
|
import itertools
|
||||||
|
|
@ -813,49 +814,6 @@ INSERT INTO "searchable_fts" (rowid, text1, text2)
|
||||||
SELECT rowid, text1, text2 FROM searchable;
|
SELECT rowid, text1, text2 FROM searchable;
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
# Can be called with data.db OR data.db metadata.json
|
|
||||||
arg_index = -1
|
|
||||||
db_filename = sys.argv[arg_index]
|
|
||||||
metadata_filename = None
|
|
||||||
plugins_path = None
|
|
||||||
if db_filename.endswith("/"):
|
|
||||||
# It's the plugins dir
|
|
||||||
plugins_path = db_filename
|
|
||||||
arg_index -= 1
|
|
||||||
db_filename = sys.argv[arg_index]
|
|
||||||
if db_filename.endswith(".json"):
|
|
||||||
metadata_filename = db_filename
|
|
||||||
arg_index -= 1
|
|
||||||
db_filename = sys.argv[arg_index]
|
|
||||||
if db_filename.endswith(".db"):
|
|
||||||
conn = sqlite3.connect(db_filename)
|
|
||||||
conn.executescript(TABLES)
|
|
||||||
for sql, params in TABLE_PARAMETERIZED_SQL:
|
|
||||||
with conn:
|
|
||||||
conn.execute(sql, params)
|
|
||||||
print("Test tables written to {}".format(db_filename))
|
|
||||||
if metadata_filename:
|
|
||||||
open(metadata_filename, "w").write(json.dumps(METADATA))
|
|
||||||
print("- metadata written to {}".format(metadata_filename))
|
|
||||||
if plugins_path:
|
|
||||||
path = pathlib.Path(plugins_path)
|
|
||||||
if not path.exists():
|
|
||||||
path.mkdir()
|
|
||||||
for filename, content in (
|
|
||||||
("my_plugin.py", PLUGIN1),
|
|
||||||
("my_plugin_2.py", PLUGIN2),
|
|
||||||
):
|
|
||||||
filepath = path / filename
|
|
||||||
filepath.write_text(content)
|
|
||||||
print(" Wrote plugin: {}".format(filepath))
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
"Usage: {} db_to_write.db [metadata_to_write.json] [plugins-dir/]".format(
|
|
||||||
sys.argv[0]
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def assert_permissions_checked(datasette, actions):
|
def assert_permissions_checked(datasette, actions):
|
||||||
# actions is a list of "action" or (action, resource) tuples
|
# actions is a list of "action" or (action, resource) tuples
|
||||||
|
|
@ -873,3 +831,56 @@ def assert_permissions_checked(datasette, actions):
|
||||||
""".format(
|
""".format(
|
||||||
action, resource, json.dumps(list(datasette._permission_checks), indent=4),
|
action, resource, json.dumps(list(datasette._permission_checks), indent=4),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@click.command()
|
||||||
|
@click.argument(
|
||||||
|
"db_filename",
|
||||||
|
default="fixtures.db",
|
||||||
|
type=click.Path(file_okay=True, dir_okay=False),
|
||||||
|
)
|
||||||
|
@click.argument("metadata", required=False)
|
||||||
|
@click.argument(
|
||||||
|
"plugins_path", type=click.Path(file_okay=False, dir_okay=True), required=False
|
||||||
|
)
|
||||||
|
@click.option(
|
||||||
|
"--recreate",
|
||||||
|
is_flag=True,
|
||||||
|
default=False,
|
||||||
|
help="Delete and recreate database if it exists",
|
||||||
|
)
|
||||||
|
def cli(db_filename, metadata, plugins_path, recreate):
|
||||||
|
"Write out the fixtures database used by Datasette's test suite"
|
||||||
|
if metadata and not metadata.endswith(".json"):
|
||||||
|
raise click.ClickException("Metadata should end with .json")
|
||||||
|
if not db_filename.endswith(".db"):
|
||||||
|
raise click.ClickException("Database file should end with .db")
|
||||||
|
if pathlib.Path(db_filename).exists():
|
||||||
|
if not recreate:
|
||||||
|
raise click.ClickException(
|
||||||
|
"{} already exists, use --recreate to reset it".format(db_filename)
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
pathlib.Path(db_filename).unlink()
|
||||||
|
conn = sqlite3.connect(db_filename)
|
||||||
|
conn.executescript(TABLES)
|
||||||
|
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||||
|
with conn:
|
||||||
|
conn.execute(sql, params)
|
||||||
|
print("Test tables written to {}".format(db_filename))
|
||||||
|
if metadata:
|
||||||
|
open(metadata, "w").write(json.dumps(METADATA, indent=4))
|
||||||
|
print("- metadata written to {}".format(metadata))
|
||||||
|
if plugins_path:
|
||||||
|
path = pathlib.Path(plugins_path)
|
||||||
|
if not path.exists():
|
||||||
|
path.mkdir()
|
||||||
|
test_plugins = pathlib.Path(__file__).parent / "plugins"
|
||||||
|
for filepath in test_plugins.glob("*.py"):
|
||||||
|
newpath = path / filepath.name
|
||||||
|
newpath.write_text(filepath.open().read())
|
||||||
|
print(" Wrote plugin: {}".format(newpath))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
cli()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue