diff --git a/tests/test_enrichments_jinja.py b/tests/test_enrichments_jinja.py index 2e0345a..f7c2d56 100644 --- a/tests/test_enrichments_jinja.py +++ b/tests/test_enrichments_jinja.py @@ -1,9 +1,20 @@ +import asyncio from datasette.app import Datasette -from datasette_enrichments_jinja import JinjaSandbox import pytest import sqlite_utils +async def _cookies(datasette): + cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")} + csrftoken = ( + await datasette.client.get( + "/-/enrich/data/items/jinja-sandbox", cookies=cookies + ) + ).cookies["ds_csrftoken"] + cookies["ds_csrftoken"] = csrftoken + return cookies + + @pytest.mark.asyncio async def test_enrichment(tmpdir): data = str(tmpdir / "data.db") @@ -14,31 +25,30 @@ async def test_enrichment(tmpdir): {"id": 2, "name": "Two", "description": "Second item"}, {"id": 3, "name": "Three", "description": "Third item"}, ] - config = { - "output_column": "template_output", + db["items"].insert_all(rows, pk="id") + + cookies = await _cookies(datasette) + post = { "template": "{{ row['name'] }}: {{ row['description'] }}", + "output_column": "template_output", } - db["items"].insert_all(rows) - ds_db = datasette.get_database("data") - enrichment = JinjaSandbox() - await enrichment.initialize(datasette, ds_db, "items", config) - # It should now have a template_output column - assert "template_output" in db["items"].columns_dict - # Now enrich the batch - await enrichment.enrich_batch( - datasette=datasette, - db=ds_db, - table="items", - rows=rows, - pks=["id"], - config={ - "output_column": "template_output", - "template": "{{ row['name'] }}: {{ row['description'] }}", - }, - job_id=1, + post["csrftoken"] = cookies["ds_csrftoken"] + response = await datasette.client.post( + "/-/enrich/data/items/jinja-sandbox", + data=post, + cookies=cookies, ) - new_rows = list(db["items"].rows) - assert new_rows == [ + assert response.status_code == 302 + await asyncio.sleep(0.3) + db = datasette.get_database("data") + jobs = await db.execute("select * from _enrichment_jobs") + job = dict(jobs.first()) + assert job["status"] == "finished" + assert job["enrichment"] == "jinja-sandbox" + assert job["done_count"] == 3 + results = await db.execute("select * from items order by id") + rows = [dict(r) for r in results.rows] + assert rows == [ { "id": 1, "name": "One",