Skip to content

Commit

Permalink
Only include JavaScript on pages with latitude/longitude columns
Browse files Browse the repository at this point in the history
Refs #15
  • Loading branch information
simonw committed Aug 16, 2020
1 parent 4784ff8 commit 6e7e269
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 14 deletions.
24 changes: 21 additions & 3 deletions datasette_cluster_map/__init__.py
Expand Up @@ -11,7 +11,9 @@


@hookimpl
def extra_css_urls():
def extra_css_urls(database, table, columns, datasette):
if not has_columns(database, table, columns, datasette):
return []
return [
{
"url": "https://unpkg.com/leaflet@1.5.1/dist/leaflet.css",
Expand All @@ -29,7 +31,9 @@ def extra_css_urls():


@hookimpl
def extra_js_urls():
def extra_js_urls(database, table, columns, datasette):
if not has_columns(database, table, columns, datasette):
return []
return [
{
"url": "https://unpkg.com/leaflet@1.5.1/dist/leaflet.js",
Expand All @@ -44,7 +48,9 @@ def extra_js_urls():


@hookimpl
def extra_body_script(template, database, table, datasette):
def extra_body_script(database, table, columns, datasette):
if not has_columns(database, table, columns, datasette):
return []
config = (
datasette.plugin_config("datasette-cluster-map", database=database, table=table)
or {}
Expand All @@ -69,3 +75,15 @@ def extra_body_script(template, database, table, datasette):
)
)
return "\n".join(js)


def has_columns(database, table, columns, datasette):
if not columns:
return False
config = (
datasette.plugin_config("datasette-cluster-map", database=database, table=table)
or {}
)
latitude_column = config.get("latitude_column") or "latitude"
longitude_column = config.get("longitude_column") or "longitude"
return latitude_column in columns and longitude_column in columns
50 changes: 39 additions & 11 deletions tests/test_cluster_map.py
Expand Up @@ -29,15 +29,21 @@ def db_path(tmp_path_factory):
],
pk="id",
)
db.create_view(
"places_lat_lng",
"select id, name, address, latitude as lat, longitude as lng from places",
)
db["dogs"].insert({"id": 1, "name": "Cleq"}, pk="id")
return db_path


@pytest.mark.asyncio
@pytest.mark.parametrize(
"config,expected_fragments",
"config,table,expected_fragments",
[
(
{},
"places",
[
'window.DATASETTE_CLUSTER_MAP_TILE_LAYER = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"'
],
Expand All @@ -46,6 +52,7 @@ def db_path(tmp_path_factory):
{
"tile_layer": "https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}"
},
"places",
[
'window.DATASETTE_CLUSTER_MAP_TILE_LAYER = "https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}"'
],
Expand All @@ -55,6 +62,7 @@ def db_path(tmp_path_factory):
"tile_layer": "https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}",
"tile_layer_options": {"minZoom": 1, "maxZoom": 16},
},
"places",
[
'window.DATASETTE_CLUSTER_MAP_TILE_LAYER = "https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}"',
'window.DATASETTE_CLUSTER_MAP_TILE_LAYER_OPTIONS = {"minZoom": 1, "maxZoom": 16};',
Expand All @@ -66,6 +74,7 @@ def db_path(tmp_path_factory):
"longitude_column": "lng",
"container": "#map-goes-here",
},
"places_lat_lng",
[
'window.DATASETTE_CLUSTER_MAP_LATITUDE_COLUMN = "lat";',
'window.DATASETTE_CLUSTER_MAP_LONGITUDE_COLUMN = "lng";',
Expand All @@ -74,17 +83,44 @@ def db_path(tmp_path_factory):
),
],
)
async def test_plugin_config(db_path, config, expected_fragments):
async def test_plugin_config(db_path, config, table, expected_fragments):
app = Datasette(
[db_path], metadata={"plugins": {"datasette-cluster-map": config}}
).app()
async with httpx.AsyncClient(app=app) as client:
response = await client.get("http://localhost/test/places")
response = await client.get("http://localhost/test/{}".format(table))
assert response.status_code == 200
for fragment in expected_fragments:
assert fragment in response.text


@pytest.mark.asyncio
@pytest.mark.parametrize(
"path,should_have_javascript",
[
("/", False),
("/test", False),
("/-/config", False),
("/test/dogs", False),
("/test/places", True),
],
)
async def test_plugin_only_on_tables_with_columns(
db_path, path, should_have_javascript
):
app = Datasette([db_path]).app()
fragments = ("/datasette-cluster-map.js", "window.DATASETTE_CLUSTER_MAP_TILE_LAYER")
async with httpx.AsyncClient(app=app) as client:
response = await client.get("http://localhost{}".format(path))
assert response.status_code == 200
if should_have_javascript:
for fragment in fragments:
assert fragment in response.text
else:
for fragment in fragments:
assert fragment not in response.text


@pytest.mark.asyncio
async def test_plugin_is_installed():
app = Datasette([], memory=True).app()
Expand All @@ -99,11 +135,3 @@ async def test_plugin_is_installed():
)
assert response.status_code == 200
assert "const clusterMapEscapeHTML" in response.text


def test_extra_css_urls():
assert isinstance(extra_css_urls(), list)


def test_extra_js_urls():
assert isinstance(extra_js_urls(), list)

0 comments on commit 6e7e269

Please sign in to comment.