2019-05-04 02:15:14 +00:00
|
|
|
|
from .fixtures import ( # noqa
|
2018-06-24 00:59:37 +00:00
|
|
|
|
app_client,
|
|
|
|
|
app_client_csv_max_mb_one,
|
2019-05-04 02:15:14 +00:00
|
|
|
|
app_client_with_cors,
|
2018-06-24 00:59:37 +00:00
|
|
|
|
)
|
2018-06-15 06:51:23 +00:00
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
|
EXPECTED_TABLE_CSV = """id,content
|
2018-06-15 06:51:23 +00:00
|
|
|
|
1,hello
|
|
|
|
|
2,world
|
|
|
|
|
3,
|
2018-08-28 10:03:01 +00:00
|
|
|
|
4,RENDER_CELL_DEMO
|
2019-05-04 02:15:14 +00:00
|
|
|
|
""".replace(
|
|
|
|
|
"\n", "\r\n"
|
|
|
|
|
)
|
2018-06-15 06:51:23 +00:00
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
|
EXPECTED_CUSTOM_CSV = """content
|
2018-06-15 06:51:23 +00:00
|
|
|
|
hello
|
|
|
|
|
world
|
2019-05-04 02:15:14 +00:00
|
|
|
|
""".replace(
|
|
|
|
|
"\n", "\r\n"
|
|
|
|
|
)
|
2018-06-15 06:51:23 +00:00
|
|
|
|
|
2019-05-04 02:15:14 +00:00
|
|
|
|
EXPECTED_TABLE_WITH_LABELS_CSV = """
|
2019-11-22 00:56:55 +00:00
|
|
|
|
pk,created,planet_int,on_earth,state,city_id,city_id_label,neighborhood,tags,complex_array,distinct_some_null
|
|
|
|
|
1,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Mission,"[""tag1"", ""tag2""]","[{""foo"": ""bar""}]",one
|
|
|
|
|
2,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Dogpatch,"[""tag1"", ""tag3""]",[],two
|
|
|
|
|
3,2019-01-14 08:00:00,1,1,CA,1,San Francisco,SOMA,[],[],
|
|
|
|
|
4,2019-01-14 08:00:00,1,1,CA,1,San Francisco,Tenderloin,[],[],
|
|
|
|
|
5,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Bernal Heights,[],[],
|
|
|
|
|
6,2019-01-15 08:00:00,1,1,CA,1,San Francisco,Hayes Valley,[],[],
|
|
|
|
|
7,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Hollywood,[],[],
|
|
|
|
|
8,2019-01-15 08:00:00,1,1,CA,2,Los Angeles,Downtown,[],[],
|
|
|
|
|
9,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Los Feliz,[],[],
|
|
|
|
|
10,2019-01-16 08:00:00,1,1,CA,2,Los Angeles,Koreatown,[],[],
|
|
|
|
|
11,2019-01-16 08:00:00,1,1,MI,3,Detroit,Downtown,[],[],
|
|
|
|
|
12,2019-01-17 08:00:00,1,1,MI,3,Detroit,Greektown,[],[],
|
|
|
|
|
13,2019-01-17 08:00:00,1,1,MI,3,Detroit,Corktown,[],[],
|
|
|
|
|
14,2019-01-17 08:00:00,1,1,MI,3,Detroit,Mexicantown,[],[],
|
|
|
|
|
15,2019-01-17 08:00:00,2,0,MC,4,Memnonia,Arcadia Planitia,[],[],
|
2019-05-04 02:15:14 +00:00
|
|
|
|
""".lstrip().replace(
|
|
|
|
|
"\n", "\r\n"
|
|
|
|
|
)
|
2018-06-15 06:51:23 +00:00
|
|
|
|
|
2019-11-02 23:12:46 +00:00
|
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
2018-06-24 00:59:37 +00:00
|
|
|
|
|
2018-06-15 06:51:23 +00:00
|
|
|
|
def test_table_csv(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_primary_key.csv")
|
2018-06-15 06:51:23 +00:00
|
|
|
|
assert response.status == 200
|
2018-06-24 00:59:37 +00:00
|
|
|
|
assert not response.headers.get("Access-Control-Allow-Origin")
|
2019-06-24 03:13:09 +00:00
|
|
|
|
assert "text/plain; charset=utf-8" == response.headers["content-type"]
|
2018-06-15 06:51:23 +00:00
|
|
|
|
assert EXPECTED_TABLE_CSV == response.text
|
|
|
|
|
|
|
|
|
|
|
2018-06-24 00:59:37 +00:00
|
|
|
|
def test_table_csv_cors_headers(app_client_with_cors):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client_with_cors.get("/fixtures/simple_primary_key.csv")
|
2018-06-24 00:59:37 +00:00
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert "*" == response.headers["Access-Control-Allow-Origin"]
|
|
|
|
|
|
|
|
|
|
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
|
def test_table_csv_with_labels(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/facetable.csv?_labels=1")
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
|
assert response.status == 200
|
2019-06-24 03:13:09 +00:00
|
|
|
|
assert "text/plain; charset=utf-8" == response.headers["content-type"]
|
?_labels= and ?_label=COL to expand foreign keys in JSON/CSV
These new querystring arguments can be used to request expanded foreign keys
in both JSON and CSV formats.
?_labels=on turns on expansions for ALL foreign key columns
?_label=COLUMN1&_label=COLUMN2 can be used to pick specific columns to expand
e.g. `Street_Tree_List.json?_label=qSpecies&_label=qLegalStatus`
{
"rowid": 233,
"TreeID": 121240,
"qLegalStatus": {
"value" 2,
"label": "Private"
}
"qSpecies": {
"value": 16,
"label": "Sycamore"
}
"qAddress": "91 Commonwealth Ave",
...
}
The labels option also works for the HTML and CSV views.
HTML defaults to `?_labels=on`, so if you pass `?_labels=off` you can disable
foreign key expansion entirely - or you can use `?_label=COLUMN` to request
just specific columns.
If you expand labels on CSV you get additional columns in the output:
`/Street_Tree_List.csv?_label=qLegalStatus`
rowid,TreeID,qLegalStatus,qLegalStatus_label...
1,141565,1,Permitted Site...
2,232565,2,Undocumented...
I also refactored the existing foreign key expansion code.
Closes #233. Refs #266.
2018-06-16 22:18:57 +00:00
|
|
|
|
assert EXPECTED_TABLE_WITH_LABELS_CSV == response.text
|
|
|
|
|
|
|
|
|
|
|
2019-11-02 23:12:46 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2018-06-15 06:51:23 +00:00
|
|
|
|
def test_custom_sql_csv(app_client):
|
|
|
|
|
response = app_client.get(
|
2019-05-04 02:15:14 +00:00
|
|
|
|
"/fixtures.csv?sql=select+content+from+simple_primary_key+limit+2"
|
2018-06-15 06:51:23 +00:00
|
|
|
|
)
|
|
|
|
|
assert response.status == 200
|
2019-06-24 03:13:09 +00:00
|
|
|
|
assert "text/plain; charset=utf-8" == response.headers["content-type"]
|
2018-06-15 06:51:23 +00:00
|
|
|
|
assert EXPECTED_CUSTOM_CSV == response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_table_csv_download(app_client):
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/simple_primary_key.csv?_dl=1")
|
2018-06-15 06:51:23 +00:00
|
|
|
|
assert response.status == 200
|
2019-06-24 03:13:09 +00:00
|
|
|
|
assert "text/csv; charset=utf-8" == response.headers["content-type"]
|
2018-06-15 06:51:23 +00:00
|
|
|
|
expected_disposition = 'attachment; filename="simple_primary_key.csv"'
|
2019-05-04 02:15:14 +00:00
|
|
|
|
assert expected_disposition == response.headers["Content-Disposition"]
|
2018-06-18 03:21:02 +00:00
|
|
|
|
|
|
|
|
|
|
2019-10-18 05:23:01 +00:00
|
|
|
|
def test_csv_with_non_ascii_characters(app_client):
|
|
|
|
|
response = app_client.get(
|
|
|
|
|
"/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"
|
|
|
|
|
)
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
assert "text/plain; charset=utf-8" == response.headers["content-type"]
|
|
|
|
|
assert "text,number\r\n𝐜𝐢𝐭𝐢𝐞𝐬,1\r\nbob,2\r\n" == response.body.decode("utf8")
|
|
|
|
|
|
|
|
|
|
|
2018-06-18 03:21:02 +00:00
|
|
|
|
def test_max_csv_mb(app_client_csv_max_mb_one):
|
|
|
|
|
response = app_client_csv_max_mb_one.get(
|
|
|
|
|
"/fixtures.csv?sql=select+randomblob(10000)+"
|
|
|
|
|
"from+compound_three_primary_keys&_stream=1&_size=max"
|
|
|
|
|
)
|
|
|
|
|
# It's a 200 because we started streaming before we knew the error
|
|
|
|
|
assert response.status == 200
|
|
|
|
|
# Last line should be an error message
|
|
|
|
|
last_line = [line for line in response.body.split(b"\r\n") if line][-1]
|
|
|
|
|
assert last_line.startswith(b"CSV contains more than")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_table_csv_stream(app_client):
|
|
|
|
|
# Without _stream should return header + 100 rows:
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/compound_three_primary_keys.csv?_size=max")
|
2018-06-18 03:21:02 +00:00
|
|
|
|
assert 101 == len([b for b in response.body.split(b"\r\n") if b])
|
|
|
|
|
# With _stream=1 should return header + 1001 rows
|
2019-05-04 02:15:14 +00:00
|
|
|
|
response = app_client.get("/fixtures/compound_three_primary_keys.csv?_stream=1")
|
2018-06-18 03:21:02 +00:00
|
|
|
|
assert 1002 == len([b for b in response.body.split(b"\r\n") if b])
|