From 470ac76cba2bc8fc3ab223b449faa8e1af94acb7 Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 24 Aug 2022 13:09:07 +0200 Subject: [PATCH] Move Postgis API tests to own module TODO: Enable tests for Solr --- ckanext/spatial/plugin/__init__.py | 2 +- .../spatial/tests/functional/test_widgets.py | 18 +- .../tests/postgis/test_package_extent.py | 160 +++++++++++++++++- 3 files changed, 165 insertions(+), 15 deletions(-) diff --git a/ckanext/spatial/plugin/__init__.py b/ckanext/spatial/plugin/__init__.py index 7f37651..03b854c 100644 --- a/ckanext/spatial/plugin/__init__.py +++ b/ckanext/spatial/plugin/__init__.py @@ -337,7 +337,7 @@ class SpatialQuery(SpatialQueryMixin, p.SingletonPlugin): return search_params def _params_for_postgis_search(self, bbox, search_params): - from ckanext.spatial.lib import bbox_query, bbox_query_ordered + from ckanext.spatial.postgis.model import bbox_query, bbox_query_ordered from ckan.lib.search import SearchError # Note: This will be deprecated at some point in favour of the diff --git a/ckanext/spatial/tests/functional/test_widgets.py b/ckanext/spatial/tests/functional/test_widgets.py index 756a02c..fdc9d86 100644 --- a/ckanext/spatial/tests/functional/test_widgets.py +++ b/ckanext/spatial/tests/functional/test_widgets.py @@ -1,24 +1,22 @@ import pytest -from ckan.lib.helpers import url_for from ckanext.spatial.tests.base import SpatialTestBase -import ckan.tests.factories as factories +from ckan.tests import factories import ckan.plugins.toolkit as tk + class TestSpatialWidgets(SpatialTestBase): - @pytest.mark.usefixtures('with_plugins', 'clean_postgis', 'clean_db', 'clean_index', 'harvest_setup', 'spatial_setup') + @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"]} - ], + extras=[{"key": "spatial", "value": self.geojson_examples["point"]}], ) if tk.check_ckan_version(min_version="2.9"): - offset = url_for("dataset.read", id=dataset["id"]) + offset = tk.url_for("dataset.read", id=dataset["id"]) else: - offset = url_for(controller="package", action="read", id=dataset["id"]) + offset = tk.url_for(controller="package", action="read", id=dataset["id"]) res = app.get(offset) assert 'data-module="dataset-map"' in res @@ -26,9 +24,9 @@ class TestSpatialWidgets(SpatialTestBase): def test_spatial_search_widget(self, app): if tk.check_ckan_version(min_version="2.9"): - offset = url_for("dataset.search") + offset = tk.url_for("dataset.search") else: - offset = url_for(controller="package", action="search") + offset = tk.url_for(controller="package", action="search") res = app.get(offset) assert 'data-module="spatial-query"' in res diff --git a/ckanext/spatial/tests/postgis/test_package_extent.py b/ckanext/spatial/tests/postgis/test_package_extent.py index b78a5a1..a98b3a2 100644 --- a/ckanext/spatial/tests/postgis/test_package_extent.py +++ b/ckanext/spatial/tests/postgis/test_package_extent.py @@ -13,6 +13,7 @@ from ckantoolkit.tests import helpers from ckan import model from ckan.model import Session from ckan.lib.helpers import json +from ckan.lib.search import SearchError from ckan.lib.munge import munge_title_to_name @@ -53,13 +54,18 @@ def spatial_setup(): pytestmark = pytest.mark.skipif( - tk.asbool(tk.config.get('ckan.spatial.use_postgis', False)) is False, - reason="PostGIS is no longer used by default" + tk.asbool(tk.config.get("ckan.spatial.use_postgis", False)) is False, + reason="PostGIS is no longer used by default", ) @pytest.mark.usefixtures( - "with_plugins", "clean_postgis", "clean_db", "clean_index", "harvest_setup", "spatial_setup" + "with_plugins", + "clean_postgis", + "clean_db", + "clean_index", + "harvest_setup", + "spatial_setup", ) class TestPackageExtent(SpatialTestBase): def test_create_extent(self): @@ -273,7 +279,12 @@ class TestBboxQueryPerformance(SpatialQueryTestBase): @pytest.mark.usefixtures( - "with_plugins", "clean_postgis", "clean_db", "clean_index", "harvest_setup", "spatial_setup" + "with_plugins", + "clean_postgis", + "clean_db", + "clean_index", + "harvest_setup", + "spatial_setup", ) class TestSpatialExtra(SpatialTestBase): def test_spatial_extra_base(self, app): @@ -440,3 +451,144 @@ class TestSpatialExtra(SpatialTestBase): assert "Error" in res, res assert "Spatial" in res assert "Error creating geometry" in res + + +extents = { + "nz": '{"type":"Polygon","coordinates":[[[174,-38],[176,-38],[176,-40],[174,-40],[174,-38]]]}', + "ohio": '{"type": "Polygon","coordinates": [[[-84,38],[-84,40],[-80,42],[-80,38],[-84,38]]]}', + "dateline": '{"type":"Polygon","coordinates":[[[169,70],[169,60],[192,60],[192,70],[169,70]]]}', + "dateline2": '{"type":"Polygon","coordinates":[[[170,60],[-170,60],[-170,70],[170,70],[170,60]]]}', +} + + +@pytest.mark.usefixtures( + "clean_postgis", "clean_db", "clean_index", "harvest_setup", "spatial_setup" +) +class TestSearchActionPostgis(SpatialTestBase): + def test_spatial_query(self): + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": self.geojson_examples["point"]}] + ) + + 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_spatial_query_outside_bbox(self): + + factories.Dataset( + extras=[{"key": "spatial", "value": self.geojson_examples["point"]}] + ) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "-10,-20,10,20"} + ) + + assert result["count"] == 0 + + def test_spatial_query_wrong_bbox(self): + with pytest.raises(SearchError): + helpers.call_action( + "package_search", + extras={"ext_bbox": "-10,-20,10,a"}, + ) + + def test_spatial_query_nz(self): + dataset = factories.Dataset(extras=[{"key": "spatial", "value": extents["nz"]}]) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "56,-54,189,-28"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"] + + def test_spatial_query_nz_wrap(self): + dataset = factories.Dataset(extras=[{"key": "spatial", "value": extents["nz"]}]) + result = helpers.call_action( + "package_search", extras={"ext_bbox": "-203,-54,-167,-28"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"] + + def test_spatial_query_ohio(self): + + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": extents["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"] + + def test_spatial_query_ohio_wrap(self): + + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": extents["ohio"]}] + ) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "258,37,281,51"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"] + + def test_spatial_query_dateline_1(self): + + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": extents["dateline"]}] + ) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "-197,56,-128,70"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"] + + def test_spatial_query_dateline_2(self): + + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": extents["dateline"]}] + ) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "162,54,237,70"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"] + + def test_spatial_query_dateline_3(self): + + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": extents["dateline2"]}] + ) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "-197,56,-128,70"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"] + + def test_spatial_query_dateline_4(self): + + dataset = factories.Dataset( + extras=[{"key": "spatial", "value": extents["dateline2"]}] + ) + + result = helpers.call_action( + "package_search", extras={"ext_bbox": "162,54,237,70"} + ) + + assert result["count"] == 1 + assert result["results"][0]["id"] == dataset["id"]