diff --git a/CHANGES.md b/CHANGES.md index 80034604..1aabfd20 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/). Note: Minor version `0.X.0` update might break the API, It's recommended to pin `tipg` to minor version: `tipg>=0.1,<0.2` +## [unreleased] + +- add `py.typed` file + ## [0.4.4] - 2023-10-03 ### fixed diff --git a/tests/conftest.py b/tests/conftest.py index bee9acc7..1c74129c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -181,11 +181,11 @@ def app(database_url, monkeypatch): @pytest.fixture def app_excludes(database_url, monkeypatch): - """Create APP with but excludes `public.nongeo_data` table.""" + """Create APP with but excludes `public.nongeo_data` and `public.minnesota` tables.""" postgres_settings = PostgresSettings(database_url=database_url) db_settings = DatabaseSettings( schemas=["public"], - exclude_tables=["public.nongeo_data"], + exclude_tables=["public.nongeo_data", "public.minnesota"], functions=[], only_spatial_tables=False, ) @@ -203,11 +203,11 @@ def app_excludes(database_url, monkeypatch): @pytest.fixture def app_includes(database_url, monkeypatch): - """Create APP with only `public.nongeo_data` table.""" + """Create APP with only `public.nongeo_data` and `public.minnesota` table.""" postgres_settings = PostgresSettings(database_url=database_url) db_settings = DatabaseSettings( schemas=["public"], - tables=["public.nongeo_data"], + tables=["public.nongeo_data", "public.minnesota"], functions=[], only_spatial_tables=False, ) @@ -223,6 +223,69 @@ def app_includes(database_url, monkeypatch): yield client +@pytest.fixture +def app_excludes_function(database_url, monkeypatch): + """Create APP with but excludes `pg_temp.squares` and `public.st_squaregrid` functions.""" + postgres_settings = PostgresSettings(database_url=database_url) + db_settings = DatabaseSettings( + schemas=["public"], + tables=[], + exclude_functions=["pg_temp.squares", "public.st_squaregrid"], + ) + sql_settings = CustomSQLSettings(custom_sql_directory=SQL_FUNCTIONS_DIRECTORY) + + 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_includes_function(database_url, monkeypatch): + """Create APP with only `public.nongeo_data` table.""" + postgres_settings = PostgresSettings(database_url=database_url) + db_settings = DatabaseSettings( + schemas=[], + tables=[], + functions=["pg_temp.hexagons"], + ) + sql_settings = CustomSQLSettings(custom_sql_directory=SQL_FUNCTIONS_DIRECTORY) + + 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_empty(database_url, monkeypatch): + """Create APP with only no table nor function.""" + postgres_settings = PostgresSettings(database_url=database_url) + db_settings = DatabaseSettings( + schemas=[], + tables=[], + functions=[], + ) + sql_settings = CustomSQLSettings(custom_sql_directory=SQL_FUNCTIONS_DIRECTORY) + + 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_myschema(database_url): """Create APP with only tables from `myschema` schema and no function schema. diff --git a/tests/routes/test_collections.py b/tests/routes/test_collections.py index 649bb6bc..74c037e9 100644 --- a/tests/routes/test_collections.py +++ b/tests/routes/test_collections.py @@ -132,6 +132,7 @@ def test_collections_excludes(app_excludes): ids = [x["id"] for x in body["collections"]] assert "public.my_data" in ids assert "public.nongeo_data" not in ids + assert "public.minnesota" not in ids def test_collections_includes(app_includes): @@ -140,7 +141,7 @@ def test_collections_includes(app_includes): assert response.status_code == 200 body = response.json() ids = [x["id"] for x in body["collections"]] - assert ["public.nongeo_data"] == ids + assert ["public.minnesota", "public.nongeo_data"] == ids def test_collections_landsat(app): @@ -252,3 +253,31 @@ def test_collections_type_filter(app): # Functions assert "public.st_squaregrid" not in ids + + +def test_collections_exclude_functions(app_excludes_function): + """Test /collections endpoint.""" + response = app_excludes_function.get("/collections") + assert response.status_code == 200 + body = response.json() + ids = [x["id"] for x in body["collections"]] + assert "pg_temp.hexagons" in ids + assert "pg_temp.squares" not in ids + assert "public.st_squaregrid" not in ids + + +def test_collections_include_functions(app_includes_function): + """Test /collections endpoint.""" + response = app_includes_function.get("/collections") + assert response.status_code == 200 + body = response.json() + ids = [x["id"] for x in body["collections"]] + assert ["pg_temp.hexagons", "pg_temp.squares"] == ids + + +def test_collections_empty(app_empty): + """Test /collections endpoint.""" + response = app_empty.get("/collections") + assert response.status_code == 200 + body = response.json() + assert not body["collections"] diff --git a/tipg/py.typed b/tipg/py.typed new file mode 100644 index 00000000..e69de29b diff --git a/tipg/sql/dbcatalog.sql b/tipg/sql/dbcatalog.sql index 651d42ee..bf230312 100644 --- a/tipg/sql/dbcatalog.sql +++ b/tipg/sql/dbcatalog.sql @@ -246,10 +246,11 @@ CREATE OR REPLACE FUNCTION pg_temp.tipg_catalog( AND relkind IN ('r','v', 'm', 'f', 'p') AND has_table_privilege(c.oid, 'SELECT') AND c.relname::text NOT IN ('spatial_ref_sys','geometry_columns','geography_columns') - AND (exclude_tables IS NULL OR concat(c.relnamespace::regnamespace::text,'.',c.relname::text) != ANY (exclude_tables)) + AND (exclude_tables IS NULL OR concat(c.relnamespace::regnamespace::text,'.',c.relname::text) <> ALL (exclude_tables)) AND (tables IS NULL OR concat(c.relnamespace::regnamespace::text,'.',c.relname::text) = ANY (tables)) UNION ALL + SELECT pg_temp.tipg_fproperties(p) as meta FROM @@ -262,8 +263,8 @@ CREATE OR REPLACE FUNCTION pg_temp.tipg_catalog( AND '' != ANY(proargnames) AND has_function_privilege(oid, 'execute') AND provariadic=0 - AND (functions IS NULL OR concat(p.pronamespace::regnamespace::text, '.', proname::text) = ANY (functions)) - AND (exclude_functions IS NULL OR concat(p.pronamespace::regnamespace::text,'.',proname::text) != ANY (exclude_functions)) + AND (exclude_functions IS NULL OR concat(p.pronamespace::regnamespace::text,'.',proname::text) <> ALL (exclude_functions)) + AND (functions IS NULL OR concat(p.pronamespace::regnamespace::text,'.',proname::text) = ANY (functions)) AND p.proname::text NOT ILIKE 'tipg_%' ) SELECT meta FROM a