Add tests for custom indexing and multivalued geometries

This commit is contained in:
amercader 2022-09-06 14:37:40 +02:00
parent efc244bff4
commit 3fce3934bf
4 changed files with 80 additions and 2 deletions

View File

@ -7,8 +7,10 @@ from ckan.tests import factories
import ckan.plugins.toolkit as tk
@pytest.mark.usefixtures("with_plugins", "clean_db", "clean_index", "harvest_setup")
@pytest.mark.ckan_config(
"ckan.plugins", "test_spatial_plugin spatial_metadata spatial_query")
class TestSpatialWidgets(SpatialTestBase):
@pytest.mark.usefixtures("with_plugins", "clean_db", "clean_index", "harvest_setup")
def test_dataset_map(self, app):
dataset = factories.Dataset(
extras=[{"key": "spatial", "value": self.geojson_examples["point"]}],

View File

@ -1,3 +1,5 @@
import json
import shapely
from ckan import plugins as p
@ -5,5 +7,27 @@ class TestSpatialPlugin(p.SingletonPlugin):
p.implements(p.IConfigurer, inherit=True)
p.implements(p.IPackageController, inherit=True)
def update_config(self, config):
p.toolkit.add_template_directory(config, "templates")
def before_dataset_index(self, pkg_dict):
if not pkg_dict.get("my_geoms"):
return pkg_dict
pkg_dict["spatial_geom"] = []
my_geoms = json.loads(pkg_dict["my_geoms"])
if not isinstance(my_geoms, list):
my_geoms = [my_geoms]
for geom in my_geoms:
shape = shapely.geometry.shape(geom)
pkg_dict["spatial_geom"].append(shape.wkt)
return pkg_dict

View File

@ -596,3 +596,55 @@ class TestSpatialFieldSearch(SpatialTestBase):
assert result["count"] == 0
@pytest.mark.usefixtures("clean_db", "clean_index", "harvest_setup", "with_plugins")
@pytest.mark.ckan_config(
"ckan.plugins", "test_spatial_plugin spatial_metadata spatial_query")
@pytest.mark.ckan_config("ckanext.spatial.search_backend", "solr-spatial-field")
class TestCustomIndexing(SpatialTestBase):
"""
These tests ensure both that
1. You can use your own custom logic to index geometries
2. The spatial fields are multivaule, ie you can index more than one geometry against
the same dataset
"""
def test_single_geom(self):
dataset = factories.Dataset(
extras=[{"key": "my_geoms", "value": self.geojson_examples["polygon"]}]
)
result = helpers.call_action(
"package_search", extras={"ext_bbox": "-180,-90,180,90"}
)
assert result["count"] == 1
assert result["results"][0]["id"] == dataset["id"]
def test_multiple_geoms(self):
dataset = factories.Dataset(
extras=[
{
"key": "my_geoms",
"value": "[{}, {}]".format(
extents["nz"], extents["ohio"])
}
]
)
# Test that we get the same dataset using two different extents
# New Zealand
result = helpers.call_action(
"package_search", extras={"ext_bbox": "56,-54,189,-28"}
)
assert result["count"] == 1
assert result["results"][0]["id"] == dataset["id"]
# Ohio
result = helpers.call_action(
"package_search", extras={"ext_bbox": "-110,37,-78,53"}
)
assert result["count"] == 1
assert result["results"][0]["id"] == dataset["id"]

View File

@ -14,7 +14,7 @@ port = 5000
[app:main]
use = config:../ckan/test-core.ini
ckan.legacy_templates = false
ckan.plugins = test_spatial_plugin harvest spatial_metadata spatial_query spatial_harvest_metadata_api gemini_csw_harvester gemini_doc_harvester gemini_waf_harvester
ckan.plugins = harvest spatial_metadata spatial_query spatial_harvest_metadata_api gemini_csw_harvester gemini_doc_harvester gemini_waf_harvester
ckan.spatial.srid = 4326
ckan.spatial.default_map_extent=-6.88,49.74,0.50,59.2
ckan.spatial.testing = true