Skip to content

Commit d82345b

Browse files
Merge pull request #96 from jackharrhy/fix-functions-without-params
fix function based collections without params
2 parents 212d04b + cd10708 commit d82345b

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin
1313
- added `python-dotenv` dependency via `pydantic[dotenv]`
1414
- `type` query parameter to filter collections based on their type (`Function` or `Table`)
1515
- 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)
16+
- handling functions that are interpreted as collections but lack parameters (author @jackharrhy, https://github.com/developmentseed/tipg/pull/96)
1617

1718
### Added
1819

tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ def database_url(test_db):
7878
).scalar()
7979
assert count_landsat == count_landsat_schema
8080

81+
# add a `userschema` schema
82+
test_db.create_schema("userschema")
83+
assert test_db.has_schema("userschema")
84+
85+
test_db.connection.execute(
86+
"CREATE OR REPLACE FUNCTION userschema.test_no_params() RETURNS TABLE(foo integer, location geometry) AS 'SELECT 1, ST_MakePoint(0,0);' LANGUAGE SQL;"
87+
)
88+
8189
return str(test_db.connection.engine.url)
8290

8391

@@ -334,6 +342,27 @@ def app_myschema_public_order(database_url, monkeypatch):
334342
yield client
335343

336344

345+
@pytest.fixture
346+
def app_user_schema(database_url):
347+
"""Create APP with only tables from `userschema` schemas."""
348+
postgres_settings = PostgresSettings(database_url=database_url)
349+
db_settings = DatabaseSettings(
350+
schemas=["userschema"],
351+
exclude_table_schemas=["public"],
352+
only_spatial_tables=False,
353+
)
354+
sql_settings = CustomSQLSettings(custom_sql_directory=None)
355+
356+
app = create_tipg_app(
357+
postgres_settings=postgres_settings,
358+
db_settings=db_settings,
359+
sql_settings=sql_settings,
360+
)
361+
362+
with TestClient(app) as client:
363+
yield client
364+
365+
337366
@pytest.fixture
338367
def app_middleware_refresh(database_url, monkeypatch):
339368
"""Create APP with CatalogUpdateMiddleware middleware."""

tests/test_schemas.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,24 @@ def test_myschema_and_public_order(app_myschema_public_order):
147147
assert "public.st_hexagongrid" not in ids
148148

149149
assert body["numberMatched"] == collection_number
150+
151+
152+
def test_user_schema(app_user_schema):
153+
"""Test Function without parameters."""
154+
collection_number = 1
155+
156+
response = app_user_schema.get("/collections")
157+
assert response.status_code == 200
158+
body = response.json()
159+
ids = [x["id"] for x in body["collections"]]
160+
161+
assert len(ids) == collection_number
162+
assert "userschema.test_no_params" in ids
163+
164+
response = app_user_schema.get("/collections/userschema.test_no_params/queryables")
165+
assert response.status_code == 200
166+
167+
response = app_user_schema.get("/collections/userschema.test_no_params/items")
168+
assert response.status_code == 200
169+
body = response.json()
170+
assert body["numberMatched"] == 1

tipg/collections.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,7 @@ async def get_collection_index( # noqa: C901
980980
properties=properties,
981981
datetime_column=datetime_column,
982982
geometry_column=geometry_column,
983-
parameters=table.get("parameters", []),
983+
parameters=table.get("parameters") or [],
984984
)
985985

986986
return Catalog(collections=catalog, last_updated=datetime.datetime.now())

0 commit comments

Comments
 (0)