Skip to content

Commit

Permalink
update from main
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Oct 30, 2023
2 parents 043d846 + 1fef6fc commit 58a53f2
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 10 deletions.
33 changes: 32 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,45 @@ Note: Minor version `0.X.0` update might break the API, It's recommended to pin

## [unreleased]

### added

- add `py.typed` file

### fixed

- hide map element in HTML pages when collections/items do not have spatial component
- hide map element in HTML pages when collections/items do not have spatial component (https://github.com/developmentseed/tipg/issues/132)

- exclude/include tables and functions (https://github.com/developmentseed/tipg/issues/130)

### changed

- split endpoints registration for more customization

```python
# before
class OGCFeaturesFactory(EndpointsFactory):

def register_routes(self):
@self.router.get("/collections", ...)
@self.router.get("/collections/{collectionId}", ...)
...

# now
class OGCFeaturesFactory(EndpointsFactory):

def register_routes(self):
self._collections_route()
self._collection_route()
self._queryables_route()
self._items_route()
self._item_route()

def _collections_route(self):
@self.router.get("/collections", ...)

...
```

- make `Catalog` a Pydantic model and add `matched`, `next` and `prev` attributes

```python
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"] == 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.
9 changes: 5 additions & 4 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 (tables IS NULL OR concat(c.relnamespace::regnamespace::text,'.',c.relname::text) = ANY (tables))
AND (exclude_tables IS NULL OR concat(pg_temp.nspname(relnamespace),'.',c.relname::text) <> ALL (exclude_tables))
AND (tables IS NULL OR concat(pg_temp.nspname(relnamespace),'.',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(pg_temp.nspname(pronamespace),'.', proname::text) <> ALL (exclude_functions))
AND (functions IS NULL OR concat(pg_temp.nspname(pronamespace),'.', proname::text) = ANY (functions))
AND p.proname::text NOT ILIKE 'tipg_%'
)
SELECT meta FROM a
Expand Down

0 comments on commit 58a53f2

Please sign in to comment.