mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Fix CSV export for nullable foreign keys, closes #612
This commit is contained in:
parent
c3181d9a84
commit
2bf7ce5f51
2 changed files with 23 additions and 4 deletions
|
|
@ -330,10 +330,14 @@ class DataView(BaseView):
|
|||
else:
|
||||
# Look for {"value": "label": } dicts and expand
|
||||
new_row = []
|
||||
for cell in row:
|
||||
if isinstance(cell, dict):
|
||||
new_row.append(cell["value"])
|
||||
new_row.append(cell["label"])
|
||||
for heading, cell in zip(data["columns"], row):
|
||||
if heading in expanded_columns:
|
||||
if cell is None:
|
||||
new_row.extend(("", ""))
|
||||
else:
|
||||
assert isinstance(cell, dict)
|
||||
new_row.append(cell["value"])
|
||||
new_row.append(cell["label"])
|
||||
else:
|
||||
new_row.append(cell)
|
||||
await writer.writerow(new_row)
|
||||
|
|
|
|||
|
|
@ -41,6 +41,14 @@ pk,created,planet_int,on_earth,state,city_id,city_id_label,neighborhood,tags,com
|
|||
"\n", "\r\n"
|
||||
)
|
||||
|
||||
EXPECTED_TABLE_WITH_NULLABLE_LABELS_CSV = """
|
||||
pk,foreign_key_with_label,foreign_key_with_label_label,foreign_key_with_no_label,foreign_key_with_no_label_label
|
||||
1,1,hello,1,1
|
||||
2,,,,
|
||||
""".lstrip().replace(
|
||||
"\n", "\r\n"
|
||||
)
|
||||
|
||||
|
||||
def test_table_csv(app_client):
|
||||
response = app_client.get("/fixtures/simple_primary_key.csv")
|
||||
|
|
@ -63,6 +71,13 @@ def test_table_csv_with_labels(app_client):
|
|||
assert EXPECTED_TABLE_WITH_LABELS_CSV == response.text
|
||||
|
||||
|
||||
def test_table_csv_with_nullable_labels(app_client):
|
||||
response = app_client.get("/fixtures/foreign_key_references.csv?_labels=1")
|
||||
assert response.status == 200
|
||||
assert "text/plain; charset=utf-8" == response.headers["content-type"]
|
||||
assert EXPECTED_TABLE_WITH_NULLABLE_LABELS_CSV == response.text
|
||||
|
||||
|
||||
def test_custom_sql_csv(app_client):
|
||||
response = app_client.get(
|
||||
"/fixtures.csv?sql=select+content+from+simple_primary_key+limit+2"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue