| 1 |
from datasette.app import Datasette |
| 2 |
from datasette.database import Database |
| 3 |
from datasette.facets import ColumnFacet, ArrayFacet, DateFacet |
| 4 |
from datasette.utils.asgi import Request |
| 5 |
from datasette.utils import detect_json1 |
| 6 |
from .fixtures import app_client, make_app_client # noqa |
| 7 |
import json |
| 8 |
import pytest |
| 9 |
|
| 10 |
|
| 11 |
@pytest.mark.asyncio |
| 12 |
async def test_column_facet_suggest(app_client): |
| 13 |
facet = ColumnFacet( |
| 14 |
app_client.ds, |
| 15 |
Request.fake("/"), |
| 16 |
database="fixtures", |
| 17 |
sql="select * from facetable", |
| 18 |
table="facetable", |
| 19 |
) |
| 20 |
suggestions = await facet.suggest() |
| 21 |
assert [ |
| 22 |
{"name": "created", "toggle_url": "http://localhost/?_facet=created"}, |
| 23 |
{"name": "planet_int", "toggle_url": "http://localhost/?_facet=planet_int"}, |
| 24 |
{"name": "on_earth", "toggle_url": "http://localhost/?_facet=on_earth"}, |
| 25 |
{"name": "state", "toggle_url": "http://localhost/?_facet=state"}, |
| 26 |
{"name": "_city_id", "toggle_url": "http://localhost/?_facet=_city_id"}, |
| 27 |
{ |
| 28 |
"name": "_neighborhood", |
| 29 |
"toggle_url": "http://localhost/?_facet=_neighborhood", |
| 30 |
}, |
| 31 |
{"name": "tags", "toggle_url": "http://localhost/?_facet=tags"}, |
| 32 |
{ |
| 33 |
"name": "complex_array", |
| 34 |
"toggle_url": "http://localhost/?_facet=complex_array", |
| 35 |
}, |
| 36 |
] == suggestions |
| 37 |
|
| 38 |
|
| 39 |
@pytest.mark.asyncio |
| 40 |
async def test_column_facet_suggest_skip_if_already_selected(app_client): |
| 41 |
facet = ColumnFacet( |
| 42 |
app_client.ds, |
| 43 |
Request.fake("/?_facet=planet_int&_facet=on_earth"), |
| 44 |
database="fixtures", |
| 45 |
sql="select * from facetable", |
| 46 |
table="facetable", |
| 47 |
) |
| 48 |
suggestions = await facet.suggest() |
| 49 |
assert [ |
| 50 |
{ |
| 51 |
"name": "created", |
| 52 |
"toggle_url": "http://localhost/?_facet=planet_int&_facet=on_earth&_facet=created", |
| 53 |
}, |
| 54 |
{ |
| 55 |
"name": "state", |
| 56 |
"toggle_url": "http://localhost/?_facet=planet_int&_facet=on_earth&_facet=state", |
| 57 |
}, |
| 58 |
{ |
| 59 |
"name": "_city_id", |
| 60 |
"toggle_url": "http://localhost/?_facet=planet_int&_facet=on_earth&_facet=_city_id", |
| 61 |
}, |
| 62 |
{ |
| 63 |
"name": "_neighborhood", |
| 64 |
"toggle_url": "http://localhost/?_facet=planet_int&_facet=on_earth&_facet=_neighborhood", |
| 65 |
}, |
| 66 |
{ |
| 67 |
"name": "tags", |
| 68 |
"toggle_url": "http://localhost/?_facet=planet_int&_facet=on_earth&_facet=tags", |
| 69 |
}, |
| 70 |
{ |
| 71 |
"name": "complex_array", |
| 72 |
"toggle_url": "http://localhost/?_facet=planet_int&_facet=on_earth&_facet=complex_array", |
| 73 |
}, |
| 74 |
] == suggestions |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.asyncio |
| 78 |
async def test_column_facet_suggest_skip_if_enabled_by_metadata(app_client): |
| 79 |
facet = ColumnFacet( |
| 80 |
app_client.ds, |
| 81 |
Request.fake("/"), |
| 82 |
database="fixtures", |
| 83 |
sql="select * from facetable", |
| 84 |
table="facetable", |
| 85 |
metadata={"facets": ["_city_id"]}, |
| 86 |
) |
| 87 |
suggestions = [s["name"] for s in await facet.suggest()] |
| 88 |
assert [ |
| 89 |
"created", |
| 90 |
"planet_int", |
| 91 |
"on_earth", |
| 92 |
"state", |
| 93 |
"_neighborhood", |
| 94 |
"tags", |
| 95 |
"complex_array", |
| 96 |
] == suggestions |
| 97 |
|
| 98 |
|
| 99 |
@pytest.mark.asyncio |
| 100 |
async def test_column_facet_results(app_client): |
| 101 |
facet = ColumnFacet( |
| 102 |
app_client.ds, |
| 103 |
Request.fake("/?_facet=_city_id"), |
| 104 |
database="fixtures", |
| 105 |
sql="select * from facetable", |
| 106 |
table="facetable", |
| 107 |
) |
| 108 |
buckets, timed_out = await facet.facet_results() |
| 109 |
assert [] == timed_out |
| 110 |
assert [ |
| 111 |
{ |
| 112 |
"name": "_city_id", |
| 113 |
"type": "column", |
| 114 |
"hideable": True, |
| 115 |
"toggle_url": "/", |
| 116 |
"results": [ |
| 117 |
{ |
| 118 |
"value": 1, |
| 119 |
"label": "San Francisco", |
| 120 |
"count": 6, |
| 121 |
"toggle_url": "http://localhost/?_facet=_city_id&_city_id__exact=1", |
| 122 |
"selected": False, |
| 123 |
}, |
| 124 |
{ |
| 125 |
"value": 2, |
| 126 |
"label": "Los Angeles", |
| 127 |
"count": 4, |
| 128 |
"toggle_url": "http://localhost/?_facet=_city_id&_city_id__exact=2", |
| 129 |
"selected": False, |
| 130 |
}, |
| 131 |
{ |
| 132 |
"value": 3, |
| 133 |
"label": "Detroit", |
| 134 |
"count": 4, |
| 135 |
"toggle_url": "http://localhost/?_facet=_city_id&_city_id__exact=3", |
| 136 |
"selected": False, |
| 137 |
}, |
| 138 |
{ |
| 139 |
"value": 4, |
| 140 |
"label": "Memnonia", |
| 141 |
"count": 1, |
| 142 |
"toggle_url": "http://localhost/?_facet=_city_id&_city_id__exact=4", |
| 143 |
"selected": False, |
| 144 |
}, |
| 145 |
], |
| 146 |
"truncated": False, |
| 147 |
} |
| 148 |
] == buckets |
| 149 |
|
| 150 |
|
| 151 |
@pytest.mark.asyncio |
| 152 |
async def test_column_facet_results_column_starts_with_underscore(app_client): |
| 153 |
facet = ColumnFacet( |
| 154 |
app_client.ds, |
| 155 |
Request.fake("/?_facet=_neighborhood"), |
| 156 |
database="fixtures", |
| 157 |
sql="select * from facetable", |
| 158 |
table="facetable", |
| 159 |
) |
| 160 |
buckets, timed_out = await facet.facet_results() |
| 161 |
assert [] == timed_out |
| 162 |
assert buckets == [ |
| 163 |
{ |
| 164 |
"name": "_neighborhood", |
| 165 |
"type": "column", |
| 166 |
"hideable": True, |
| 167 |
"toggle_url": "/", |
| 168 |
"results": [ |
| 169 |
{ |
| 170 |
"value": "Downtown", |
| 171 |
"label": "Downtown", |
| 172 |
"count": 2, |
| 173 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Downtown", |
| 174 |
"selected": False, |
| 175 |
}, |
| 176 |
{ |
| 177 |
"value": "Arcadia Planitia", |
| 178 |
"label": "Arcadia Planitia", |
| 179 |
"count": 1, |
| 180 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Arcadia+Planitia", |
| 181 |
"selected": False, |
| 182 |
}, |
| 183 |
{ |
| 184 |
"value": "Bernal Heights", |
| 185 |
"label": "Bernal Heights", |
| 186 |
"count": 1, |
| 187 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Bernal+Heights", |
| 188 |
"selected": False, |
| 189 |
}, |
| 190 |
{ |
| 191 |
"value": "Corktown", |
| 192 |
"label": "Corktown", |
| 193 |
"count": 1, |
| 194 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Corktown", |
| 195 |
"selected": False, |
| 196 |
}, |
| 197 |
{ |
| 198 |
"value": "Dogpatch", |
| 199 |
"label": "Dogpatch", |
| 200 |
"count": 1, |
| 201 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Dogpatch", |
| 202 |
"selected": False, |
| 203 |
}, |
| 204 |
{ |
| 205 |
"value": "Greektown", |
| 206 |
"label": "Greektown", |
| 207 |
"count": 1, |
| 208 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Greektown", |
| 209 |
"selected": False, |
| 210 |
}, |
| 211 |
{ |
| 212 |
"value": "Hayes Valley", |
| 213 |
"label": "Hayes Valley", |
| 214 |
"count": 1, |
| 215 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Hayes+Valley", |
| 216 |
"selected": False, |
| 217 |
}, |
| 218 |
{ |
| 219 |
"value": "Hollywood", |
| 220 |
"label": "Hollywood", |
| 221 |
"count": 1, |
| 222 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Hollywood", |
| 223 |
"selected": False, |
| 224 |
}, |
| 225 |
{ |
| 226 |
"value": "Koreatown", |
| 227 |
"label": "Koreatown", |
| 228 |
"count": 1, |
| 229 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Koreatown", |
| 230 |
"selected": False, |
| 231 |
}, |
| 232 |
{ |
| 233 |
"value": "Los Feliz", |
| 234 |
"label": "Los Feliz", |
| 235 |
"count": 1, |
| 236 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Los+Feliz", |
| 237 |
"selected": False, |
| 238 |
}, |
| 239 |
{ |
| 240 |
"value": "Mexicantown", |
| 241 |
"label": "Mexicantown", |
| 242 |
"count": 1, |
| 243 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Mexicantown", |
| 244 |
"selected": False, |
| 245 |
}, |
| 246 |
{ |
| 247 |
"value": "Mission", |
| 248 |
"label": "Mission", |
| 249 |
"count": 1, |
| 250 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Mission", |
| 251 |
"selected": False, |
| 252 |
}, |
| 253 |
{ |
| 254 |
"value": "SOMA", |
| 255 |
"label": "SOMA", |
| 256 |
"count": 1, |
| 257 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=SOMA", |
| 258 |
"selected": False, |
| 259 |
}, |
| 260 |
{ |
| 261 |
"value": "Tenderloin", |
| 262 |
"label": "Tenderloin", |
| 263 |
"count": 1, |
| 264 |
"toggle_url": "http://localhost/?_facet=_neighborhood&_neighborhood__exact=Tenderloin", |
| 265 |
"selected": False, |
| 266 |
}, |
| 267 |
], |
| 268 |
"truncated": False, |
| 269 |
} |
| 270 |
] |
| 271 |
|
| 272 |
|
| 273 |
@pytest.mark.asyncio |
| 274 |
async def test_column_facet_from_metadata_cannot_be_hidden(app_client): |
| 275 |
facet = ColumnFacet( |
| 276 |
app_client.ds, |
| 277 |
Request.fake("/"), |
| 278 |
database="fixtures", |
| 279 |
sql="select * from facetable", |
| 280 |
table="facetable", |
| 281 |
metadata={"facets": ["_city_id"]}, |
| 282 |
) |
| 283 |
buckets, timed_out = await facet.facet_results() |
| 284 |
assert [] == timed_out |
| 285 |
assert [ |
| 286 |
{ |
| 287 |
"name": "_city_id", |
| 288 |
"type": "column", |
| 289 |
"hideable": False, |
| 290 |
"toggle_url": "/", |
| 291 |
"results": [ |
| 292 |
{ |
| 293 |
"value": 1, |
| 294 |
"label": "San Francisco", |
| 295 |
"count": 6, |
| 296 |
"toggle_url": "http://localhost/?_city_id__exact=1", |
| 297 |
"selected": False, |
| 298 |
}, |
| 299 |
{ |
| 300 |
"value": 2, |
| 301 |
"label": "Los Angeles", |
| 302 |
"count": 4, |
| 303 |
"toggle_url": "http://localhost/?_city_id__exact=2", |
| 304 |
"selected": False, |
| 305 |
}, |
| 306 |
{ |
| 307 |
"value": 3, |
| 308 |
"label": "Detroit", |
| 309 |
"count": 4, |
| 310 |
"toggle_url": "http://localhost/?_city_id__exact=3", |
| 311 |
"selected": False, |
| 312 |
}, |
| 313 |
{ |
| 314 |
"value": 4, |
| 315 |
"label": "Memnonia", |
| 316 |
"count": 1, |
| 317 |
"toggle_url": "http://localhost/?_city_id__exact=4", |
| 318 |
"selected": False, |
| 319 |
}, |
| 320 |
], |
| 321 |
"truncated": False, |
| 322 |
} |
| 323 |
] == buckets |
| 324 |
|
| 325 |
|
| 326 |
@pytest.mark.asyncio |
| 327 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 328 |
async def test_array_facet_suggest(app_client): |
| 329 |
facet = ArrayFacet( |
| 330 |
app_client.ds, |
| 331 |
Request.fake("/"), |
| 332 |
database="fixtures", |
| 333 |
sql="select * from facetable", |
| 334 |
table="facetable", |
| 335 |
) |
| 336 |
suggestions = await facet.suggest() |
| 337 |
assert [ |
| 338 |
{ |
| 339 |
"name": "tags", |
| 340 |
"type": "array", |
| 341 |
"toggle_url": "http://localhost/?_facet_array=tags", |
| 342 |
} |
| 343 |
] == suggestions |
| 344 |
|
| 345 |
|
| 346 |
@pytest.mark.asyncio |
| 347 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 348 |
async def test_array_facet_suggest_not_if_all_empty_arrays(app_client): |
| 349 |
facet = ArrayFacet( |
| 350 |
app_client.ds, |
| 351 |
Request.fake("/"), |
| 352 |
database="fixtures", |
| 353 |
sql="select * from facetable where tags = '[]'", |
| 354 |
table="facetable", |
| 355 |
) |
| 356 |
suggestions = await facet.suggest() |
| 357 |
assert [] == suggestions |
| 358 |
|
| 359 |
|
| 360 |
@pytest.mark.asyncio |
| 361 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 362 |
async def test_array_facet_results(app_client): |
| 363 |
facet = ArrayFacet( |
| 364 |
app_client.ds, |
| 365 |
Request.fake("/?_facet_array=tags"), |
| 366 |
database="fixtures", |
| 367 |
sql="select * from facetable", |
| 368 |
table="facetable", |
| 369 |
) |
| 370 |
buckets, timed_out = await facet.facet_results() |
| 371 |
assert [] == timed_out |
| 372 |
assert [ |
| 373 |
{ |
| 374 |
"name": "tags", |
| 375 |
"type": "array", |
| 376 |
"results": [ |
| 377 |
{ |
| 378 |
"value": "tag1", |
| 379 |
"label": "tag1", |
| 380 |
"count": 2, |
| 381 |
"toggle_url": "http://localhost/?_facet_array=tags&tags__arraycontains=tag1", |
| 382 |
"selected": False, |
| 383 |
}, |
| 384 |
{ |
| 385 |
"value": "tag2", |
| 386 |
"label": "tag2", |
| 387 |
"count": 1, |
| 388 |
"toggle_url": "http://localhost/?_facet_array=tags&tags__arraycontains=tag2", |
| 389 |
"selected": False, |
| 390 |
}, |
| 391 |
{ |
| 392 |
"value": "tag3", |
| 393 |
"label": "tag3", |
| 394 |
"count": 1, |
| 395 |
"toggle_url": "http://localhost/?_facet_array=tags&tags__arraycontains=tag3", |
| 396 |
"selected": False, |
| 397 |
}, |
| 398 |
], |
| 399 |
"hideable": True, |
| 400 |
"toggle_url": "/", |
| 401 |
"truncated": False, |
| 402 |
} |
| 403 |
] == buckets |
| 404 |
|
| 405 |
|
| 406 |
@pytest.mark.asyncio |
| 407 |
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module") |
| 408 |
async def test_array_facet_handle_duplicate_tags(): |
| 409 |
ds = Datasette([], memory=True) |
| 410 |
db = ds.add_database(Database(ds, memory_name="test_array_facet")) |
| 411 |
await db.execute_write("create table otters(name text, tags text)") |
| 412 |
for name, tags in ( |
| 413 |
("Charles", ["friendly", "cunning", "friendly"]), |
| 414 |
("Shaun", ["cunning", "empathetic", "friendly"]), |
| 415 |
("Tracy", ["empathetic", "eager"]), |
| 416 |
): |
| 417 |
await db.execute_write( |
| 418 |
"insert into otters (name, tags) values (?, ?)", [name, json.dumps(tags)] |
| 419 |
) |
| 420 |
|
| 421 |
response = await ds.client.get("/test_array_facet/otters.json?_facet_array=tags") |
| 422 |
assert response.json()["facet_results"]["tags"] == { |
| 423 |
"name": "tags", |
| 424 |
"type": "array", |
| 425 |
"results": [ |
| 426 |
{ |
| 427 |
"value": "cunning", |
| 428 |
"label": "cunning", |
| 429 |
"count": 2, |
| 430 |
"toggle_url": "http://localhost/test_array_facet/otters.json?_facet_array=tags&tags__arraycontains=cunning", |
| 431 |
"selected": False, |
| 432 |
}, |
| 433 |
{ |
| 434 |
"value": "empathetic", |
| 435 |
"label": "empathetic", |
| 436 |
"count": 2, |
| 437 |
"toggle_url": "http://localhost/test_array_facet/otters.json?_facet_array=tags&tags__arraycontains=empathetic", |
| 438 |
"selected": False, |
| 439 |
}, |
| 440 |
{ |
| 441 |
"value": "friendly", |
| 442 |
"label": "friendly", |
| 443 |
"count": 2, |
| 444 |
"toggle_url": "http://localhost/test_array_facet/otters.json?_facet_array=tags&tags__arraycontains=friendly", |
| 445 |
"selected": False, |
| 446 |
}, |
| 447 |
{ |
| 448 |
"value": "eager", |
| 449 |
"label": "eager", |
| 450 |
"count": 1, |
| 451 |
"toggle_url": "http://localhost/test_array_facet/otters.json?_facet_array=tags&tags__arraycontains=eager", |
| 452 |
"selected": False, |
| 453 |
}, |
| 454 |
], |
| 455 |
"hideable": True, |
| 456 |
"toggle_url": "/test_array_facet/otters.json", |
| 457 |
"truncated": False, |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
@pytest.mark.asyncio |
| 462 |
async def test_date_facet_results(app_client): |
| 463 |
facet = DateFacet( |
| 464 |
app_client.ds, |
| 465 |
Request.fake("/?_facet_date=created"), |
| 466 |
database="fixtures", |
| 467 |
sql="select * from facetable", |
| 468 |
table="facetable", |
| 469 |
) |
| 470 |
buckets, timed_out = await facet.facet_results() |
| 471 |
assert [] == timed_out |
| 472 |
assert [ |
| 473 |
{ |
| 474 |
"name": "created", |
| 475 |
"type": "date", |
| 476 |
"results": [ |
| 477 |
{ |
| 478 |
"value": "2019-01-14", |
| 479 |
"label": "2019-01-14", |
| 480 |
"count": 4, |
| 481 |
"toggle_url": "http://localhost/?_facet_date=created&created__date=2019-01-14", |
| 482 |
"selected": False, |
| 483 |
}, |
| 484 |
{ |
| 485 |
"value": "2019-01-15", |
| 486 |
"label": "2019-01-15", |
| 487 |
"count": 4, |
| 488 |
"toggle_url": "http://localhost/?_facet_date=created&created__date=2019-01-15", |
| 489 |
"selected": False, |
| 490 |
}, |
| 491 |
{ |
| 492 |
"value": "2019-01-17", |
| 493 |
"label": "2019-01-17", |
| 494 |
"count": 4, |
| 495 |
"toggle_url": "http://localhost/?_facet_date=created&created__date=2019-01-17", |
| 496 |
"selected": False, |
| 497 |
}, |
| 498 |
{ |
| 499 |
"value": "2019-01-16", |
| 500 |
"label": "2019-01-16", |
| 501 |
"count": 3, |
| 502 |
"toggle_url": "http://localhost/?_facet_date=created&created__date=2019-01-16", |
| 503 |
"selected": False, |
| 504 |
}, |
| 505 |
], |
| 506 |
"hideable": True, |
| 507 |
"toggle_url": "/", |
| 508 |
"truncated": False, |
| 509 |
} |
| 510 |
] == buckets |
| 511 |
|
| 512 |
|
| 513 |
@pytest.mark.asyncio |
| 514 |
async def test_json_array_with_blanks_and_nulls(): |
| 515 |
ds = Datasette([], memory=True) |
| 516 |
db = ds.add_database(Database(ds, memory_name="test_json_array")) |
| 517 |
await db.execute_write("create table foo(json_column text)") |
| 518 |
for value in ('["a", "b", "c"]', '["a", "b"]', "", None): |
| 519 |
await db.execute_write("insert into foo (json_column) values (?)", [value]) |
| 520 |
response = await ds.client.get("/test_json_array/foo.json") |
| 521 |
data = response.json() |
| 522 |
assert data["suggested_facets"] == [ |
| 523 |
{ |
| 524 |
"name": "json_column", |
| 525 |
"type": "array", |
| 526 |
"toggle_url": "http://localhost/test_json_array/foo.json?_facet_array=json_column", |
| 527 |
} |
| 528 |
] |
| 529 |
|
| 530 |
|
| 531 |
@pytest.mark.asyncio |
| 532 |
async def test_facet_size(): |
| 533 |
ds = Datasette([], memory=True, settings={"max_returned_rows": 50}) |
| 534 |
db = ds.add_database(Database(ds, memory_name="test_facet_size")) |
| 535 |
await db.execute_write("create table neighbourhoods(city text, neighbourhood text)") |
| 536 |
for i in range(1, 51): |
| 537 |
for j in range(1, 4): |
| 538 |
await db.execute_write( |
| 539 |
"insert into neighbourhoods (city, neighbourhood) values (?, ?)", |
| 540 |
["City {}".format(i), "Neighbourhood {}".format(j)], |
| 541 |
) |
| 542 |
response = await ds.client.get("/test_facet_size/neighbourhoods.json") |
| 543 |
data = response.json() |
| 544 |
assert data["suggested_facets"] == [ |
| 545 |
{ |
| 546 |
"name": "neighbourhood", |
| 547 |
"toggle_url": "http://localhost/test_facet_size/neighbourhoods.json?_facet=neighbourhood", |
| 548 |
} |
| 549 |
] |
| 550 |
# Bump up _facet_size= to suggest city too |
| 551 |
response2 = await ds.client.get( |
| 552 |
"/test_facet_size/neighbourhoods.json?_facet_size=50" |
| 553 |
) |
| 554 |
data2 = response2.json() |
| 555 |
assert sorted(data2["suggested_facets"], key=lambda f: f["name"]) == [ |
| 556 |
{ |
| 557 |
"name": "city", |
| 558 |
"toggle_url": "http://localhost/test_facet_size/neighbourhoods.json?_facet_size=50&_facet=city", |
| 559 |
}, |
| 560 |
{ |
| 561 |
"name": "neighbourhood", |
| 562 |
"toggle_url": "http://localhost/test_facet_size/neighbourhoods.json?_facet_size=50&_facet=neighbourhood", |
| 563 |
}, |
| 564 |
] |
| 565 |
# Facet by city should return expected number of results |
| 566 |
response3 = await ds.client.get( |
| 567 |
"/test_facet_size/neighbourhoods.json?_facet_size=50&_facet=city" |
| 568 |
) |
| 569 |
data3 = response3.json() |
| 570 |
assert len(data3["facet_results"]["city"]["results"]) == 50 |
| 571 |
# Reduce max_returned_rows and check that it's respected |
| 572 |
ds._settings["max_returned_rows"] = 20 |
| 573 |
response4 = await ds.client.get( |
| 574 |
"/test_facet_size/neighbourhoods.json?_facet_size=50&_facet=city" |
| 575 |
) |
| 576 |
data4 = response4.json() |
| 577 |
assert len(data4["facet_results"]["city"]["results"]) == 20 |
| 578 |
# Test _facet_size=max |
| 579 |
response5 = await ds.client.get( |
| 580 |
"/test_facet_size/neighbourhoods.json?_facet_size=max&_facet=city" |
| 581 |
) |
| 582 |
data5 = response5.json() |
| 583 |
assert len(data5["facet_results"]["city"]["results"]) == 20 |
| 584 |
# Now try messing with facet_size in the table metadata |
| 585 |
ds._metadata_local = { |
| 586 |
"databases": { |
| 587 |
"test_facet_size": {"tables": {"neighbourhoods": {"facet_size": 6}}} |
| 588 |
} |
| 589 |
} |
| 590 |
response6 = await ds.client.get("/test_facet_size/neighbourhoods.json?_facet=city") |
| 591 |
data6 = response6.json() |
| 592 |
assert len(data6["facet_results"]["city"]["results"]) == 6 |
| 593 |
# Setting it to max bumps it up to 50 again |
| 594 |
ds._metadata_local["databases"]["test_facet_size"]["tables"]["neighbourhoods"][ |
| 595 |
"facet_size" |
| 596 |
] = "max" |
| 597 |
data7 = ( |
| 598 |
await ds.client.get("/test_facet_size/neighbourhoods.json?_facet=city") |
| 599 |
).json() |
| 600 |
assert len(data7["facet_results"]["city"]["results"]) == 20 |
| 601 |
|
| 602 |
|
| 603 |
def test_other_types_of_facet_in_metadata(): |
| 604 |
with make_app_client( |
| 605 |
metadata={ |
| 606 |
"databases": { |
| 607 |
"fixtures": { |
| 608 |
"tables": { |
| 609 |
"facetable": { |
| 610 |
"facets": ["state", {"array": "tags"}, {"date": "created"}] |
| 611 |
} |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
} |
| 616 |
) as client: |
| 617 |
response = client.get("/fixtures/facetable") |
| 618 |
for fragment in ( |
| 619 |
"<strong>created (date)\n", |
| 620 |
"<strong>tags (array)\n", |
| 621 |
"<strong>state\n", |
| 622 |
): |
| 623 |
assert fragment in response.text |
| 624 |
|
| 625 |
|
| 626 |
def test_conflicting_facet_names_json(app_client): |
| 627 |
response = app_client.get( |
| 628 |
"/fixtures/facetable.json?_facet=created&_facet_date=created" |
| 629 |
"&_facet=tags&_facet_array=tags" |
| 630 |
) |
| 631 |
assert set(response.json["facet_results"].keys()) == { |
| 632 |
"created", |
| 633 |
"tags", |
| 634 |
"created_2", |
| 635 |
"tags_2", |
| 636 |
} |