Skip to content

Commit

Permalink
Test for basic geocoder enrichment, refs #3
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Nov 30, 2023
1 parent 4d8cd89 commit cb8cdfa
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 11 deletions.
11 changes: 7 additions & 4 deletions datasette_enrichments_opencage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def register_enrichments(datasette):
return [OpenCageEnrichment()]



class OpenCageEnrichment(Enrichment):
name = "OpenCage geocoder"
slug = "opencage"
Expand All @@ -30,7 +29,9 @@ class OpenCageEnrichment(Enrichment):
async def get_config_form(self, datasette: "Datasette", db: Database, table: str):
def get_text_columns(conn):
db = sqlite_utils.Database(conn)
return [key for key, value in db[table].columns_dict.items() if value == str]
return [
key for key, value in db[table].columns_dict.items() if value == str
]

text_columns = await db.execute_fn(get_text_columns)

Expand All @@ -39,12 +40,14 @@ class ConfigForm(Form):
"Geocode input",
description="A template to run against each row to generate geocoder input. Use {{ COL }} for columns.",
validators=[DataRequired(message="Prompt is required.")],
default = ' '.join(["{{ %s }}" % c for c in text_columns])
default=" ".join(["{{ %s }}" % c for c in text_columns]),
)
json_column = StringField(
"Store JSON in column",
description="To store full JSON from OpenCage, enter a column name here",
render_kw={"placeholder": "Leave this blank if you only want to store latitude/longitude"},
render_kw={
"placeholder": "Leave this blank if you only want to store latitude/longitude"
},
)

def stash_api_key(form, field):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ CI = "https://github.com/datasette/datasette-enrichments-opencage/actions"
enrichments_opencage = "datasette_enrichments_opencage"

[project.optional-dependencies]
test = ["pytest", "pytest-asyncio"]
test = ["pytest", "pytest-asyncio", "pytest-httpx"]

[tool.pytest.ini_options]
asyncio_mode = "strict"
77 changes: 71 additions & 6 deletions tests/test_enrichments_opencage.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,76 @@
from datasette.app import Datasette
from datasette_enrichments.utils import wait_for_job
import pytest
import re
import sqlite_utils


@pytest.fixture
def non_mocked_hosts():
# This ensures httpx-mock will not affect Datasette's own
# httpx calls made in the tests by datasette.client:
return ["localhost"]


@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-opencage" in installed_plugins
@pytest.mark.parametrize("api_key_from_config", (True, False))
async def test_enrichment(tmpdir, api_key_from_config, httpx_mock):
httpx_mock.add_response(
url=re.compile(r"https://api.opencagedata.com/geocode/v1/json.*"),
method="GET",
json={
"results": [
{
"geometry": {
"lat": 38.8976633,
"lng": -77.0365739,
}
}
]
},
)
db_path = str(tmpdir / "data.db")
db = sqlite_utils.Database(db_path)
db["addresses"].insert(
{"id": 1, "address": "1600 Pennsylvania Ave NW, Washington, DC 20500"},
pk="id",
)

metadata = {}
if api_key_from_config:
metadata["plugins"] = {"datasette-enrichments-opencage": {"api_key": "abc123"}}
datasette = Datasette([db_path], metadata=metadata)

cookies = {"ds_actor": datasette.sign({"a": {"id": "root"}}, "actor")}
csrftoken = (
await datasette.client.get("/-/enrich/data/addresses/opencage", cookies=cookies)
).cookies["ds_csrftoken"]
cookies["ds_csrftoken"] = csrftoken

post = {
"input": "{{ address }}",
"csrftoken": cookies["ds_csrftoken"],
}
if not api_key_from_config:
post["api_key"] = "abc123"

response = await datasette.client.post(
"/-/enrich/data/addresses/opencage",
data=post,
cookies=cookies,
)
assert response.status_code == 302

job_id = response.headers["Location"].split("=")[-1]
await wait_for_job(datasette, job_id, timeout=1)

assert db["addresses"].columns_dict == {
"id": int,
"address": str,
"latitude": float,
"longitude": float,
}

# Check the API key was used
request = httpx_mock.get_request()
assert request.url.params["key"] == "abc123"

0 comments on commit cb8cdfa

Please sign in to comment.