Skip to content

Commit

Permalink
Updated test to new test pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 28, 2023
1 parent ea521ec commit b492847
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions tests/test_enrichments_jinja.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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",
Expand Down

0 comments on commit b492847

Please sign in to comment.