Skip to content

Commit

Permalink
fix exclude/include tables
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Oct 17, 2023
1 parent 9c5bbf0 commit 5777e16
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 67 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
)
Expand All @@ -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.
Expand Down
31 changes: 30 additions & 1 deletion tests/routes/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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"]
Empty file added tipg/py.typed
Empty file.
7 changes: 4 additions & 3 deletions tipg/sql/dbcatalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 5777e16

Please sign in to comment.