mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Unit test for binary data display, refs #442
This commit is contained in:
parent
d555baf508
commit
01b3de5b66
4 changed files with 41 additions and 2 deletions
|
|
@ -121,7 +121,9 @@ class RowTableShared(BaseView):
|
||||||
if plugin_display_value is not None:
|
if plugin_display_value is not None:
|
||||||
display_value = plugin_display_value
|
display_value = plugin_display_value
|
||||||
elif isinstance(value, bytes):
|
elif isinstance(value, bytes):
|
||||||
display_value = jinja2.Markup("<{} bytes>".format(len(value)))
|
display_value = jinja2.Markup("<Binary data: {} byte{}>".format(
|
||||||
|
len(value), "" if len(value) == 1 else "s")
|
||||||
|
)
|
||||||
elif isinstance(value, dict):
|
elif isinstance(value, dict):
|
||||||
# It's an expanded foreign key - display link to other row
|
# It's an expanded foreign key - display link to other row
|
||||||
label = value["label"]
|
label = value["label"]
|
||||||
|
|
|
||||||
|
|
@ -36,6 +36,9 @@ def make_app_client(
|
||||||
filepath = os.path.join(tmpdir, filename)
|
filepath = os.path.join(tmpdir, filename)
|
||||||
conn = sqlite3.connect(filepath)
|
conn = sqlite3.connect(filepath)
|
||||||
conn.executescript(TABLES)
|
conn.executescript(TABLES)
|
||||||
|
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||||
|
with conn:
|
||||||
|
conn.execute(sql, params)
|
||||||
os.chdir(os.path.dirname(filepath))
|
os.chdir(os.path.dirname(filepath))
|
||||||
plugins_dir = os.path.join(tmpdir, "plugins")
|
plugins_dir = os.path.join(tmpdir, "plugins")
|
||||||
os.mkdir(plugins_dir)
|
os.mkdir(plugins_dir)
|
||||||
|
|
@ -550,6 +553,10 @@ VALUES
|
||||||
(2, 0, 'MC', 4, 'Arcadia Planitia', '[]')
|
(2, 0, 'MC', 4, 'Arcadia Planitia', '[]')
|
||||||
;
|
;
|
||||||
|
|
||||||
|
CREATE TABLE binary_data (
|
||||||
|
data BLOB
|
||||||
|
);
|
||||||
|
|
||||||
INSERT INTO simple_primary_key VALUES (1, 'hello');
|
INSERT INTO simple_primary_key VALUES (1, 'hello');
|
||||||
INSERT INTO simple_primary_key VALUES (2, 'world');
|
INSERT INTO simple_primary_key VALUES (2, 'world');
|
||||||
INSERT INTO simple_primary_key VALUES (3, '');
|
INSERT INTO simple_primary_key VALUES (3, '');
|
||||||
|
|
@ -589,6 +596,9 @@ CREATE VIEW searchable_view_configured_by_metadata AS
|
||||||
**row
|
**row
|
||||||
).replace('None', 'null') for row in generate_sortable_rows(201)
|
).replace('None', 'null') for row in generate_sortable_rows(201)
|
||||||
])
|
])
|
||||||
|
TABLE_PARAMETERIZED_SQL = [(
|
||||||
|
"insert into binary_data (data) values (?);", [b'this is binary data']
|
||||||
|
)]
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Can be called with data.db OR data.db metadata.json
|
# Can be called with data.db OR data.db metadata.json
|
||||||
|
|
@ -600,6 +610,9 @@ if __name__ == '__main__':
|
||||||
if db_filename.endswith(".db"):
|
if db_filename.endswith(".db"):
|
||||||
conn = sqlite3.connect(db_filename)
|
conn = sqlite3.connect(db_filename)
|
||||||
conn.executescript(TABLES)
|
conn.executescript(TABLES)
|
||||||
|
for sql, params in TABLE_PARAMETERIZED_SQL:
|
||||||
|
with conn:
|
||||||
|
conn.execute(sql, params)
|
||||||
print("Test tables written to {}".format(db_filename))
|
print("Test tables written to {}".format(db_filename))
|
||||||
if metadata_filename:
|
if metadata_filename:
|
||||||
open(metadata_filename, 'w').write(json.dumps(METADATA))
|
open(metadata_filename, 'w').write(json.dumps(METADATA))
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ def test_homepage(app_client):
|
||||||
assert response.json.keys() == {"fixtures": 0}.keys()
|
assert response.json.keys() == {"fixtures": 0}.keys()
|
||||||
d = response.json["fixtures"]
|
d = response.json["fixtures"]
|
||||||
assert d["name"] == "fixtures"
|
assert d["name"] == "fixtures"
|
||||||
assert d["tables_count"] == 25
|
assert d["tables_count"] == 26
|
||||||
assert len(d["tables_truncated"]) == 5
|
assert len(d["tables_truncated"]) == 5
|
||||||
assert d["tables_more"] is True
|
assert d["tables_more"] is True
|
||||||
# 4 hidden FTS tables + no_primary_key (hidden in metadata)
|
# 4 hidden FTS tables + no_primary_key (hidden in metadata)
|
||||||
|
|
@ -53,6 +53,14 @@ def test_database_page(app_client):
|
||||||
'foreign_keys': {'incoming': [], 'outgoing': []},
|
'foreign_keys': {'incoming': [], 'outgoing': []},
|
||||||
'fts_table': None,
|
'fts_table': None,
|
||||||
'primary_keys': ['pk'],
|
'primary_keys': ['pk'],
|
||||||
|
}, {
|
||||||
|
'columns': ['data'],
|
||||||
|
'count': 1,
|
||||||
|
'foreign_keys': {'incoming': [], 'outgoing': []},
|
||||||
|
'fts_table': None,
|
||||||
|
'hidden': False,
|
||||||
|
'name': 'binary_data',
|
||||||
|
'primary_keys': []
|
||||||
}, {
|
}, {
|
||||||
'columns': ['pk', 'f1', 'f2', 'f3'],
|
'columns': ['pk', 'f1', 'f2', 'f3'],
|
||||||
'name': 'complex_foreign_keys',
|
'name': 'complex_foreign_keys',
|
||||||
|
|
|
||||||
|
|
@ -883,3 +883,19 @@ def test_extra_where_clauses(app_client):
|
||||||
"/fixtures/facetable?_where=city_id%3D1",
|
"/fixtures/facetable?_where=city_id%3D1",
|
||||||
"/fixtures/facetable?_where=neighborhood%3D%27Dogpatch%27"
|
"/fixtures/facetable?_where=neighborhood%3D%27Dogpatch%27"
|
||||||
] == hrefs
|
] == hrefs
|
||||||
|
|
||||||
|
|
||||||
|
def test_binary_data_display(app_client):
|
||||||
|
response = app_client.get("/fixtures/binary_data")
|
||||||
|
assert response.status == 200
|
||||||
|
table = Soup(response.body, "html.parser").find("table")
|
||||||
|
expected_tds = [
|
||||||
|
[
|
||||||
|
'<td class="col-Link"><a href="/fixtures/binary_data/1">1</a></td>',
|
||||||
|
'<td class="col-rowid">1</td>',
|
||||||
|
'<td class="col-data"><Binary\xa0data:\xa019\xa0bytes></td>'
|
||||||
|
]
|
||||||
|
]
|
||||||
|
assert expected_tds == [
|
||||||
|
[str(td) for td in tr.select("td")] for tr in table.select("tbody tr")
|
||||||
|
]
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue