Skip to content

Commit

Permalink
Fix for CSV labels=on missing foreign key bug, closes #2214
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Dec 22, 2023
1 parent 978249b commit 872dae1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
14 changes: 8 additions & 6 deletions datasette/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,16 +553,18 @@ async def stream_fn(r):
if cell is None:
new_row.extend(("", ""))
else:
assert isinstance(cell, dict)
new_row.append(cell["value"])
new_row.append(cell["label"])
if not isinstance(cell, dict):
new_row.extend((cell, ""))
else:
new_row.append(cell["value"])
new_row.append(cell["label"])
else:
new_row.append(cell)
await writer.writerow(new_row)
except Exception as e:
sys.stderr.write("Caught this error: {}\n".format(e))
except Exception as ex:
sys.stderr.write("Caught this error: {}\n".format(ex))
sys.stderr.flush()
await r.write(str(e))
await r.write(str(ex))
return
await limited_writer.write(postamble)

Expand Down
35 changes: 35 additions & 0 deletions tests/test_csv.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datasette.app import Datasette
from bs4 import BeautifulSoup as Soup
import pytest
from .fixtures import ( # noqa
Expand Down Expand Up @@ -95,6 +96,40 @@ async def test_table_csv_with_nullable_labels(ds_client):
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"
)


@pytest.mark.asyncio
async def test_table_csv_blob_columns(ds_client):
response = await ds_client.get("/fixtures/binary_data.csv")
Expand Down

0 comments on commit 872dae1

Please sign in to comment.