Skip to content

Commit 8a54e31

Browse files
Merge pull request #131 from developmentseed/patch/includes-excludes
fix exclude/include tables
2 parents 7859298 + 77722da commit 8a54e31

File tree

5 files changed

+111
-9
lines changed

5 files changed

+111
-9
lines changed

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,16 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin
88

99
## [unreleased]
1010

11+
### added
12+
13+
- add `py.typed` file
14+
15+
### fixed
16+
1117
- hide map element in HTML pages when collections/items do not have spatial component
18+
19+
### changed
20+
1221
- split endpoints registration for more customization
1322

1423
## [0.4.4] - 2023-10-03

tests/conftest.py

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ def app(database_url, monkeypatch):
181181

182182
@pytest.fixture
183183
def app_excludes(database_url, monkeypatch):
184-
"""Create APP with but excludes `public.nongeo_data` table."""
184+
"""Create APP with but excludes `public.nongeo_data` and `public.minnesota` tables."""
185185
postgres_settings = PostgresSettings(database_url=database_url)
186186
db_settings = DatabaseSettings(
187187
schemas=["public"],
188-
exclude_tables=["public.nongeo_data"],
188+
exclude_tables=["public.nongeo_data", "public.minnesota"],
189189
functions=[],
190190
only_spatial_tables=False,
191191
)
@@ -203,11 +203,11 @@ def app_excludes(database_url, monkeypatch):
203203

204204
@pytest.fixture
205205
def app_includes(database_url, monkeypatch):
206-
"""Create APP with only `public.nongeo_data` table."""
206+
"""Create APP with only `public.nongeo_data` and `public.minnesota` table."""
207207
postgres_settings = PostgresSettings(database_url=database_url)
208208
db_settings = DatabaseSettings(
209209
schemas=["public"],
210-
tables=["public.nongeo_data"],
210+
tables=["public.nongeo_data", "public.minnesota"],
211211
functions=[],
212212
only_spatial_tables=False,
213213
)
@@ -223,6 +223,69 @@ def app_includes(database_url, monkeypatch):
223223
yield client
224224

225225

226+
@pytest.fixture
227+
def app_excludes_function(database_url, monkeypatch):
228+
"""Create APP with but excludes `pg_temp.squares` and `public.st_squaregrid` functions."""
229+
postgres_settings = PostgresSettings(database_url=database_url)
230+
db_settings = DatabaseSettings(
231+
schemas=["public"],
232+
tables=[],
233+
exclude_functions=["pg_temp.squares", "public.st_squaregrid"],
234+
)
235+
sql_settings = CustomSQLSettings(custom_sql_directory=SQL_FUNCTIONS_DIRECTORY)
236+
237+
app = create_tipg_app(
238+
postgres_settings=postgres_settings,
239+
db_settings=db_settings,
240+
sql_settings=sql_settings,
241+
)
242+
243+
with TestClient(app) as client:
244+
yield client
245+
246+
247+
@pytest.fixture
248+
def app_includes_function(database_url, monkeypatch):
249+
"""Create APP with only `public.nongeo_data` table."""
250+
postgres_settings = PostgresSettings(database_url=database_url)
251+
db_settings = DatabaseSettings(
252+
schemas=[],
253+
tables=[],
254+
functions=["pg_temp.hexagons"],
255+
)
256+
sql_settings = CustomSQLSettings(custom_sql_directory=SQL_FUNCTIONS_DIRECTORY)
257+
258+
app = create_tipg_app(
259+
postgres_settings=postgres_settings,
260+
db_settings=db_settings,
261+
sql_settings=sql_settings,
262+
)
263+
264+
with TestClient(app) as client:
265+
yield client
266+
267+
268+
@pytest.fixture
269+
def app_empty(database_url, monkeypatch):
270+
"""Create APP with only no table nor function."""
271+
postgres_settings = PostgresSettings(database_url=database_url)
272+
db_settings = DatabaseSettings(
273+
schemas=[],
274+
tables=[],
275+
functions=[],
276+
)
277+
sql_settings = CustomSQLSettings(custom_sql_directory=SQL_FUNCTIONS_DIRECTORY)
278+
279+
app = create_tipg_app(
280+
postgres_settings=postgres_settings,
281+
db_settings=db_settings,
282+
sql_settings=sql_settings,
283+
)
284+
285+
with TestClient(app) as client:
286+
yield client
287+
288+
226289
@pytest.fixture
227290
def app_myschema(database_url):
228291
"""Create APP with only tables from `myschema` schema and no function schema.

tests/routes/test_collections.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def test_collections_excludes(app_excludes):
132132
ids = [x["id"] for x in body["collections"]]
133133
assert "public.my_data" in ids
134134
assert "public.nongeo_data" not in ids
135+
assert "public.minnesota" not in ids
135136

136137

137138
def test_collections_includes(app_includes):
@@ -140,7 +141,7 @@ def test_collections_includes(app_includes):
140141
assert response.status_code == 200
141142
body = response.json()
142143
ids = [x["id"] for x in body["collections"]]
143-
assert ["public.nongeo_data"] == ids
144+
assert ["public.minnesota", "public.nongeo_data"] == ids
144145

145146

146147
def test_collections_landsat(app):
@@ -252,3 +253,31 @@ def test_collections_type_filter(app):
252253

253254
# Functions
254255
assert "public.st_squaregrid" not in ids
256+
257+
258+
def test_collections_exclude_functions(app_excludes_function):
259+
"""Test /collections endpoint."""
260+
response = app_excludes_function.get("/collections")
261+
assert response.status_code == 200
262+
body = response.json()
263+
ids = [x["id"] for x in body["collections"]]
264+
assert "pg_temp.hexagons" in ids
265+
assert "pg_temp.squares" not in ids
266+
assert "public.st_squaregrid" not in ids
267+
268+
269+
def test_collections_include_functions(app_includes_function):
270+
"""Test /collections endpoint."""
271+
response = app_includes_function.get("/collections")
272+
assert response.status_code == 200
273+
body = response.json()
274+
ids = [x["id"] for x in body["collections"]]
275+
assert ["pg_temp.hexagons"] == ids
276+
277+
278+
def test_collections_empty(app_empty):
279+
"""Test /collections endpoint."""
280+
response = app_empty.get("/collections")
281+
assert response.status_code == 200
282+
body = response.json()
283+
assert not body["collections"]

tipg/py.typed

Whitespace-only changes.

tipg/sql/dbcatalog.sql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,11 @@ CREATE OR REPLACE FUNCTION pg_temp.tipg_catalog(
246246
AND relkind IN ('r','v', 'm', 'f', 'p')
247247
AND has_table_privilege(c.oid, 'SELECT')
248248
AND c.relname::text NOT IN ('spatial_ref_sys','geometry_columns','geography_columns')
249-
AND (exclude_tables IS NULL OR concat(c.relnamespace::regnamespace::text,'.',c.relname::text) != ANY (exclude_tables))
250-
AND (tables IS NULL OR concat(c.relnamespace::regnamespace::text,'.',c.relname::text) = ANY (tables))
249+
AND (exclude_tables IS NULL OR concat(pg_temp.nspname(relnamespace),'.',c.relname::text) <> ALL (exclude_tables))
250+
AND (tables IS NULL OR concat(pg_temp.nspname(relnamespace),'.',c.relname::text) = ANY (tables))
251251

252252
UNION ALL
253+
253254
SELECT
254255
pg_temp.tipg_fproperties(p) as meta
255256
FROM
@@ -262,8 +263,8 @@ CREATE OR REPLACE FUNCTION pg_temp.tipg_catalog(
262263
AND '' != ANY(proargnames)
263264
AND has_function_privilege(oid, 'execute')
264265
AND provariadic=0
265-
AND (functions IS NULL OR concat(p.pronamespace::regnamespace::text, '.', proname::text) = ANY (functions))
266-
AND (exclude_functions IS NULL OR concat(p.pronamespace::regnamespace::text,'.',proname::text) != ANY (exclude_functions))
266+
AND (exclude_functions IS NULL OR concat(pg_temp.nspname(pronamespace),'.', proname::text) <> ALL (exclude_functions))
267+
AND (functions IS NULL OR concat(pg_temp.nspname(pronamespace),'.', proname::text) = ANY (functions))
267268
AND p.proname::text NOT ILIKE 'tipg_%'
268269
)
269270
SELECT meta FROM a

0 commit comments

Comments
 (0)