Skip to content

Commit

Permalink
Test for single enrichment, refs #2
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 27, 2023
1 parent 4b188bf commit bc1c28a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
3 changes: 2 additions & 1 deletion datasette_enrichments_re2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ async def enrich_batch(
if to_update:

def fn(conn):
db = sqlite_utils.Database(conn)
for ids, values in to_update:
sqlite_utils.Database(conn)[table].update(ids, values, alter=True)
db[table].update(ids, values, alter=True)

await db.execute_write_fn(fn, block=True)
51 changes: 45 additions & 6 deletions tests/test_enrichments_re2.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
import asyncio
from datasette.app import Datasette
import pytest
import pytest_asyncio


@pytest_asyncio.fixture()
async def datasette():
datasette = Datasette()
db = datasette.add_memory_database("demo")
await db.execute_write("create table news (body text)")
for text in ("example a", "example b", "example c"):
await db.execute_write("insert into news (body) values (?)", [text])
return datasette


@pytest.mark.asyncio
async def test_plugin_is_installed():
datasette = Datasette(memory=True)
response = await datasette.client.get("/-/plugins.json")
assert response.status_code == 200
installed_plugins = {p["name"] for p in response.json()}
assert "datasette-enrichments-re2" in installed_plugins
async def test_enrich_re2(datasette: Datasette):
cookies = {"ds_actor": datasette.client.actor_cookie({"id": "root"})}
csrftoken = (
await datasette.client.get("/-/enrich/demo/news/re2", cookies=cookies)
).cookies["ds_csrftoken"]
cookies["ds_csrftoken"] = csrftoken
response = await datasette.client.post(
"/-/enrich/demo/news/re2",
data={
"source_column": "body",
"regex": r"example (?P<letter>[a-z])",
"single_column": "letter",
"mode": "single",
"csrftoken": csrftoken,
},
cookies=cookies,
)
assert response.status_code == 302
# Wait 0.5s and the enrichment should have run
await asyncio.sleep(0.5)
db = datasette.get_database("demo")
jobs = await db.execute("select * from _enrichment_jobs")
job = dict(jobs.first())
assert job["status"] == "finished"
assert job["enrichment"] == "re2"
assert job["done_count"] == 3
results = await db.execute("select body, letter from news order by body")
rows = [dict(r) for r in results.rows]
assert rows == [
{"body": "example a", "letter": "a"},
{"body": "example b", "letter": "b"},
{"body": "example c", "letter": "c"},
]

0 comments on commit bc1c28a

Please sign in to comment.