mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Only doing ColumnFacet for the moment - tidied up and added tests
This commit is contained in:
parent
a64a39648d
commit
c7a11ab4e6
4 changed files with 148 additions and 532 deletions
87
tests/test_facets.py
Normal file
87
tests/test_facets.py
Normal file
|
|
@ -0,0 +1,87 @@
|
|||
from datasette.facets import ColumnFacet
|
||||
from .fixtures import app_client # noqa
|
||||
from collections import namedtuple
|
||||
import pytest
|
||||
|
||||
|
||||
class MockRequest:
|
||||
def __init__(self, url):
|
||||
self.url = url
|
||||
self.path = "/" + url.split("://")[1].split("/", 1)[1]
|
||||
self.query_string = ""
|
||||
if "?" in url:
|
||||
self.query_string = url.split("?", 1)[1]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_column_facet_suggest(app_client):
|
||||
facet = ColumnFacet(
|
||||
app_client.ds, MockRequest("http://localhost/"), "fixtures", "facetable", []
|
||||
)
|
||||
suggestions = await facet.suggest(
|
||||
"select * from facetable",
|
||||
[],
|
||||
await app_client.ds.table_count("fixtures", "facetable"),
|
||||
)
|
||||
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"),
|
||||
"fixtures",
|
||||
"facetable",
|
||||
[{"single": "city_id"}],
|
||||
)
|
||||
buckets, timed_out = await facet.facet_results(
|
||||
"""
|
||||
select * from facetable
|
||||
""",
|
||||
[],
|
||||
)
|
||||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue