From 6d95cb4f9146a5c4584a147bdf243c778a0f23f5 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 27 May 2020 21:09:16 -0700 Subject: [PATCH] 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. --- tests/plugins/my_plugin.py | 34 +++++++++++++++++++++++++++++++ tests/test_plugins.py | 41 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) diff --git a/tests/plugins/my_plugin.py b/tests/plugins/my_plugin.py index 434a1977..10d7e7e6 100644 --- a/tests/plugins/my_plugin.py +++ b/tests/plugins/my_plugin.py @@ -1,4 +1,6 @@ from datasette import hookimpl +from datasette.facets import Facet +from datasette.utils import path_with_added_args import base64 import pint import json @@ -92,3 +94,35 @@ def extra_template_vars(template, database, table, view_name, request, datasette @hookimpl def prepare_jinja2_environment(env): 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 diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 9ebf455a..2aadb252 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -428,3 +428,44 @@ def test_publish_subcommand(): # advantage of the fact that cloudrun/heroku use the plugin hook # to register themselves as default plugins. 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"]