| 1 |
from datasette.app import Datasette |
| 2 |
from bs4 import BeautifulSoup as Soup |
| 3 |
from .fixtures import ( # noqa |
| 4 |
app_client, |
| 5 |
app_client_csv_max_mb_one, |
| 6 |
app_client_with_cors, |
| 7 |
app_client_with_trace, |
| 8 |
) |
| 9 |
import pytest |
| 10 |
import urllib.parse |
| 11 |
|
| 12 |
EXPECTED_TABLE_CSV = """id,content |
| 13 |
1,hello |
| 14 |
2,world |
| 15 |
3, |
| 16 |
4,RENDER_CELL_DEMO |
| 17 |
5,RENDER_CELL_ASYNC |
| 18 |
""".replace( |
| 19 |
"\n", "\r\n" |
| 20 |
) |
| 21 |
|
| 22 |
EXPECTED_CUSTOM_CSV = """content |
| 23 |
hello |
| 24 |
world |
| 25 |
""".replace( |
| 26 |
"\n", "\r\n" |
| 27 |
) |
| 28 |
|
| 29 |
EXPECTED_TABLE_WITH_LABELS_CSV = """ |
| 30 |
pk,created,planet_int,on_earth,state,_city_id,_city_id_label,_neighborhood,tags,complex_array,distinct_some_null,n |
| 31 |
1,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Mission,"[""tag1"", ""tag2""]","[{""foo"": ""bar""}]",one,n1 |
| 32 |
2,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Dogpatch,"[""tag1"", ""tag3""]",[],two,n2 |
| 33 |
3,2019-01-14 08:00:00,1,1,CA,1,San Francisco,SOMA,[],[],, |
| 34 |
4,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Tenderloin,[],[],, |
| 35 |
5,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Bernal Heights,[],[],, |
| 36 |
6,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Hayes Valley,[],[],, |
| 37 |
7,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Hollywood,[],[],, |
| 38 |
8,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Downtown,[],[],, |
| 39 |
9,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Los Feliz,[],[],, |
| 40 |
10,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Koreatown,[],[],, |
| 41 |
11,2019-01-16 08:00:00,1,1,MI,3,Detroit,Downtown,[],[],, |
| 42 |
12,2019-01-17 08:00:00,1,1,MI,3,Detroit,Greektown,[],[],, |
| 43 |
13,2019-01-17 08:00:00,1,1,MI,3,Detroit,Corktown,[],[],, |
| 44 |
14,2019-01-17 08:00:00,1,1,MI,3,Detroit,Mexicantown,[],[],, |
| 45 |
15,2019-01-17 08:00:00,2,0,MC,4,Memnonia,Arcadia Planitia,[],[],, |
| 46 |
""".lstrip().replace( |
| 47 |
"\n", "\r\n" |
| 48 |
) |
| 49 |
|
| 50 |
EXPECTED_TABLE_WITH_NULLABLE_LABELS_CSV = """ |
| 51 |
pk,foreign_key_with_label,foreign_key_with_label_label,foreign_key_with_blank_label,foreign_key_with_blank_label_label,foreign_key_with_no_label,foreign_key_with_no_label_label,foreign_key_compound_pk1,foreign_key_compound_pk2 |
| 52 |
1,1,hello,3,,1,1,a,b |
| 53 |
2,,,,,,,, |
| 54 |
""".lstrip().replace( |
| 55 |
"\n", "\r\n" |
| 56 |
) |
| 57 |
|
| 58 |
|
| 59 |
def test_table_csv(app_client): |
| 60 |
response = app_client.get("/fixtures/simple_primary_key.csv?_oh=1") |
| 61 |
assert response.status == 200 |
| 62 |
assert not response.headers.get("Access-Control-Allow-Origin") |
| 63 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 64 |
assert response.text == EXPECTED_TABLE_CSV |
| 65 |
|
| 66 |
|
| 67 |
def test_table_csv_cors_headers(app_client_with_cors): |
| 68 |
response = app_client_with_cors.get("/fixtures/simple_primary_key.csv") |
| 69 |
assert response.status == 200 |
| 70 |
assert response.headers["Access-Control-Allow-Origin"] == "*" |
| 71 |
|
| 72 |
|
| 73 |
def test_table_csv_no_header(app_client): |
| 74 |
response = app_client.get("/fixtures/simple_primary_key.csv?_header=off") |
| 75 |
assert response.status == 200 |
| 76 |
assert not response.headers.get("Access-Control-Allow-Origin") |
| 77 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 78 |
assert response.text == EXPECTED_TABLE_CSV.split("\r\n", 1)[1] |
| 79 |
|
| 80 |
|
| 81 |
def test_table_csv_with_labels(app_client): |
| 82 |
response = app_client.get("/fixtures/facetable.csv?_labels=1") |
| 83 |
assert response.status == 200 |
| 84 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 85 |
assert response.text == EXPECTED_TABLE_WITH_LABELS_CSV |
| 86 |
|
| 87 |
|
| 88 |
def test_table_csv_with_nullable_labels(app_client): |
| 89 |
response = app_client.get("/fixtures/foreign_key_references.csv?_labels=1") |
| 90 |
assert response.status == 200 |
| 91 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 92 |
assert response.text == EXPECTED_TABLE_WITH_NULLABLE_LABELS_CSV |
| 93 |
|
| 94 |
|
| 95 |
@pytest.mark.asyncio |
| 96 |
async def test_table_csv_with_invalid_labels(): |
| 97 |
# https://github.com/simonw/datasette/issues/2214 |
| 98 |
ds = Datasette() |
| 99 |
await ds.invoke_startup() |
| 100 |
db = ds.add_memory_database("db_2214") |
| 101 |
await db.execute_write_script( |
| 102 |
""" |
| 103 |
create table t1 (id integer primary key, name text); |
| 104 |
insert into t1 (id, name) values (1, 'one'); |
| 105 |
insert into t1 (id, name) values (2, 'two'); |
| 106 |
create table t2 (textid text primary key, name text); |
| 107 |
insert into t2 (textid, name) values ('a', 'alpha'); |
| 108 |
insert into t2 (textid, name) values ('b', 'beta'); |
| 109 |
create table if not exists maintable ( |
| 110 |
id integer primary key, |
| 111 |
fk_integer integer references t1(id), |
| 112 |
fk_text text references t2(textid) |
| 113 |
); |
| 114 |
insert into maintable (id, fk_integer, fk_text) values (1, 1, 'a'); |
| 115 |
insert into maintable (id, fk_integer, fk_text) values (2, 3, 'b'); -- invalid fk_integer |
| 116 |
insert into maintable (id, fk_integer, fk_text) values (3, 2, 'c'); -- invalid fk_text |
| 117 |
""" |
| 118 |
) |
| 119 |
response = await ds.client.get("/db_2214/maintable.csv?_labels=1") |
| 120 |
assert response.status_code == 200 |
| 121 |
assert response.text == ( |
| 122 |
"id,fk_integer,fk_integer_label,fk_text,fk_text_label\r\n" |
| 123 |
"1,1,one,a,alpha\r\n" |
| 124 |
"2,3,,b,beta\r\n" |
| 125 |
"3,2,two,c,\r\n" |
| 126 |
) |
| 127 |
|
| 128 |
|
| 129 |
def test_table_csv_blob_columns(app_client): |
| 130 |
response = app_client.get("/fixtures/binary_data.csv") |
| 131 |
assert response.status == 200 |
| 132 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 133 |
assert response.text == ( |
| 134 |
"rowid,data\r\n" |
| 135 |
"1,http://localhost/fixtures/binary_data/1.blob?_blob_column=data\r\n" |
| 136 |
"2,http://localhost/fixtures/binary_data/2.blob?_blob_column=data\r\n" |
| 137 |
"3,\r\n" |
| 138 |
) |
| 139 |
|
| 140 |
|
| 141 |
def test_custom_sql_csv_blob_columns(app_client): |
| 142 |
response = app_client.get("/fixtures.csv?sql=select+rowid,+data+from+binary_data") |
| 143 |
assert response.status == 200 |
| 144 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 145 |
assert response.text == ( |
| 146 |
"rowid,data\r\n" |
| 147 |
'1,"http://localhost/fixtures.blob?sql=select+rowid,+data+from+binary_data&_blob_column=data&_blob_hash=f3088978da8f9aea479ffc7f631370b968d2e855eeb172bea7f6c7a04262bb6d"\r\n' |
| 148 |
'2,"http://localhost/fixtures.blob?sql=select+rowid,+data+from+binary_data&_blob_column=data&_blob_hash=b835b0483cedb86130b9a2c280880bf5fadc5318ddf8c18d0df5204d40df1724"\r\n' |
| 149 |
"3,\r\n" |
| 150 |
) |
| 151 |
|
| 152 |
|
| 153 |
def test_custom_sql_csv(app_client): |
| 154 |
response = app_client.get( |
| 155 |
"/fixtures.csv?sql=select+content+from+simple_primary_key+limit+2" |
| 156 |
) |
| 157 |
assert response.status == 200 |
| 158 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 159 |
assert response.text == EXPECTED_CUSTOM_CSV |
| 160 |
|
| 161 |
|
| 162 |
def test_table_csv_download(app_client): |
| 163 |
response = app_client.get("/fixtures/simple_primary_key.csv?_dl=1") |
| 164 |
assert response.status == 200 |
| 165 |
assert response.headers["content-type"] == "text/csv; charset=utf-8" |
| 166 |
assert ( |
| 167 |
response.headers["content-disposition"] |
| 168 |
== 'attachment; filename="simple_primary_key.csv"' |
| 169 |
) |
| 170 |
|
| 171 |
|
| 172 |
def test_csv_with_non_ascii_characters(app_client): |
| 173 |
response = app_client.get( |
| 174 |
"/fixtures.csv?sql=select%0D%0A++%27%F0%9D%90%9C%F0%9D%90%A2%F0%9D%90%AD%F0%9D%90%A2%F0%9D%90%9E%F0%9D%90%AC%27+as+text%2C%0D%0A++1+as+number%0D%0Aunion%0D%0Aselect%0D%0A++%27bob%27+as+text%2C%0D%0A++2+as+number%0D%0Aorder+by%0D%0A++number" |
| 175 |
) |
| 176 |
assert response.status == 200 |
| 177 |
assert response.headers["content-type"] == "text/plain; charset=utf-8" |
| 178 |
assert response.text == "text,number\r\nšš¢šš¢šš¬,1\r\nbob,2\r\n" |
| 179 |
|
| 180 |
|
| 181 |
def test_max_csv_mb(app_client_csv_max_mb_one): |
| 182 |
# This query deliberately generates a really long string |
| 183 |
# should be 100*100*100*2 = roughly 2MB |
| 184 |
response = app_client_csv_max_mb_one.get( |
| 185 |
"/fixtures.csv?" |
| 186 |
+ urllib.parse.urlencode( |
| 187 |
{ |
| 188 |
"sql": """ |
| 189 |
select group_concat('ab', '') |
| 190 |
from json_each(json_array({lots})), |
| 191 |
json_each(json_array({lots})), |
| 192 |
json_each(json_array({lots})) |
| 193 |
""".format( |
| 194 |
lots=", ".join(str(i) for i in range(100)) |
| 195 |
), |
| 196 |
"_stream": 1, |
| 197 |
"_size": "max", |
| 198 |
} |
| 199 |
), |
| 200 |
) |
| 201 |
# It's a 200 because we started streaming before we knew the error |
| 202 |
assert response.status == 200 |
| 203 |
# Last line should be an error message |
| 204 |
last_line = [line for line in response.body.split(b"\r\n") if line][-1] |
| 205 |
assert last_line.startswith(b"CSV contains more than") |
| 206 |
|
| 207 |
|
| 208 |
def test_table_csv_stream(app_client): |
| 209 |
# Without _stream should return header + 100 rows: |
| 210 |
response = app_client.get("/fixtures/compound_three_primary_keys.csv?_size=max") |
| 211 |
assert len([b for b in response.body.split(b"\r\n") if b]) == 101 |
| 212 |
# With _stream=1 should return header + 1001 rows |
| 213 |
response = app_client.get("/fixtures/compound_three_primary_keys.csv?_stream=1") |
| 214 |
assert len([b for b in response.body.split(b"\r\n") if b]) == 1002 |
| 215 |
|
| 216 |
|
| 217 |
def test_csv_trace(app_client_with_trace): |
| 218 |
response = app_client_with_trace.get("/fixtures/simple_primary_key.csv?_trace=1") |
| 219 |
assert response.headers["content-type"] == "text/html; charset=utf-8" |
| 220 |
soup = Soup(response.text, "html.parser") |
| 221 |
assert ( |
| 222 |
soup.find("textarea").text |
| 223 |
== "id,content\r\n1,hello\r\n2,world\r\n3,\r\n4,RENDER_CELL_DEMO\r\n5,RENDER_CELL_ASYNC\r\n" |
| 224 |
) |
| 225 |
assert "select id, content from simple_primary_key" in soup.find("pre").text |
| 226 |
|
| 227 |
|
| 228 |
def test_table_csv_stream_does_not_calculate_facets(app_client_with_trace): |
| 229 |
response = app_client_with_trace.get("/fixtures/simple_primary_key.csv?_trace=1") |
| 230 |
soup = Soup(response.text, "html.parser") |
| 231 |
assert "select content, count(*) as n" not in soup.find("pre").text |
| 232 |
|
| 233 |
|
| 234 |
def test_table_csv_stream_does_not_calculate_counts(app_client_with_trace): |
| 235 |
response = app_client_with_trace.get("/fixtures/simple_primary_key.csv?_trace=1") |
| 236 |
soup = Soup(response.text, "html.parser") |
| 237 |
assert "select count(*)" not in soup.find("pre").text |