kopia lustrzana https://github.com/simonw/datasette
Unit test for register_facet_classes plugin, closes #773
I was a bit lazy with this one. I didn't hook up a test for the facet_results mechanism. The custom facet hook isn't a great design so I will probably rethink it at some point in the future anyway.pull/783/head
rodzic
defead17a4
commit
6d95cb4f91
|
@ -1,4 +1,6 @@
|
||||||
from datasette import hookimpl
|
from datasette import hookimpl
|
||||||
|
from datasette.facets import Facet
|
||||||
|
from datasette.utils import path_with_added_args
|
||||||
import base64
|
import base64
|
||||||
import pint
|
import pint
|
||||||
import json
|
import json
|
||||||
|
@ -92,3 +94,35 @@ def extra_template_vars(template, database, table, view_name, request, datasette
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def prepare_jinja2_environment(env):
|
def prepare_jinja2_environment(env):
|
||||||
env.filters["format_numeric"] = lambda s: "{:,.0f}".format(float(s))
|
env.filters["format_numeric"] = lambda s: "{:,.0f}".format(float(s))
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl
|
||||||
|
def register_facet_classes():
|
||||||
|
return [DummyFacet]
|
||||||
|
|
||||||
|
|
||||||
|
class DummyFacet(Facet):
|
||||||
|
type = "dummy"
|
||||||
|
|
||||||
|
async def suggest(self):
|
||||||
|
columns = await self.get_columns(self.sql, self.params)
|
||||||
|
return (
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"name": column,
|
||||||
|
"toggle_url": self.ds.absolute_url(
|
||||||
|
self.request,
|
||||||
|
path_with_added_args(self.request, {"_facet_dummy": column}),
|
||||||
|
),
|
||||||
|
"type": "dummy",
|
||||||
|
}
|
||||||
|
for column in columns
|
||||||
|
]
|
||||||
|
if self.request.args.get("_dummy_facet")
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
|
||||||
|
async def facet_results(self):
|
||||||
|
facet_results = {}
|
||||||
|
facets_timed_out = []
|
||||||
|
return facet_results, facets_timed_out
|
||||||
|
|
|
@ -428,3 +428,44 @@ def test_publish_subcommand():
|
||||||
# advantage of the fact that cloudrun/heroku use the plugin hook
|
# advantage of the fact that cloudrun/heroku use the plugin hook
|
||||||
# to register themselves as default plugins.
|
# to register themselves as default plugins.
|
||||||
assert ["cloudrun", "heroku"] == cli.publish.list_commands({})
|
assert ["cloudrun", "heroku"] == cli.publish.list_commands({})
|
||||||
|
|
||||||
|
|
||||||
|
def test_register_facet_classes(app_client):
|
||||||
|
response = app_client.get(
|
||||||
|
"/fixtures/compound_three_primary_keys.json?_dummy_facet=1"
|
||||||
|
)
|
||||||
|
data = json.loads(response.body)
|
||||||
|
assert [
|
||||||
|
{
|
||||||
|
"name": "pk1",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet_dummy=pk1",
|
||||||
|
"type": "dummy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pk2",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet_dummy=pk2",
|
||||||
|
"type": "dummy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pk3",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet_dummy=pk3",
|
||||||
|
"type": "dummy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "content",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet_dummy=content",
|
||||||
|
"type": "dummy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pk1",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet=pk1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pk2",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet=pk2",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "pk3",
|
||||||
|
"toggle_url": "http://localhost/fixtures/compound_three_primary_keys.json?_dummy_facet=1&_facet=pk3",
|
||||||
|
},
|
||||||
|
] == data["suggested_facets"]
|
||||||
|
|
Ładowanie…
Reference in New Issue