| 1 |
from datasette.utils import detect_json1 |
| 2 |
from datasette.utils.sqlite import sqlite_version |
| 3 |
from .fixtures import ( # noqa |
| 4 |
app_client, |
| 5 |
app_client_with_trace, |
| 6 |
app_client_returned_rows_matches_page_size, |
| 7 |
generate_compound_rows, |
| 8 |
generate_sortable_rows, |
| 9 |
make_app_client, |
| 10 |
) |
| 11 |
import json |
| 12 |
import pytest |
| 13 |
import urllib |
| 14 |
|
| 15 |
|
| 16 |
def test_table_json(app_client): |
| 17 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=objects") |
| 18 |
assert response.status == 200 |
| 19 |
data = response.json |
| 20 |
assert ( |
| 21 |
data["query"]["sql"] |
| 22 |
== "select id, content from simple_primary_key order by id limit 51" |
| 23 |
) |
| 24 |
assert data["query"]["params"] == {} |
| 25 |
assert data["rows"] == [ |
| 26 |
{"id": "1", "content": "hello"}, |
| 27 |
{"id": "2", "content": "world"}, |
| 28 |
{"id": "3", "content": ""}, |
| 29 |
{"id": "4", "content": "RENDER_CELL_DEMO"}, |
| 30 |
{"id": "5", "content": "RENDER_CELL_ASYNC"}, |
| 31 |
] |
| 32 |
|
| 33 |
|
| 34 |
def test_table_not_exists_json(app_client): |
| 35 |
assert { |
| 36 |
"ok": False, |
| 37 |
"error": "Table not found", |
| 38 |
"status": 404, |
| 39 |
"title": None, |
| 40 |
} == app_client.get("/fixtures/blah.json").json |
| 41 |
|
| 42 |
|
| 43 |
def test_table_shape_arrays(app_client): |
| 44 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=arrays") |
| 45 |
assert [ |
| 46 |
["1", "hello"], |
| 47 |
["2", "world"], |
| 48 |
["3", ""], |
| 49 |
["4", "RENDER_CELL_DEMO"], |
| 50 |
["5", "RENDER_CELL_ASYNC"], |
| 51 |
] == response.json["rows"] |
| 52 |
|
| 53 |
|
| 54 |
def test_table_shape_arrayfirst(app_client): |
| 55 |
response = app_client.get( |
| 56 |
"/fixtures.json?" |
| 57 |
+ urllib.parse.urlencode( |
| 58 |
{ |
| 59 |
"sql": "select content from simple_primary_key order by id", |
| 60 |
"_shape": "arrayfirst", |
| 61 |
} |
| 62 |
) |
| 63 |
) |
| 64 |
assert [ |
| 65 |
"hello", |
| 66 |
"world", |
| 67 |
"", |
| 68 |
"RENDER_CELL_DEMO", |
| 69 |
"RENDER_CELL_ASYNC", |
| 70 |
] == response.json |
| 71 |
|
| 72 |
|
| 73 |
def test_table_shape_objects(app_client): |
| 74 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=objects") |
| 75 |
assert [ |
| 76 |
{"id": "1", "content": "hello"}, |
| 77 |
{"id": "2", "content": "world"}, |
| 78 |
{"id": "3", "content": ""}, |
| 79 |
{"id": "4", "content": "RENDER_CELL_DEMO"}, |
| 80 |
{"id": "5", "content": "RENDER_CELL_ASYNC"}, |
| 81 |
] == response.json["rows"] |
| 82 |
|
| 83 |
|
| 84 |
def test_table_shape_array(app_client): |
| 85 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=array") |
| 86 |
assert [ |
| 87 |
{"id": "1", "content": "hello"}, |
| 88 |
{"id": "2", "content": "world"}, |
| 89 |
{"id": "3", "content": ""}, |
| 90 |
{"id": "4", "content": "RENDER_CELL_DEMO"}, |
| 91 |
{"id": "5", "content": "RENDER_CELL_ASYNC"}, |
| 92 |
] == response.json |
| 93 |
|
| 94 |
|
| 95 |
def test_table_shape_array_nl(app_client): |
| 96 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=array&_nl=on") |
| 97 |
lines = response.text.split("\n") |
| 98 |
results = [json.loads(line) for line in lines] |
| 99 |
assert [ |
| 100 |
{"id": "1", "content": "hello"}, |
| 101 |
{"id": "2", "content": "world"}, |
| 102 |
{"id": "3", "content": ""}, |
| 103 |
{"id": "4", "content": "RENDER_CELL_DEMO"}, |
| 104 |
{"id": "5", "content": "RENDER_CELL_ASYNC"}, |
| 105 |
] == results |
| 106 |
|
| 107 |
|
| 108 |
def test_table_shape_invalid(app_client): |
| 109 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=invalid") |
| 110 |
assert { |
| 111 |
"ok": False, |
| 112 |
"error": "Invalid _shape: invalid", |
| 113 |
"status": 400, |
| 114 |
"title": None, |
| 115 |
} == response.json |
| 116 |
|
| 117 |
|
| 118 |
def test_table_shape_object(app_client): |
| 119 |
response = app_client.get("/fixtures/simple_primary_key.json?_shape=object") |
| 120 |
assert { |
| 121 |
"1": {"id": "1", "content": "hello"}, |
| 122 |
"2": {"id": "2", "content": "world"}, |
| 123 |
"3": {"id": "3", "content": ""}, |
| 124 |
"4": {"id": "4", "content": "RENDER_CELL_DEMO"}, |
| 125 |
"5": {"id": "5", "content": "RENDER_CELL_ASYNC"}, |
| 126 |
} == response.json |
| 127 |
|
| 128 |
|
| 129 |
def test_table_shape_object_compound_primary_key(app_client): |
| 130 |
response = app_client.get("/fixtures/compound_primary_key.json?_shape=object") |
| 131 |
assert response.json == { |
| 132 |
"a,b": {"pk1": "a", "pk2": "b", "content": "c"}, |
| 133 |
"a~2Fb,~2Ec-d": {"pk1": "a/b", "pk2": ".c-d", "content": "c"}, |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
def test_table_with_slashes_in_name(app_client): |
| 138 |
response = app_client.get( |
| 139 |
"/fixtures/table~2Fwith~2Fslashes~2Ecsv.json?_shape=objects" |
| 140 |
) |
| 141 |
assert response.status == 200 |
| 142 |
data = response.json |
| 143 |
assert data["rows"] == [{"pk": "3", "content": "hey"}] |
| 144 |
|
| 145 |
|
| 146 |
def test_table_with_reserved_word_name(app_client): |
| 147 |
response = app_client.get("/fixtures/select.json?_shape=objects") |
| 148 |
assert response.status == 200 |
| 149 |
data = response.json |
| 150 |
assert data["rows"] == [ |
| 151 |
{ |
| 152 |
"rowid": 1, |
| 153 |
"group": "group", |
| 154 |
"having": "having", |
| 155 |
"and": "and", |
| 156 |
"json": '{"href": "http://example.com/", "label":"Example"}', |
| 157 |
} |
| 158 |
] |
| 159 |
|
| 160 |
|
| 161 |
@pytest.mark.parametrize( |
| 162 |
"path,expected_rows,expected_pages", |
| 163 |
[ |
| 164 |
("/fixtures/no_primary_key.json", 201, 5), |
| 165 |
("/fixtures/paginated_view.json", 201, 9), |
| 166 |
("/fixtures/no_primary_key.json?_size=25", 201, 9), |
| 167 |
("/fixtures/paginated_view.json?_size=50", 201, 5), |
| 168 |
("/fixtures/paginated_view.json?_size=max", 201, 3), |
| 169 |
("/fixtures/123_starts_with_digits.json", 0, 1), |
| 170 |
# Ensure faceting doesn't break pagination: |
| 171 |
("/fixtures/compound_three_primary_keys.json?_facet=pk1", 1001, 21), |
| 172 |
# Paginating while sorted by an expanded foreign key should work |
| 173 |
( |
| 174 |
"/fixtures/roadside_attraction_characteristics.json?_size=2&_sort=attraction_id&_labels=on", |
| 175 |
5, |
| 176 |
3, |
| 177 |
), |
| 178 |
], |
| 179 |
) |
| 180 |
def test_paginate_tables_and_views(app_client, path, expected_rows, expected_pages): |
| 181 |
fetched = [] |
| 182 |
count = 0 |
| 183 |
while path: |
| 184 |
response = app_client.get(path) |
| 185 |
assert 200 == response.status |
| 186 |
count += 1 |
| 187 |
fetched.extend(response.json["rows"]) |
| 188 |
path = response.json["next_url"] |
| 189 |
if path: |
| 190 |
assert urllib.parse.urlencode({"_next": response.json["next"]}) in path |
| 191 |
path = path.replace("http://localhost", "") |
| 192 |
assert count < 30, "Possible infinite loop detected" |
| 193 |
|
| 194 |
assert expected_rows == len(fetched) |
| 195 |
assert expected_pages == count |
| 196 |
|
| 197 |
|
| 198 |
@pytest.mark.parametrize( |
| 199 |
"path,expected_error", |
| 200 |
[ |
| 201 |
("/fixtures/no_primary_key.json?_size=-4", "_size must be a positive integer"), |
| 202 |
("/fixtures/no_primary_key.json?_size=dog", "_size must be a positive integer"), |
| 203 |
("/fixtures/no_primary_key.json?_size=1001", "_size must be <= 100"), |
| 204 |
], |
| 205 |
) |
| 206 |
def test_validate_page_size(app_client, path, expected_error): |
| 207 |
response = app_client.get(path) |
| 208 |
assert expected_error == response.json["error"] |
| 209 |
assert 400 == response.status |
| 210 |
|
| 211 |
|
| 212 |
def test_page_size_zero(app_client): |
| 213 |
"""For _size=0 we return the counts, empty rows and no continuation token""" |
| 214 |
response = app_client.get("/fixtures/no_primary_key.json?_size=0") |
| 215 |
assert 200 == response.status |
| 216 |
assert [] == response.json["rows"] |
| 217 |
assert 201 == response.json["filtered_table_rows_count"] |
| 218 |
assert None is response.json["next"] |
| 219 |
assert None is response.json["next_url"] |
| 220 |
|
| 221 |
|
| 222 |
def test_paginate_compound_keys(app_client): |
| 223 |
fetched = [] |
| 224 |
path = "/fixtures/compound_three_primary_keys.json?_shape=objects" |
| 225 |
page = 0 |
| 226 |
while path: |
| 227 |
page += 1 |
| 228 |
response = app_client.get(path) |
| 229 |
fetched.extend(response.json["rows"]) |
| 230 |
path = response.json["next_url"] |
| 231 |
if path: |
| 232 |
path = path.replace("http://localhost", "") |
| 233 |
assert page < 100 |
| 234 |
assert 1001 == len(fetched) |
| 235 |
assert 21 == page |
| 236 |
# Should be correctly ordered |
| 237 |
contents = [f["content"] for f in fetched] |
| 238 |
expected = [r[3] for r in generate_compound_rows(1001)] |
| 239 |
assert expected == contents |
| 240 |
|
| 241 |
|
| 242 |
def test_paginate_compound_keys_with_extra_filters(app_client): |
| 243 |
fetched = [] |
| 244 |
path = ( |
| 245 |
"/fixtures/compound_three_primary_keys.json?content__contains=d&_shape=objects" |
| 246 |
) |
| 247 |
page = 0 |
| 248 |
while path: |
| 249 |
page += 1 |
| 250 |
assert page < 100 |
| 251 |
response = app_client.get(path) |
| 252 |
fetched.extend(response.json["rows"]) |
| 253 |
path = response.json["next_url"] |
| 254 |
if path: |
| 255 |
path = path.replace("http://localhost", "") |
| 256 |
assert 2 == page |
| 257 |
expected = [r[3] for r in generate_compound_rows(1001) if "d" in r[3]] |
| 258 |
assert expected == [f["content"] for f in fetched] |
| 259 |
|
| 260 |
|
| 261 |
@pytest.mark.parametrize( |
| 262 |
"query_string,sort_key,human_description_en", |
| 263 |
[ |
| 264 |
("_sort=sortable", lambda row: row["sortable"], "sorted by sortable"), |
| 265 |
( |
| 266 |
"_sort_desc=sortable", |
| 267 |
lambda row: -row["sortable"], |
| 268 |
"sorted by sortable descending", |
| 269 |
), |
| 270 |
( |
| 271 |
"_sort=sortable_with_nulls", |
| 272 |
lambda row: ( |
| 273 |
1 if row["sortable_with_nulls"] is not None else 0, |
| 274 |
row["sortable_with_nulls"], |
| 275 |
), |
| 276 |
"sorted by sortable_with_nulls", |
| 277 |
), |
| 278 |
( |
| 279 |
"_sort_desc=sortable_with_nulls", |
| 280 |
lambda row: ( |
| 281 |
1 if row["sortable_with_nulls"] is None else 0, |
| 282 |
( |
| 283 |
-row["sortable_with_nulls"] |
| 284 |
if row["sortable_with_nulls"] is not None |
| 285 |
else 0 |
| 286 |
), |
| 287 |
row["content"], |
| 288 |
), |
| 289 |
"sorted by sortable_with_nulls descending", |
| 290 |
), |
| 291 |
# text column contains '$null' - ensure it doesn't confuse pagination: |
| 292 |
("_sort=text", lambda row: row["text"], "sorted by text"), |
| 293 |
# Still works if sort column removed using _col= |
| 294 |
("_sort=text&_col=content", lambda row: row["text"], "sorted by text"), |
| 295 |
], |
| 296 |
) |
| 297 |
def test_sortable(app_client, query_string, sort_key, human_description_en): |
| 298 |
path = f"/fixtures/sortable.json?_shape=objects&{query_string}" |
| 299 |
fetched = [] |
| 300 |
page = 0 |
| 301 |
while path: |
| 302 |
page += 1 |
| 303 |
assert page < 100 |
| 304 |
response = app_client.get(path) |
| 305 |
assert human_description_en == response.json["human_description_en"] |
| 306 |
fetched.extend(response.json["rows"]) |
| 307 |
path = response.json["next_url"] |
| 308 |
if path: |
| 309 |
path = path.replace("http://localhost", "") |
| 310 |
assert page == 5 |
| 311 |
expected = list(generate_sortable_rows(201)) |
| 312 |
expected.sort(key=sort_key) |
| 313 |
assert [r["content"] for r in expected] == [r["content"] for r in fetched] |
| 314 |
|
| 315 |
|
| 316 |
def test_sortable_and_filtered(app_client): |
| 317 |
path = ( |
| 318 |
"/fixtures/sortable.json" |
| 319 |
"?content__contains=d&_sort_desc=sortable&_shape=objects" |
| 320 |
) |
| 321 |
response = app_client.get(path) |
| 322 |
fetched = response.json["rows"] |
| 323 |
assert ( |
| 324 |
'where content contains "d" sorted by sortable descending' |
| 325 |
== response.json["human_description_en"] |
| 326 |
) |
| 327 |
expected = [row for row in generate_sortable_rows(201) if "d" in row["content"]] |
| 328 |
assert len(expected) == response.json["filtered_table_rows_count"] |
| 329 |
expected.sort(key=lambda row: -row["sortable"]) |
| 330 |
assert [r["content"] for r in expected] == [r["content"] for r in fetched] |
| 331 |
|
| 332 |
|
| 333 |
def test_sortable_argument_errors(app_client): |
| 334 |
response = app_client.get("/fixtures/sortable.json?_sort=badcolumn") |
| 335 |
assert "Cannot sort table by badcolumn" == response.json["error"] |
| 336 |
response = app_client.get("/fixtures/sortable.json?_sort_desc=badcolumn2") |
| 337 |
assert "Cannot sort table by badcolumn2" == response.json["error"] |
| 338 |
response = app_client.get( |
| 339 |
"/fixtures/sortable.json?_sort=sortable_with_nulls&_sort_desc=sortable" |
| 340 |
) |
| 341 |
assert "Cannot use _sort and _sort_desc at the same time" == response.json["error"] |
| 342 |
|
| 343 |
|
| 344 |
def test_sortable_columns_metadata(app_client): |
| 345 |
response = app_client.get("/fixtures/sortable.json?_sort=content") |
| 346 |
assert "Cannot sort table by content" == response.json["error"] |
| 347 |
# no_primary_key has ALL sort options disabled |
| 348 |
for column in ("content", "a", "b", "c"): |
| 349 |
response = app_client.get(f"/fixtures/sortable.json?_sort={column}") |
| 350 |
assert f"Cannot sort table by {column}" == response.json["error"] |
| 351 |
|
| 352 |
|
| 353 |
@pytest.mark.parametrize( |
| 354 |
"path,expected_rows", |
| 355 |
[ |
| 356 |
( |
| 357 |
"/fixtures/searchable.json?_search=dog", |
| 358 |
[ |
| 359 |
[1, "barry cat", "terry dog", "panther"], |
| 360 |
[2, "terry dog", "sara weasel", "puma"], |
| 361 |
], |
| 362 |
), |
| 363 |
( |
| 364 |
# Special keyword shouldn't break FTS query |
| 365 |
"/fixtures/searchable.json?_search=AND", |
| 366 |
[], |
| 367 |
), |
| 368 |
( |
| 369 |
# Without _searchmode=raw this should return no results |
| 370 |
"/fixtures/searchable.json?_search=te*+AND+do*", |
| 371 |
[], |
| 372 |
), |
| 373 |
( |
| 374 |
# _searchmode=raw |
| 375 |
"/fixtures/searchable.json?_search=te*+AND+do*&_searchmode=raw", |
| 376 |
[ |
| 377 |
[1, "barry cat", "terry dog", "panther"], |
| 378 |
[2, "terry dog", "sara weasel", "puma"], |
| 379 |
], |
| 380 |
), |
| 381 |
( |
| 382 |
# _searchmode=raw combined with _search_COLUMN |
| 383 |
"/fixtures/searchable.json?_search_text2=te*&_searchmode=raw", |
| 384 |
[ |
| 385 |
[1, "barry cat", "terry dog", "panther"], |
| 386 |
], |
| 387 |
), |
| 388 |
( |
| 389 |
"/fixtures/searchable.json?_search=weasel", |
| 390 |
[[2, "terry dog", "sara weasel", "puma"]], |
| 391 |
), |
| 392 |
( |
| 393 |
"/fixtures/searchable.json?_search_text2=dog", |
| 394 |
[[1, "barry cat", "terry dog", "panther"]], |
| 395 |
), |
| 396 |
( |
| 397 |
"/fixtures/searchable.json?_search_name%20with%20.%20and%20spaces=panther", |
| 398 |
[[1, "barry cat", "terry dog", "panther"]], |
| 399 |
), |
| 400 |
], |
| 401 |
) |
| 402 |
def test_searchable(app_client, path, expected_rows): |
| 403 |
response = app_client.get(path) |
| 404 |
assert expected_rows == response.json["rows"] |
| 405 |
|
| 406 |
|
| 407 |
_SEARCHMODE_RAW_RESULTS = [ |
| 408 |
[1, "barry cat", "terry dog", "panther"], |
| 409 |
[2, "terry dog", "sara weasel", "puma"], |
| 410 |
] |
| 411 |
|
| 412 |
|
| 413 |
@pytest.mark.parametrize( |
| 414 |
"table_metadata,querystring,expected_rows", |
| 415 |
[ |
| 416 |
( |
| 417 |
{}, |
| 418 |
"_search=te*+AND+do*", |
| 419 |
[], |
| 420 |
), |
| 421 |
( |
| 422 |
{"searchmode": "raw"}, |
| 423 |
"_search=te*+AND+do*", |
| 424 |
_SEARCHMODE_RAW_RESULTS, |
| 425 |
), |
| 426 |
( |
| 427 |
{}, |
| 428 |
"_search=te*+AND+do*&_searchmode=raw", |
| 429 |
_SEARCHMODE_RAW_RESULTS, |
| 430 |
), |
| 431 |
# Can be over-ridden with _searchmode=escaped |
| 432 |
( |
| 433 |
{"searchmode": "raw"}, |
| 434 |
"_search=te*+AND+do*&_searchmode=escaped", |
| 435 |
[], |
| 436 |
), |
| 437 |
], |
| 438 |
) |
| 439 |
def test_searchmode(table_metadata, querystring, expected_rows): |
| 440 |
with make_app_client( |
| 441 |
metadata={"databases": {"fixtures": {"tables": {"searchable": table_metadata}}}} |
| 442 |
) as client: |
| 443 |
response = client.get("/fixtures/searchable.json?" + querystring) |
| 444 |
assert expected_rows == response.json["rows"] |
| 445 |
|
| 446 |
|
| 447 |
@pytest.mark.parametrize( |
| 448 |
"path,expected_rows", |
| 449 |
[ |
| 450 |
( |
| 451 |
"/fixtures/searchable_view_configured_by_metadata.json?_search=weasel", |
| 452 |
[[2, "terry dog", "sara weasel", "puma"]], |
| 453 |
), |
| 454 |
# This should return all results because search is not configured: |
| 455 |
( |
| 456 |
"/fixtures/searchable_view.json?_search=weasel", |
| 457 |
[ |
| 458 |
[1, "barry cat", "terry dog", "panther"], |
| 459 |
[2, "terry dog", "sara weasel", "puma"], |
| 460 |
], |
| 461 |
), |
| 462 |
( |
| 463 |
"/fixtures/searchable_view.json?_search=weasel&_fts_table=searchable_fts&_fts_pk=pk", |
| 464 |
[[2, "terry dog", "sara weasel", "puma"]], |
| 465 |
), |
| 466 |
], |
| 467 |
) |
| 468 |
def test_searchable_views(app_client, path, expected_rows): |
| 469 |
response = app_client.get(path) |
| 470 |
assert expected_rows == response.json["rows"] |
| 471 |
|
| 472 |
|
| 473 |
def test_searchable_invalid_column(app_client): |
| 474 |
response = app_client.get("/fixtures/searchable.json?_search_invalid=x") |
| 475 |
assert 400 == response.status |
| 476 |
assert { |
| 477 |
"ok": False, |
| 478 |
"error": "Cannot search by that column", |
| 479 |
"status": 400, |
| 480 |
"title": None, |
| 481 |
} == response.json |
| 482 |
|
| 483 |
|
| 484 |
@pytest.mark.parametrize( |
| 485 |
"path,expected_rows", |
| 486 |
[ |
| 487 |
("/fixtures/simple_primary_key.json?content=hello", [["1", "hello"]]), |
| 488 |
( |
| 489 |
"/fixtures/simple_primary_key.json?content__contains=o", |
| 490 |
[ |
| 491 |
["1", "hello"], |
| 492 |
["2", "world"], |
| 493 |
["4", "RENDER_CELL_DEMO"], |
| 494 |
], |
| 495 |
), |
| 496 |
("/fixtures/simple_primary_key.json?content__exact=", [["3", ""]]), |
| 497 |
( |
| 498 |
"/fixtures/simple_primary_key.json?content__not=world", |
| 499 |
[ |
| 500 |
["1", "hello"], |
| 501 |
["3", ""], |
| 502 |
["4", "RENDER_CELL_DEMO"], |
| 503 |
["5", "RENDER_CELL_ASYNC"], |
| 504 |
], |
| 505 |
), |
| 506 |
], |
| 507 |
) |
| 508 |
def test_table_filter_queries(app_client, path, expected_rows): |
| 509 |
response = app_client.get(path) |
| 510 |
assert expected_rows == response.json["rows"] |
| 511 |
|
| 512 |
|
| 513 |
def test_table_filter_queries_multiple_of_same_type(app_client): |
| 514 |
response = app_client.get( |
| 515 |
"/fixtures/simple_primary_key.json?content__not=world&content__not=hello" |
| 516 |
) |
| 517 |
assert [ |
| 518 |
["3", ""], |
| 519 |
["4", "RENDER_CELL_DEMO"], |
| 520 |
["5", "RENDER_CELL_ASYNC"], |
| 521 |
] == response.json["rows"] |
| 522 |
|
| 523 |
|
| 524 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 525 |
def test_table_filters_quote_identifiers(): |
| 526 |
with make_app_client( |
| 527 |
extra_databases={ |
| 528 |
"demo.db": """ |
| 529 |
create table items ( |
| 530 |
id integer primary key, |
| 531 |
"name""quote" text, |
| 532 |
"tags]bracket" text |
| 533 |
); |
| 534 |
insert into items values (1, 'Alice', '["red"]'); |
| 535 |
""" |
| 536 |
}, |
| 537 |
) as client: |
| 538 |
exact_query = urllib.parse.urlencode( |
| 539 |
{'name"quote__exact': "Alice", "_shape": "arrays"} |
| 540 |
) |
| 541 |
exact_response = client.get(f"/demo/items.json?{exact_query}") |
| 542 |
assert exact_response.status == 200 |
| 543 |
assert exact_response.json["rows"] == [[1, "Alice", '["red"]']] |
| 544 |
|
| 545 |
array_query = urllib.parse.urlencode( |
| 546 |
{"tags]bracket__arraycontains": "red", "_shape": "arrays"} |
| 547 |
) |
| 548 |
array_response = client.get(f"/demo/items.json?{array_query}") |
| 549 |
assert array_response.status == 200 |
| 550 |
assert array_response.json["rows"] == [[1, "Alice", '["red"]']] |
| 551 |
|
| 552 |
|
| 553 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 554 |
def test_table_filter_json_arraycontains(app_client): |
| 555 |
response = app_client.get("/fixtures/facetable.json?tags__arraycontains=tag1") |
| 556 |
assert response.json["rows"] == [ |
| 557 |
[ |
| 558 |
1, |
| 559 |
"2019-01-14 08:00:00", |
| 560 |
1, |
| 561 |
1, |
| 562 |
"CA", |
| 563 |
1, |
| 564 |
"Mission", |
| 565 |
'["tag1", "tag2"]', |
| 566 |
'[{"foo": "bar"}]', |
| 567 |
"one", |
| 568 |
"n1", |
| 569 |
], |
| 570 |
[ |
| 571 |
2, |
| 572 |
"2019-01-14 08:00:00", |
| 573 |
1, |
| 574 |
1, |
| 575 |
"CA", |
| 576 |
1, |
| 577 |
"Dogpatch", |
| 578 |
'["tag1", "tag3"]', |
| 579 |
"[]", |
| 580 |
"two", |
| 581 |
"n2", |
| 582 |
], |
| 583 |
] |
| 584 |
|
| 585 |
|
| 586 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 587 |
def test_table_filter_json_arraynotcontains(app_client): |
| 588 |
response = app_client.get( |
| 589 |
"/fixtures/facetable.json?tags__arraynotcontains=tag3&tags__not=[]" |
| 590 |
) |
| 591 |
assert response.json["rows"] == [ |
| 592 |
[ |
| 593 |
1, |
| 594 |
"2019-01-14 08:00:00", |
| 595 |
1, |
| 596 |
1, |
| 597 |
"CA", |
| 598 |
1, |
| 599 |
"Mission", |
| 600 |
'["tag1", "tag2"]', |
| 601 |
'[{"foo": "bar"}]', |
| 602 |
"one", |
| 603 |
"n1", |
| 604 |
] |
| 605 |
] |
| 606 |
|
| 607 |
|
| 608 |
def test_table_filter_extra_where(app_client): |
| 609 |
response = app_client.get( |
| 610 |
"/fixtures/facetable.json?_where=_neighborhood='Dogpatch'" |
| 611 |
) |
| 612 |
assert [ |
| 613 |
[ |
| 614 |
2, |
| 615 |
"2019-01-14 08:00:00", |
| 616 |
1, |
| 617 |
1, |
| 618 |
"CA", |
| 619 |
1, |
| 620 |
"Dogpatch", |
| 621 |
'["tag1", "tag3"]', |
| 622 |
"[]", |
| 623 |
"two", |
| 624 |
"n2", |
| 625 |
] |
| 626 |
] == response.json["rows"] |
| 627 |
|
| 628 |
|
| 629 |
def test_table_filter_extra_where_invalid(app_client): |
| 630 |
response = app_client.get("/fixtures/facetable.json?_where=_neighborhood=Dogpatch'") |
| 631 |
assert 400 == response.status |
| 632 |
assert "Invalid SQL" == response.json["title"] |
| 633 |
|
| 634 |
|
| 635 |
def test_table_filter_extra_where_disabled_if_no_sql_allowed(): |
| 636 |
with make_app_client(metadata={"allow_sql": {}}) as client: |
| 637 |
response = client.get( |
| 638 |
"/fixtures/facetable.json?_where=_neighborhood='Dogpatch'" |
| 639 |
) |
| 640 |
assert 403 == response.status |
| 641 |
assert "_where= is not allowed" == response.json["error"] |
| 642 |
|
| 643 |
|
| 644 |
def test_table_through(app_client): |
| 645 |
# Just the museums: |
| 646 |
response = app_client.get( |
| 647 |
'/fixtures/roadside_attractions.json?_through={"table":"roadside_attraction_characteristics","column":"characteristic_id","value":"1"}' |
| 648 |
) |
| 649 |
assert response.json["rows"] == [ |
| 650 |
[ |
| 651 |
3, |
| 652 |
"Burlingame Museum of PEZ Memorabilia", |
| 653 |
"214 California Drive, Burlingame, CA 94010", |
| 654 |
None, |
| 655 |
37.5793, |
| 656 |
-122.3442, |
| 657 |
], |
| 658 |
[ |
| 659 |
4, |
| 660 |
"Bigfoot Discovery Museum", |
| 661 |
"5497 Highway 9, Felton, CA 95018", |
| 662 |
"https://www.bigfootdiscoveryproject.com/", |
| 663 |
37.0414, |
| 664 |
-122.0725, |
| 665 |
], |
| 666 |
] |
| 667 |
|
| 668 |
assert ( |
| 669 |
response.json["human_description_en"] |
| 670 |
== 'where roadside_attraction_characteristics.characteristic_id = "1"' |
| 671 |
) |
| 672 |
|
| 673 |
|
| 674 |
def test_max_returned_rows(app_client): |
| 675 |
response = app_client.get("/fixtures.json?sql=select+content+from+no_primary_key") |
| 676 |
data = response.json |
| 677 |
assert {"sql": "select content from no_primary_key", "params": {}} == data["query"] |
| 678 |
assert data["truncated"] |
| 679 |
assert 100 == len(data["rows"]) |
| 680 |
|
| 681 |
|
| 682 |
def test_view(app_client): |
| 683 |
response = app_client.get("/fixtures/simple_view.json?_shape=objects") |
| 684 |
assert response.status == 200 |
| 685 |
data = response.json |
| 686 |
assert data["rows"] == [ |
| 687 |
{"upper_content": "HELLO", "content": "hello"}, |
| 688 |
{"upper_content": "WORLD", "content": "world"}, |
| 689 |
{"upper_content": "", "content": ""}, |
| 690 |
{"upper_content": "RENDER_CELL_DEMO", "content": "RENDER_CELL_DEMO"}, |
| 691 |
{"upper_content": "RENDER_CELL_ASYNC", "content": "RENDER_CELL_ASYNC"}, |
| 692 |
] |
| 693 |
|
| 694 |
|
| 695 |
def test_unit_filters(app_client): |
| 696 |
response = app_client.get( |
| 697 |
"/fixtures/units.json?distance__lt=75km&frequency__gt=1kHz" |
| 698 |
) |
| 699 |
assert response.status == 200 |
| 700 |
data = response.json |
| 701 |
|
| 702 |
assert data["units"]["distance"] == "m" |
| 703 |
assert data["units"]["frequency"] == "Hz" |
| 704 |
|
| 705 |
assert len(data["rows"]) == 1 |
| 706 |
assert data["rows"][0][0] == 2 |
| 707 |
|
| 708 |
|
| 709 |
def test_page_size_matching_max_returned_rows( |
| 710 |
app_client_returned_rows_matches_page_size, |
| 711 |
): |
| 712 |
fetched = [] |
| 713 |
path = "/fixtures/no_primary_key.json" |
| 714 |
while path: |
| 715 |
response = app_client_returned_rows_matches_page_size.get(path) |
| 716 |
fetched.extend(response.json["rows"]) |
| 717 |
assert len(response.json["rows"]) in (1, 50) |
| 718 |
path = response.json["next_url"] |
| 719 |
if path: |
| 720 |
path = path.replace("http://localhost", "") |
| 721 |
assert 201 == len(fetched) |
| 722 |
|
| 723 |
|
| 724 |
@pytest.mark.parametrize( |
| 725 |
"path,expected_facet_results", |
| 726 |
[ |
| 727 |
( |
| 728 |
"/fixtures/facetable.json?_facet=state&_facet=_city_id", |
| 729 |
{ |
| 730 |
"state": { |
| 731 |
"name": "state", |
| 732 |
"hideable": True, |
| 733 |
"type": "column", |
| 734 |
"toggle_url": "/fixtures/facetable.json?_facet=_city_id", |
| 735 |
"results": [ |
| 736 |
{ |
| 737 |
"value": "CA", |
| 738 |
"label": "CA", |
| 739 |
"count": 10, |
| 740 |
"toggle_url": "_facet=state&_facet=_city_id&state=CA", |
| 741 |
"selected": False, |
| 742 |
}, |
| 743 |
{ |
| 744 |
"value": "MI", |
| 745 |
"label": "MI", |
| 746 |
"count": 4, |
| 747 |
"toggle_url": "_facet=state&_facet=_city_id&state=MI", |
| 748 |
"selected": False, |
| 749 |
}, |
| 750 |
{ |
| 751 |
"value": "MC", |
| 752 |
"label": "MC", |
| 753 |
"count": 1, |
| 754 |
"toggle_url": "_facet=state&_facet=_city_id&state=MC", |
| 755 |
"selected": False, |
| 756 |
}, |
| 757 |
], |
| 758 |
"truncated": False, |
| 759 |
}, |
| 760 |
"_city_id": { |
| 761 |
"name": "_city_id", |
| 762 |
"hideable": True, |
| 763 |
"type": "column", |
| 764 |
"toggle_url": "/fixtures/facetable.json?_facet=state", |
| 765 |
"results": [ |
| 766 |
{ |
| 767 |
"value": 1, |
| 768 |
"label": "San Francisco", |
| 769 |
"count": 6, |
| 770 |
"toggle_url": "_facet=state&_facet=_city_id&_city_id__exact=1", |
| 771 |
"selected": False, |
| 772 |
}, |
| 773 |
{ |
| 774 |
"value": 2, |
| 775 |
"label": "Los Angeles", |
| 776 |
"count": 4, |
| 777 |
"toggle_url": "_facet=state&_facet=_city_id&_city_id__exact=2", |
| 778 |
"selected": False, |
| 779 |
}, |
| 780 |
{ |
| 781 |
"value": 3, |
| 782 |
"label": "Detroit", |
| 783 |
"count": 4, |
| 784 |
"toggle_url": "_facet=state&_facet=_city_id&_city_id__exact=3", |
| 785 |
"selected": False, |
| 786 |
}, |
| 787 |
{ |
| 788 |
"value": 4, |
| 789 |
"label": "Memnonia", |
| 790 |
"count": 1, |
| 791 |
"toggle_url": "_facet=state&_facet=_city_id&_city_id__exact=4", |
| 792 |
"selected": False, |
| 793 |
}, |
| 794 |
], |
| 795 |
"truncated": False, |
| 796 |
}, |
| 797 |
}, |
| 798 |
), |
| 799 |
( |
| 800 |
"/fixtures/facetable.json?_facet=state&_facet=_city_id&state=MI", |
| 801 |
{ |
| 802 |
"state": { |
| 803 |
"name": "state", |
| 804 |
"hideable": True, |
| 805 |
"type": "column", |
| 806 |
"toggle_url": "/fixtures/facetable.json?_facet=_city_id&state=MI", |
| 807 |
"results": [ |
| 808 |
{ |
| 809 |
"value": "MI", |
| 810 |
"label": "MI", |
| 811 |
"count": 4, |
| 812 |
"selected": True, |
| 813 |
"toggle_url": "_facet=state&_facet=_city_id", |
| 814 |
} |
| 815 |
], |
| 816 |
"truncated": False, |
| 817 |
}, |
| 818 |
"_city_id": { |
| 819 |
"name": "_city_id", |
| 820 |
"hideable": True, |
| 821 |
"type": "column", |
| 822 |
"toggle_url": "/fixtures/facetable.json?_facet=state&state=MI", |
| 823 |
"results": [ |
| 824 |
{ |
| 825 |
"value": 3, |
| 826 |
"label": "Detroit", |
| 827 |
"count": 4, |
| 828 |
"selected": False, |
| 829 |
"toggle_url": "_facet=state&_facet=_city_id&state=MI&_city_id__exact=3", |
| 830 |
} |
| 831 |
], |
| 832 |
"truncated": False, |
| 833 |
}, |
| 834 |
}, |
| 835 |
), |
| 836 |
( |
| 837 |
"/fixtures/facetable.json?_facet=planet_int", |
| 838 |
{ |
| 839 |
"planet_int": { |
| 840 |
"name": "planet_int", |
| 841 |
"hideable": True, |
| 842 |
"type": "column", |
| 843 |
"toggle_url": "/fixtures/facetable.json", |
| 844 |
"results": [ |
| 845 |
{ |
| 846 |
"value": 1, |
| 847 |
"label": 1, |
| 848 |
"count": 14, |
| 849 |
"selected": False, |
| 850 |
"toggle_url": "_facet=planet_int&planet_int=1", |
| 851 |
}, |
| 852 |
{ |
| 853 |
"value": 2, |
| 854 |
"label": 2, |
| 855 |
"count": 1, |
| 856 |
"selected": False, |
| 857 |
"toggle_url": "_facet=planet_int&planet_int=2", |
| 858 |
}, |
| 859 |
], |
| 860 |
"truncated": False, |
| 861 |
} |
| 862 |
}, |
| 863 |
), |
| 864 |
( |
| 865 |
# planet_int is an integer field: |
| 866 |
"/fixtures/facetable.json?_facet=planet_int&planet_int=1", |
| 867 |
{ |
| 868 |
"planet_int": { |
| 869 |
"name": "planet_int", |
| 870 |
"hideable": True, |
| 871 |
"type": "column", |
| 872 |
"toggle_url": "/fixtures/facetable.json?planet_int=1", |
| 873 |
"results": [ |
| 874 |
{ |
| 875 |
"value": 1, |
| 876 |
"label": 1, |
| 877 |
"count": 14, |
| 878 |
"selected": True, |
| 879 |
"toggle_url": "_facet=planet_int", |
| 880 |
} |
| 881 |
], |
| 882 |
"truncated": False, |
| 883 |
} |
| 884 |
}, |
| 885 |
), |
| 886 |
], |
| 887 |
) |
| 888 |
def test_facets(app_client, path, expected_facet_results): |
| 889 |
response = app_client.get(path) |
| 890 |
facet_results = response.json["facet_results"] |
| 891 |
# We only compare the querystring portion of the taggle_url |
| 892 |
for facet_name, facet_info in facet_results.items(): |
| 893 |
assert facet_name == facet_info["name"] |
| 894 |
assert False is facet_info["truncated"] |
| 895 |
for facet_value in facet_info["results"]: |
| 896 |
facet_value["toggle_url"] = facet_value["toggle_url"].split("?")[1] |
| 897 |
assert expected_facet_results == facet_results |
| 898 |
|
| 899 |
|
| 900 |
def test_suggested_facets(app_client): |
| 901 |
suggestions = [ |
| 902 |
{ |
| 903 |
"name": suggestion["name"], |
| 904 |
"querystring": suggestion["toggle_url"].split("?")[-1], |
| 905 |
} |
| 906 |
for suggestion in app_client.get("/fixtures/facetable.json").json[ |
| 907 |
"suggested_facets" |
| 908 |
] |
| 909 |
] |
| 910 |
expected = [ |
| 911 |
{"name": "created", "querystring": "_facet=created"}, |
| 912 |
{"name": "planet_int", "querystring": "_facet=planet_int"}, |
| 913 |
{"name": "on_earth", "querystring": "_facet=on_earth"}, |
| 914 |
{"name": "state", "querystring": "_facet=state"}, |
| 915 |
{"name": "_city_id", "querystring": "_facet=_city_id"}, |
| 916 |
{"name": "_neighborhood", "querystring": "_facet=_neighborhood"}, |
| 917 |
{"name": "tags", "querystring": "_facet=tags"}, |
| 918 |
{"name": "complex_array", "querystring": "_facet=complex_array"}, |
| 919 |
{"name": "created", "querystring": "_facet_date=created"}, |
| 920 |
] |
| 921 |
if detect_json1(): |
| 922 |
expected.append({"name": "tags", "querystring": "_facet_array=tags"}) |
| 923 |
assert expected == suggestions |
| 924 |
|
| 925 |
|
| 926 |
def test_allow_facet_off(): |
| 927 |
with make_app_client(settings={"allow_facet": False}) as client: |
| 928 |
assert 400 == client.get("/fixtures/facetable.json?_facet=planet_int").status |
| 929 |
# Should not suggest any facets either: |
| 930 |
assert [] == client.get("/fixtures/facetable.json").json["suggested_facets"] |
| 931 |
|
| 932 |
|
| 933 |
def test_suggest_facets_off(): |
| 934 |
with make_app_client(settings={"suggest_facets": False}) as client: |
| 935 |
# Now suggested_facets should be [] |
| 936 |
assert [] == client.get("/fixtures/facetable.json").json["suggested_facets"] |
| 937 |
|
| 938 |
|
| 939 |
@pytest.mark.parametrize("nofacet", (True, False)) |
| 940 |
def test_nofacet(app_client, nofacet): |
| 941 |
path = "/fixtures/facetable.json?_facet=state" |
| 942 |
if nofacet: |
| 943 |
path += "&_nofacet=1" |
| 944 |
response = app_client.get(path) |
| 945 |
if nofacet: |
| 946 |
assert response.json["suggested_facets"] == [] |
| 947 |
assert response.json["facet_results"] == {} |
| 948 |
else: |
| 949 |
assert response.json["suggested_facets"] != [] |
| 950 |
assert response.json["facet_results"] != {} |
| 951 |
|
| 952 |
|
| 953 |
@pytest.mark.parametrize("nosuggest", (True, False)) |
| 954 |
def test_nosuggest(app_client, nosuggest): |
| 955 |
path = "/fixtures/facetable.json?_facet=state" |
| 956 |
if nosuggest: |
| 957 |
path += "&_nosuggest=1" |
| 958 |
response = app_client.get(path) |
| 959 |
if nosuggest: |
| 960 |
assert response.json["suggested_facets"] == [] |
| 961 |
# But facets should still be returned: |
| 962 |
assert response.json["facet_results"] != {} |
| 963 |
else: |
| 964 |
assert response.json["suggested_facets"] != [] |
| 965 |
assert response.json["facet_results"] != {} |
| 966 |
|
| 967 |
|
| 968 |
@pytest.mark.parametrize("nocount,expected_count", ((True, None), (False, 15))) |
| 969 |
def test_nocount(app_client, nocount, expected_count): |
| 970 |
path = "/fixtures/facetable.json" |
| 971 |
if nocount: |
| 972 |
path += "?_nocount=1" |
| 973 |
response = app_client.get(path) |
| 974 |
assert response.json["filtered_table_rows_count"] == expected_count |
| 975 |
|
| 976 |
|
| 977 |
def test_nocount_nofacet_if_shape_is_object(app_client_with_trace): |
| 978 |
response = app_client_with_trace.get( |
| 979 |
"/fixtures/facetable.json?_trace=1&_shape=object" |
| 980 |
) |
| 981 |
assert "count(*)" not in response.text |
| 982 |
|
| 983 |
|
| 984 |
def test_expand_labels(app_client): |
| 985 |
response = app_client.get( |
| 986 |
"/fixtures/facetable.json?_shape=object&_labels=1&_size=2" |
| 987 |
"&_neighborhood__contains=c" |
| 988 |
) |
| 989 |
assert { |
| 990 |
"2": { |
| 991 |
"pk": 2, |
| 992 |
"created": "2019-01-14 08:00:00", |
| 993 |
"planet_int": 1, |
| 994 |
"on_earth": 1, |
| 995 |
"state": "CA", |
| 996 |
"_city_id": {"value": 1, "label": "San Francisco"}, |
| 997 |
"_neighborhood": "Dogpatch", |
| 998 |
"tags": '["tag1", "tag3"]', |
| 999 |
"complex_array": "[]", |
| 1000 |
"distinct_some_null": "two", |
| 1001 |
"n": "n2", |
| 1002 |
}, |
| 1003 |
"13": { |
| 1004 |
"pk": 13, |
| 1005 |
"created": "2019-01-17 08:00:00", |
| 1006 |
"planet_int": 1, |
| 1007 |
"on_earth": 1, |
| 1008 |
"state": "MI", |
| 1009 |
"_city_id": {"value": 3, "label": "Detroit"}, |
| 1010 |
"_neighborhood": "Corktown", |
| 1011 |
"tags": "[]", |
| 1012 |
"complex_array": "[]", |
| 1013 |
"distinct_some_null": None, |
| 1014 |
"n": None, |
| 1015 |
}, |
| 1016 |
} == response.json |
| 1017 |
|
| 1018 |
|
| 1019 |
def test_expand_label(app_client): |
| 1020 |
response = app_client.get( |
| 1021 |
"/fixtures/foreign_key_references.json?_shape=object" |
| 1022 |
"&_label=foreign_key_with_label&_size=1" |
| 1023 |
) |
| 1024 |
assert response.json == { |
| 1025 |
"1": { |
| 1026 |
"pk": "1", |
| 1027 |
"foreign_key_with_label": {"value": "1", "label": "hello"}, |
| 1028 |
"foreign_key_with_blank_label": "3", |
| 1029 |
"foreign_key_with_no_label": "1", |
| 1030 |
"foreign_key_compound_pk1": "a", |
| 1031 |
"foreign_key_compound_pk2": "b", |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
|
| 1036 |
@pytest.mark.parametrize( |
| 1037 |
"path,expected_cache_control", |
| 1038 |
[ |
| 1039 |
("/fixtures/facetable.json", "max-age=5"), |
| 1040 |
("/fixtures/facetable.json?_ttl=invalid", "max-age=5"), |
| 1041 |
("/fixtures/facetable.json?_ttl=10", "max-age=10"), |
| 1042 |
("/fixtures/facetable.json?_ttl=0", "no-cache"), |
| 1043 |
], |
| 1044 |
) |
| 1045 |
def test_ttl_parameter(app_client, path, expected_cache_control): |
| 1046 |
response = app_client.get(path) |
| 1047 |
assert expected_cache_control == response.headers["Cache-Control"] |
| 1048 |
|
| 1049 |
|
| 1050 |
def test_infinity_returned_as_null(app_client): |
| 1051 |
response = app_client.get("/fixtures/infinity.json?_shape=array") |
| 1052 |
assert [ |
| 1053 |
{"rowid": 1, "value": None}, |
| 1054 |
{"rowid": 2, "value": None}, |
| 1055 |
{"rowid": 3, "value": 1.5}, |
| 1056 |
] == response.json |
| 1057 |
|
| 1058 |
|
| 1059 |
def test_infinity_returned_as_invalid_json_if_requested(app_client): |
| 1060 |
response = app_client.get("/fixtures/infinity.json?_shape=array&_json_infinity=1") |
| 1061 |
assert [ |
| 1062 |
{"rowid": 1, "value": float("inf")}, |
| 1063 |
{"rowid": 2, "value": float("-inf")}, |
| 1064 |
{"rowid": 3, "value": 1.5}, |
| 1065 |
] == response.json |
| 1066 |
|
| 1067 |
|
| 1068 |
def test_custom_query_with_unicode_characters(app_client): |
| 1069 |
# /fixtures/𝐜𝐢𝐭𝐢𝐞𝐬.json |
| 1070 |
response = app_client.get( |
| 1071 |
"/fixtures/~F0~9D~90~9C~F0~9D~90~A2~F0~9D~90~AD~F0~9D~90~A2~F0~9D~90~9E~F0~9D~90~AC.json?_shape=array" |
| 1072 |
) |
| 1073 |
assert [{"id": 1, "name": "San Francisco"}] == response.json |
| 1074 |
|
| 1075 |
|
| 1076 |
def test_null_and_compound_foreign_keys_are_not_expanded(app_client): |
| 1077 |
response = app_client.get( |
| 1078 |
"/fixtures/foreign_key_references.json?_shape=array&_labels=on" |
| 1079 |
) |
| 1080 |
assert response.json == [ |
| 1081 |
{ |
| 1082 |
"pk": "1", |
| 1083 |
"foreign_key_with_label": {"value": "1", "label": "hello"}, |
| 1084 |
"foreign_key_with_blank_label": {"value": "3", "label": ""}, |
| 1085 |
"foreign_key_with_no_label": {"value": "1", "label": "1"}, |
| 1086 |
"foreign_key_compound_pk1": "a", |
| 1087 |
"foreign_key_compound_pk2": "b", |
| 1088 |
}, |
| 1089 |
{ |
| 1090 |
"pk": "2", |
| 1091 |
"foreign_key_with_label": None, |
| 1092 |
"foreign_key_with_blank_label": None, |
| 1093 |
"foreign_key_with_no_label": None, |
| 1094 |
"foreign_key_compound_pk1": None, |
| 1095 |
"foreign_key_compound_pk2": None, |
| 1096 |
}, |
| 1097 |
] |
| 1098 |
|
| 1099 |
|
| 1100 |
@pytest.mark.parametrize( |
| 1101 |
"path,expected_json,expected_text", |
| 1102 |
[ |
| 1103 |
( |
| 1104 |
"/fixtures/binary_data.json?_shape=array", |
| 1105 |
[ |
| 1106 |
{"rowid": 1, "data": {"$base64": True, "encoded": "FRwCx60F/g=="}}, |
| 1107 |
{"rowid": 2, "data": {"$base64": True, "encoded": "FRwDx60F/g=="}}, |
| 1108 |
{"rowid": 3, "data": None}, |
| 1109 |
], |
| 1110 |
None, |
| 1111 |
), |
| 1112 |
( |
| 1113 |
"/fixtures/binary_data.json?_shape=array&_nl=on", |
| 1114 |
None, |
| 1115 |
( |
| 1116 |
'{"rowid": 1, "data": {"$base64": true, "encoded": "FRwCx60F/g=="}}\n' |
| 1117 |
'{"rowid": 2, "data": {"$base64": true, "encoded": "FRwDx60F/g=="}}\n' |
| 1118 |
'{"rowid": 3, "data": null}' |
| 1119 |
), |
| 1120 |
), |
| 1121 |
], |
| 1122 |
) |
| 1123 |
def test_binary_data_in_json(app_client, path, expected_json, expected_text): |
| 1124 |
response = app_client.get(path) |
| 1125 |
if expected_json: |
| 1126 |
assert response.json == expected_json |
| 1127 |
else: |
| 1128 |
assert response.text == expected_text |
| 1129 |
|
| 1130 |
|
| 1131 |
@pytest.mark.parametrize( |
| 1132 |
"qs", |
| 1133 |
[ |
| 1134 |
"", |
| 1135 |
"?_shape=arrays", |
| 1136 |
"?_shape=arrayfirst", |
| 1137 |
"?_shape=object", |
| 1138 |
"?_shape=objects", |
| 1139 |
"?_shape=array", |
| 1140 |
"?_shape=array&_nl=on", |
| 1141 |
], |
| 1142 |
) |
| 1143 |
def test_paginate_using_link_header(app_client, qs): |
| 1144 |
path = f"/fixtures/compound_three_primary_keys.json{qs}" |
| 1145 |
num_pages = 0 |
| 1146 |
while path: |
| 1147 |
response = app_client.get(path) |
| 1148 |
assert response.status == 200 |
| 1149 |
num_pages += 1 |
| 1150 |
link = response.headers.get("link") |
| 1151 |
if link: |
| 1152 |
assert link.startswith("<") |
| 1153 |
assert link.endswith('>; rel="next"') |
| 1154 |
path = link[1:].split(">")[0] |
| 1155 |
path = path.replace("http://localhost", "") |
| 1156 |
else: |
| 1157 |
path = None |
| 1158 |
assert num_pages == 21 |
| 1159 |
|
| 1160 |
|
| 1161 |
@pytest.mark.skipif( |
| 1162 |
sqlite_version() < (3, 31, 0), |
| 1163 |
reason="generated columns were added in SQLite 3.31.0", |
| 1164 |
) |
| 1165 |
def test_generated_columns_are_visible_in_datasette(): |
| 1166 |
with make_app_client( |
| 1167 |
extra_databases={ |
| 1168 |
"generated.db": """ |
| 1169 |
CREATE TABLE generated_columns ( |
| 1170 |
body TEXT, |
| 1171 |
id INT GENERATED ALWAYS AS (json_extract(body, '$.number')) STORED, |
| 1172 |
consideration INT GENERATED ALWAYS AS (json_extract(body, '$.string')) STORED |
| 1173 |
); |
| 1174 |
INSERT INTO generated_columns (body) VALUES ( |
| 1175 |
'{"number": 1, "string": "This is a string"}' |
| 1176 |
);""" |
| 1177 |
} |
| 1178 |
) as client: |
| 1179 |
response = client.get("/generated/generated_columns.json?_shape=array") |
| 1180 |
assert response.json == [ |
| 1181 |
{ |
| 1182 |
"rowid": 1, |
| 1183 |
"body": '{"number": 1, "string": "This is a string"}', |
| 1184 |
"id": 1, |
| 1185 |
"consideration": "This is a string", |
| 1186 |
} |
| 1187 |
] |
| 1188 |
|
| 1189 |
|
| 1190 |
@pytest.mark.parametrize( |
| 1191 |
"path,expected_columns", |
| 1192 |
( |
| 1193 |
("/fixtures/facetable.json?_col=created", ["pk", "created"]), |
| 1194 |
( |
| 1195 |
"/fixtures/facetable.json?_nocol=created", |
| 1196 |
[ |
| 1197 |
"pk", |
| 1198 |
"planet_int", |
| 1199 |
"on_earth", |
| 1200 |
"state", |
| 1201 |
"_city_id", |
| 1202 |
"_neighborhood", |
| 1203 |
"tags", |
| 1204 |
"complex_array", |
| 1205 |
"distinct_some_null", |
| 1206 |
"n", |
| 1207 |
], |
| 1208 |
), |
| 1209 |
( |
| 1210 |
"/fixtures/facetable.json?_col=state&_col=created", |
| 1211 |
["pk", "state", "created"], |
| 1212 |
), |
| 1213 |
( |
| 1214 |
"/fixtures/facetable.json?_col=state&_col=state", |
| 1215 |
["pk", "state"], |
| 1216 |
), |
| 1217 |
( |
| 1218 |
"/fixtures/facetable.json?_col=state&_col=created&_nocol=created", |
| 1219 |
["pk", "state"], |
| 1220 |
), |
| 1221 |
( |
| 1222 |
# Ensure faceting doesn't break, https://github.com/simonw/datasette/issues/1345 |
| 1223 |
"/fixtures/facetable.json?_nocol=state&_facet=state", |
| 1224 |
[ |
| 1225 |
"pk", |
| 1226 |
"created", |
| 1227 |
"planet_int", |
| 1228 |
"on_earth", |
| 1229 |
"_city_id", |
| 1230 |
"_neighborhood", |
| 1231 |
"tags", |
| 1232 |
"complex_array", |
| 1233 |
"distinct_some_null", |
| 1234 |
"n", |
| 1235 |
], |
| 1236 |
), |
| 1237 |
( |
| 1238 |
"/fixtures/simple_view.json?_nocol=content", |
| 1239 |
["upper_content"], |
| 1240 |
), |
| 1241 |
("/fixtures/simple_view.json?_col=content", ["content"]), |
| 1242 |
), |
| 1243 |
) |
| 1244 |
def test_col_nocol(app_client, path, expected_columns): |
| 1245 |
response = app_client.get(path) |
| 1246 |
assert response.status == 200 |
| 1247 |
columns = response.json["columns"] |
| 1248 |
assert columns == expected_columns |
| 1249 |
|
| 1250 |
|
| 1251 |
@pytest.mark.parametrize( |
| 1252 |
"path,expected_error", |
| 1253 |
( |
| 1254 |
("/fixtures/facetable.json?_col=bad", "_col=bad - invalid columns"), |
| 1255 |
("/fixtures/facetable.json?_nocol=bad", "_nocol=bad - invalid columns"), |
| 1256 |
("/fixtures/facetable.json?_nocol=pk", "_nocol=pk - invalid columns"), |
| 1257 |
("/fixtures/simple_view.json?_col=bad", "_col=bad - invalid columns"), |
| 1258 |
), |
| 1259 |
) |
| 1260 |
def test_col_nocol_errors(app_client, path, expected_error): |
| 1261 |
response = app_client.get(path) |
| 1262 |
assert response.status == 400 |
| 1263 |
assert response.json["error"] == expected_error |