From c1c5257465433e8180d1024aba3510c65d8646ba Mon Sep 17 00:00:00 2001 From: Jack Arthur Harrhy Date: Tue, 18 Jul 2023 23:25:25 -0230 Subject: [PATCH 1/3] fix function based collections without params --- tipg/collections.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tipg/collections.py b/tipg/collections.py index 61b77ff8..00c41024 100644 --- a/tipg/collections.py +++ b/tipg/collections.py @@ -970,6 +970,9 @@ async def get_collection_index( # noqa: C901 ): geometry_column = c + if table.get("parameters") is None: + table["parameters"] = [] + catalog[table_id] = Collection( type=table["entity"], id=table_id, @@ -980,7 +983,7 @@ async def get_collection_index( # noqa: C901 properties=properties, datetime_column=datetime_column, geometry_column=geometry_column, - parameters=table.get("parameters", []), + parameters=table["parameters"], ) return Catalog(collections=catalog, last_updated=datetime.datetime.now()) From 644e7d3471a7ce3524814b03dd1c53cf5d6ce0cf Mon Sep 17 00:00:00 2001 From: Jack Arthur Harrhy Date: Tue, 18 Jul 2023 23:35:22 -0230 Subject: [PATCH 2/3] updated changelog --- CHANGES.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 26aba620..a8a37930 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,10 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin - fixed a small bug in the `tipg_properties` SQL function where the bounds property was not properly transformed to 4326 (author @RemcoMeeuwissen, https://github.com/developmentseed/tipg/pull/87) - added popups to leaflet maps on `items` and `item` page. (author @krishnaglodha & @jackharrhy, https://github.com/developmentseed/tipg/pull/91, https://github.com/developmentseed/tipg/pull/94) +### Fixed + +- fix handling functions that are interpreted as collections but lack parameters (author @jackharrhy, https://github.com/developmentseed/tipg/pull/96) + ## [0.2.0] - 2023-06-22 ### Changed From cd10708fa87dbb59ec6a588ddd04476b5091444a Mon Sep 17 00:00:00 2001 From: vincentsarago Date: Tue, 25 Jul 2023 11:55:35 +0200 Subject: [PATCH 3/3] edit and add tests --- CHANGES.md | 5 +---- tests/conftest.py | 29 +++++++++++++++++++++++++++++ tests/test_schemas.py | 21 +++++++++++++++++++++ tipg/collections.py | 5 +---- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 21457d7b..ad41df87 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -13,6 +13,7 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin - added `python-dotenv` dependency via `pydantic[dotenv]` - `type` query parameter to filter collections based on their type (`Function` or `Table`) - fixed a small bug in the `tipg_properties` SQL function where the bounds property was not properly transformed to 4326 (author @RemcoMeeuwissen, https://github.com/developmentseed/tipg/pull/87) +- handling functions that are interpreted as collections but lack parameters (author @jackharrhy, https://github.com/developmentseed/tipg/pull/96) ### Added @@ -24,10 +25,6 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin - pin `geojson-pydantic` to `>=0.4.3,<1.0` to avoid geojson-pydantic breaking changes - pin `pydantic` to `~=1.0` -### Fixed - -- fix handling functions that are interpreted as collections but lack parameters (author @jackharrhy, https://github.com/developmentseed/tipg/pull/96) - ## [0.2.0] - 2023-06-22 ### Changed diff --git a/tests/conftest.py b/tests/conftest.py index 5f020df4..3283791a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -78,6 +78,14 @@ def database_url(test_db): ).scalar() assert count_landsat == count_landsat_schema + # add a `userschema` schema + test_db.create_schema("userschema") + assert test_db.has_schema("userschema") + + test_db.connection.execute( + "CREATE OR REPLACE FUNCTION userschema.test_no_params() RETURNS TABLE(foo integer, location geometry) AS 'SELECT 1, ST_MakePoint(0,0);' LANGUAGE SQL;" + ) + return str(test_db.connection.engine.url) @@ -334,6 +342,27 @@ def app_myschema_public_order(database_url, monkeypatch): yield client +@pytest.fixture +def app_user_schema(database_url): + """Create APP with only tables from `userschema` schemas.""" + postgres_settings = PostgresSettings(database_url=database_url) + db_settings = DatabaseSettings( + schemas=["userschema"], + exclude_table_schemas=["public"], + only_spatial_tables=False, + ) + sql_settings = CustomSQLSettings(custom_sql_directory=None) + + app = create_tipg_app( + postgres_settings=postgres_settings, + db_settings=db_settings, + sql_settings=sql_settings, + ) + + with TestClient(app) as client: + yield client + + @pytest.fixture def app_middleware_refresh(database_url, monkeypatch): """Create APP with CatalogUpdateMiddleware middleware.""" diff --git a/tests/test_schemas.py b/tests/test_schemas.py index 10daad04..848ad470 100644 --- a/tests/test_schemas.py +++ b/tests/test_schemas.py @@ -147,3 +147,24 @@ def test_myschema_and_public_order(app_myschema_public_order): assert "public.st_hexagongrid" not in ids assert body["numberMatched"] == collection_number + + +def test_user_schema(app_user_schema): + """Test Function without parameters.""" + collection_number = 1 + + response = app_user_schema.get("/collections") + assert response.status_code == 200 + body = response.json() + ids = [x["id"] for x in body["collections"]] + + assert len(ids) == collection_number + assert "userschema.test_no_params" in ids + + response = app_user_schema.get("/collections/userschema.test_no_params/queryables") + assert response.status_code == 200 + + response = app_user_schema.get("/collections/userschema.test_no_params/items") + assert response.status_code == 200 + body = response.json() + assert body["numberMatched"] == 1 diff --git a/tipg/collections.py b/tipg/collections.py index 00c41024..517aba39 100644 --- a/tipg/collections.py +++ b/tipg/collections.py @@ -970,9 +970,6 @@ async def get_collection_index( # noqa: C901 ): geometry_column = c - if table.get("parameters") is None: - table["parameters"] = [] - catalog[table_id] = Collection( type=table["entity"], id=table_id, @@ -983,7 +980,7 @@ async def get_collection_index( # noqa: C901 properties=properties, datetime_column=datetime_column, geometry_column=geometry_column, - parameters=table["parameters"], + parameters=table.get("parameters") or [], ) return Catalog(collections=catalog, last_updated=datetime.datetime.now())