From cbeea23d00b36f72386e68b67d76fdb8a151a486 Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Wed, 27 May 2020 20:13:32 -0700 Subject: [PATCH] Test for prepare_jinja2_environment, refs #773 --- tests/plugins/my_plugin.py | 5 +++++ tests/test_plugins.py | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/tests/plugins/my_plugin.py b/tests/plugins/my_plugin.py index e55a0a32..434a1977 100644 --- a/tests/plugins/my_plugin.py +++ b/tests/plugins/my_plugin.py @@ -87,3 +87,8 @@ def extra_template_vars(template, database, table, view_name, request, datasette default=lambda b: b.decode("utf8"), ) } + + +@hookimpl +def prepare_jinja2_environment(env): + env.filters["format_numeric"] = lambda s: "{:,.0f}".format(float(s)) diff --git a/tests/test_plugins.py b/tests/test_plugins.py index 94b69c1f..1bfd9d3f 100644 --- a/tests/test_plugins.py +++ b/tests/test_plugins.py @@ -9,6 +9,7 @@ from .fixtures import ( from datasette.app import Datasette from datasette.plugins import get_plugins, DEFAULT_PLUGINS, pm from datasette.utils import sqlite3 +from jinja2.environment import Template import base64 import json import os @@ -408,3 +409,12 @@ def test_register_output_renderer_custom_headers(app_client): ) assert "1" == response.headers["x-wow"] assert "2" == response.headers["x-gosh"] + + +@pytest.mark.asyncio +async def test_prepare_jinja2_environment(app_client): + template = app_client.ds.jinja_env.from_string( + "Hello there, {{ a|format_numeric }}", {"a": 3412341} + ) + rendered = await app_client.ds.render_template(template) + assert "Hello there, 3,412,341" == rendered