Improved fixtures to support publication

The fixtures database created by our unit tests makes for a good "live" demo
of Datasette in action.

I've improved the metadata it ships with to better support this use-case.

I've also improved the mechanism for writing out fixtures: you can do this:

	python tests/fixtures.py fixtures.db

To get just the fixtures database written out... or you can do this:

	python tests/fixtures.py fixtures.db fixtures.json

To get metadata which you can then serve like so:

	datasette fixtures.db -m fixtures.json

Refs #313
This commit is contained in:
Simon Willison 2018-06-17 12:16:04 -07:00
commit 6a32df334d
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
2 changed files with 28 additions and 17 deletions

View file

@ -1,5 +1,6 @@
from datasette.app import Datasette from datasette.app import Datasette
import itertools import itertools
import json
import os import os
import pytest import pytest
import random import random
@ -98,12 +99,12 @@ def generate_sortable_rows(num):
METADATA = { METADATA = {
'title': 'Datasette Title', 'title': 'Datasette Fixtures',
'description': 'Datasette Description', 'description': 'An example SQLite database demonstrating Datasette',
'license': 'License', 'license': 'Apache License 2.0',
'license_url': 'http://www.example.com/license', 'license_url': 'https://github.com/simonw/datasette/blob/master/LICENSE',
'source': 'Source', 'source': 'tests/fixtures.py',
'source_url': 'http://www.example.com/source', 'source_url': 'https://github.com/simonw/datasette/blob/master/tests/fixtures.py',
'databases': { 'databases': {
'fixtures': { 'fixtures': {
'description': 'Test tables description', 'description': 'Test tables description',
@ -381,10 +382,20 @@ CREATE VIEW simple_view AS
]) ])
if __name__ == '__main__': if __name__ == '__main__':
filename = sys.argv[-1] # Can be called with data.db OR data.db metadata.json
if filename.endswith('.db'): db_filename = sys.argv[-1]
conn = sqlite3.connect(filename) metadata_filename = None
if db_filename.endswith(".json"):
metadata_filename = db_filename
db_filename = sys.argv[-2]
if db_filename.endswith(".db"):
conn = sqlite3.connect(db_filename)
conn.executescript(TABLES) conn.executescript(TABLES)
print('Test tables written to {}'.format(filename)) 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))
else: else:
print('Usage: {} name_of_file_to_write.db'.format(sys.argv[0])) print("Usage: {} db_to_write.db [metadata_to_write.json]".format(
sys.argv[0]
))

View file

@ -458,8 +458,8 @@ def test_index_metadata(app_client):
response = app_client.get('/') response = app_client.get('/')
assert response.status == 200 assert response.status == 200
soup = Soup(response.body, 'html.parser') soup = Soup(response.body, 'html.parser')
assert 'Datasette Title' == soup.find('h1').text assert 'Datasette Fixtures' == soup.find('h1').text
assert 'Datasette Description' == inner_html( assert 'An example SQLite database demonstrating Datasette' == inner_html(
soup.find('div', {'class': 'metadata-description'}) soup.find('div', {'class': 'metadata-description'})
) )
assert_footer_links(soup) assert_footer_links(soup)
@ -547,11 +547,11 @@ def assert_footer_links(soup):
assert 3 == len(footer_links) assert 3 == len(footer_links)
datasette_link, license_link, source_link = footer_links datasette_link, license_link, source_link = footer_links
assert 'Datasette' == datasette_link.text.strip() assert 'Datasette' == datasette_link.text.strip()
assert 'Source' == source_link.text.strip() assert 'tests/fixtures.py' == source_link.text.strip()
assert 'License' == license_link.text.strip() assert 'Apache License 2.0' == license_link.text.strip()
assert 'https://github.com/simonw/datasette' == datasette_link['href'] assert 'https://github.com/simonw/datasette' == datasette_link['href']
assert 'http://www.example.com/source' == source_link['href'] assert 'https://github.com/simonw/datasette/blob/master/tests/fixtures.py' == source_link['href']
assert 'http://www.example.com/license' == license_link['href'] assert 'https://github.com/simonw/datasette/blob/master/LICENSE' == license_link['href']
def inner_html(soup): def inner_html(soup):