Implemented ArrayFacet, closes #359

This commit is contained in:
Simon Willison 2019-05-02 20:21:27 -04:00
commit 53d2f00b73
3 changed files with 185 additions and 4 deletions

View file

@ -1,4 +1,5 @@
from datasette.facets import ColumnFacet
from datasette.facets import ColumnFacet, ArrayFacet
from datasette.utils import detect_json1
from .fixtures import app_client # noqa
from .utils import MockRequest
from collections import namedtuple
@ -172,3 +173,69 @@ async def test_column_facet_from_metadata_cannot_be_hidden(app_client):
"truncated": False,
}
} == buckets
@pytest.mark.asyncio
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
async def test_array_facet_suggest(app_client):
facet = ArrayFacet(
app_client.ds,
MockRequest("http://localhost/"),
database="fixtures",
sql="select * from facetable",
table="facetable",
)
suggestions = await facet.suggest()
assert [
{
"name": "tags",
"type": "array",
"toggle_url": "http://localhost/?_facet_array=tags",
}
] == suggestions
@pytest.mark.asyncio
@pytest.mark.skipif(not detect_json1(), reason="Requires the SQLite json1 module")
async def test_array_facet_results(app_client):
facet = ArrayFacet(
app_client.ds,
MockRequest("http://localhost/?_facet_array=tags"),
database="fixtures",
sql="select * from facetable",
table="facetable",
)
buckets, timed_out = await facet.facet_results()
assert [] == timed_out
assert {
"tags": {
"name": "tags",
"type": "array",
"results": [
{
"value": "tag1",
"label": "tag1",
"count": 2,
"toggle_url": "http://localhost/?_facet_array=tags&tags__arraycontains=tag1",
"selected": False,
},
{
"value": "tag2",
"label": "tag2",
"count": 1,
"toggle_url": "http://localhost/?_facet_array=tags&tags__arraycontains=tag2",
"selected": False,
},
{
"value": "tag3",
"label": "tag3",
"count": 1,
"toggle_url": "http://localhost/?_facet_array=tags&tags__arraycontains=tag3",
"selected": False,
},
],
"hideable": True,
"toggle_url": "/",
"truncated": False,
}
} == buckets