datasette/tests/test_facets.py
Simon Willison 5c198f7ca5 Include table metadata when calculating applied facets
Still need to ensure facets from metadata cannot be hidden in the UI
2019-04-18 09:29:43 -07:00

75 lines
2.6 KiB
Python

from datasette.facets import ColumnFacet
from .fixtures import app_client # noqa
from .utils import MockRequest
from collections import namedtuple
import pytest
@pytest.mark.asyncio
async def test_column_facet_suggest(app_client):
facet = ColumnFacet(
app_client.ds,
MockRequest("http://localhost/"),
database="fixtures",
sql="select * from facetable",
table="facetable",
)
suggestions = await facet.suggest()
assert [
{"name": "planet_int", "toggle_url": "http://localhost/?_facet=planet_int"},
{"name": "on_earth", "toggle_url": "http://localhost/?_facet=on_earth"},
{"name": "state", "toggle_url": "http://localhost/?_facet=state"},
{"name": "city_id", "toggle_url": "http://localhost/?_facet=city_id"},
{"name": "neighborhood", "toggle_url": "http://localhost/?_facet=neighborhood"},
{"name": "tags", "toggle_url": "http://localhost/?_facet=tags"},
] == suggestions
@pytest.mark.asyncio
async def test_column_facet_results(app_client):
facet = ColumnFacet(
app_client.ds,
MockRequest("http://localhost/?_facet=city_id"),
database="fixtures",
sql="select * from facetable",
table="facetable",
configs=[{"simple": "city_id"}],
)
buckets, timed_out = await facet.facet_results()
assert [] == timed_out
assert {
"city_id": {
"name": "city_id",
"results": [
{
"value": 1,
"label": "San Francisco",
"count": 6,
"toggle_url": "http://localhost/?_facet=city_id?_facet=city_id&city_id=1",
"selected": False,
},
{
"value": 2,
"label": "Los Angeles",
"count": 4,
"toggle_url": "http://localhost/?_facet=city_id?_facet=city_id&city_id=2",
"selected": False,
},
{
"value": 3,
"label": "Detroit",
"count": 4,
"toggle_url": "http://localhost/?_facet=city_id?_facet=city_id&city_id=3",
"selected": False,
},
{
"value": 4,
"label": "Memnonia",
"count": 1,
"toggle_url": "http://localhost/?_facet=city_id?_facet=city_id&city_id=4",
"selected": False,
},
],
"truncated": False,
}
} == buckets