mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Backport of CSV labels fix from #2214
This commit is contained in:
parent
5800eaeb5b
commit
324090919d
2 changed files with 41 additions and 3 deletions
|
|
@ -308,9 +308,11 @@ class DataView(BaseView):
|
||||||
if cell is None:
|
if cell is None:
|
||||||
new_row.extend(("", ""))
|
new_row.extend(("", ""))
|
||||||
else:
|
else:
|
||||||
assert isinstance(cell, dict)
|
if not isinstance(cell, dict):
|
||||||
new_row.append(cell["value"])
|
new_row.extend((cell, ""))
|
||||||
new_row.append(cell["label"])
|
else:
|
||||||
|
new_row.append(cell["value"])
|
||||||
|
new_row.append(cell["label"])
|
||||||
else:
|
else:
|
||||||
new_row.append(cell)
|
new_row.append(cell)
|
||||||
await writer.writerow(new_row)
|
await writer.writerow(new_row)
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
from datasette.app import Datasette
|
||||||
from bs4 import BeautifulSoup as Soup
|
from bs4 import BeautifulSoup as Soup
|
||||||
from .fixtures import ( # noqa
|
from .fixtures import ( # noqa
|
||||||
app_client,
|
app_client,
|
||||||
|
|
@ -5,6 +6,7 @@ from .fixtures import ( # noqa
|
||||||
app_client_with_cors,
|
app_client_with_cors,
|
||||||
app_client_with_trace,
|
app_client_with_trace,
|
||||||
)
|
)
|
||||||
|
import pytest
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
EXPECTED_TABLE_CSV = """id,content
|
EXPECTED_TABLE_CSV = """id,content
|
||||||
|
|
@ -90,6 +92,40 @@ def test_table_csv_with_nullable_labels(app_client):
|
||||||
assert response.text == EXPECTED_TABLE_WITH_NULLABLE_LABELS_CSV
|
assert response.text == EXPECTED_TABLE_WITH_NULLABLE_LABELS_CSV
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_table_csv_with_invalid_labels():
|
||||||
|
# https://github.com/simonw/datasette/issues/2214
|
||||||
|
ds = Datasette()
|
||||||
|
await ds.invoke_startup()
|
||||||
|
db = ds.add_memory_database("db_2214")
|
||||||
|
await db.execute_write_script(
|
||||||
|
"""
|
||||||
|
create table t1 (id integer primary key, name text);
|
||||||
|
insert into t1 (id, name) values (1, 'one');
|
||||||
|
insert into t1 (id, name) values (2, 'two');
|
||||||
|
create table t2 (textid text primary key, name text);
|
||||||
|
insert into t2 (textid, name) values ('a', 'alpha');
|
||||||
|
insert into t2 (textid, name) values ('b', 'beta');
|
||||||
|
create table if not exists maintable (
|
||||||
|
id integer primary key,
|
||||||
|
fk_integer integer references t1(id),
|
||||||
|
fk_text text references t2(textid)
|
||||||
|
);
|
||||||
|
insert into maintable (id, fk_integer, fk_text) values (1, 1, 'a');
|
||||||
|
insert into maintable (id, fk_integer, fk_text) values (2, 3, 'b'); -- invalid fk_integer
|
||||||
|
insert into maintable (id, fk_integer, fk_text) values (3, 2, 'c'); -- invalid fk_text
|
||||||
|
"""
|
||||||
|
)
|
||||||
|
response = await ds.client.get("/db_2214/maintable.csv?_labels=1")
|
||||||
|
assert response.status_code == 200
|
||||||
|
assert response.text == (
|
||||||
|
"id,fk_integer,fk_integer_label,fk_text,fk_text_label\r\n"
|
||||||
|
"1,1,one,a,alpha\r\n"
|
||||||
|
"2,3,,b,beta\r\n"
|
||||||
|
"3,2,two,c,\r\n"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_table_csv_blob_columns(app_client):
|
def test_table_csv_blob_columns(app_client):
|
||||||
response = app_client.get("/fixtures/binary_data.csv")
|
response = app_client.get("/fixtures/binary_data.csv")
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue