Skip to content

Commit 484d066

Browse files
Merge pull request #191 from stac-utils/patch/tests-multi-pgstac-version-and-fix-ro
enable multi pgstac version testing and handle =<0.8.2 pgstac version
2 parents 15cb63c + ee8f452 commit 484d066

File tree

5 files changed

+59
-184
lines changed

5 files changed

+59
-184
lines changed

tests/conftest.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pytest
1010
import rasterio
1111
from pypgstac.db import PgstacDB
12-
from pypgstac.load import Loader
1312
from pypgstac.migrate import Migrate
1413
from pytest_postgresql.janitor import DatabaseJanitor
1514
from rasterio.errors import NotGeoreferencedWarning
@@ -67,9 +66,25 @@ def database(postgresql_proc):
6766
yield jan
6867

6968

70-
@pytest.fixture(scope="session")
71-
def pgstac(database):
69+
def load_json(filepath: str):
70+
"""yield JSON file content."""
71+
with open(filepath, "r") as f:
72+
for line in f:
73+
yield line.strip()
74+
75+
76+
@pytest.fixture(
77+
params=[
78+
"0.7.10",
79+
"0.8.5",
80+
"0.9.1",
81+
],
82+
scope="session",
83+
)
84+
def pgstac(request, database):
7285
"""Create PgSTAC fixture."""
86+
pgstac_version = request.param
87+
7388
connection = f"postgresql://{database.user}:{quote(database.password)}@{database.host}:{database.port}/{database.dbname}"
7489

7590
# Clear PgSTAC
@@ -80,15 +95,22 @@ def pgstac(database):
8095
print("Running to PgSTAC migration...")
8196
with PgstacDB(dsn=connection) as db:
8297
migrator = Migrate(db)
83-
version = migrator.run_migration()
98+
version = migrator.run_migration(pgstac_version)
8499
assert version
85100
print(f"PgSTAC version: {version}")
86101

87-
print("Load items and collection into PgSTAC")
88-
loader = Loader(db=db)
89-
loader.load_collections(collection)
90-
loader.load_collections(collection_maxar)
91-
loader.load_items(items)
102+
with psycopg.connect(connection) as conn:
103+
with conn.cursor() as cur:
104+
for col in load_json(collection):
105+
cur.execute(
106+
"SELECT * FROM pgstac.create_collection(%s::jsonb);", (col,)
107+
)
108+
for col in load_json(collection_maxar):
109+
cur.execute(
110+
"SELECT * FROM pgstac.create_collection(%s::jsonb);", (col,)
111+
)
112+
for item in load_json(items):
113+
cur.execute("SELECT * FROM pgstac.create_item(%s::jsonb);", (item,))
92114

93115
# Make sure we have 1 collection and 163 items in pgstac
94116
with psycopg.connect(connection) as conn:
@@ -104,7 +126,7 @@ def pgstac(database):
104126
yield connection
105127

106128

107-
@pytest.fixture(autouse=True)
129+
@pytest.fixture
108130
def app(pgstac, monkeypatch):
109131
"""Create app with connection to the pytest database."""
110132
monkeypatch.setenv("AWS_ACCESS_KEY_ID", "jqt")

tests/fixtures/maxar_BayOfBengal.json

Lines changed: 1 addition & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,133 +1 @@
1-
{
2-
"id": "MAXAR_BayofBengal_Cyclone_Mocha_May_23",
3-
"type": "Collection",
4-
"links": [],
5-
"title": "Bay of Bengal Cyclone Mocha 2023",
6-
"extent": {
7-
"spatial": {
8-
"bbox": [
9-
[
10-
91.831615,
11-
19.982078842323997,
12-
92.97426268500965,
13-
21.666101
14-
],
15-
[
16-
92.567815,
17-
20.18811887678192,
18-
92.74417544237298,
19-
20.62968532404085
20-
],
21-
[
22-
92.72278776887262,
23-
20.104801,
24-
92.893524,
25-
20.630214
26-
],
27-
[
28-
92.75855246040959,
29-
19.982078842323997,
30-
92.89682495377032,
31-
20.514473160464657
32-
],
33-
[
34-
92.84253515935835,
35-
19.984656587012033,
36-
92.97426268500965,
37-
20.514418665444474
38-
],
39-
[
40-
91.831615,
41-
21.518411,
42-
91.957078,
43-
21.666101
44-
]
45-
]
46-
},
47-
"temporal": {
48-
"interval": [
49-
[
50-
"2023-01-03T04:30:17Z",
51-
"2023-05-22T04:35:25Z"
52-
]
53-
]
54-
}
55-
},
56-
"license": "CC-BY-NC-4.0",
57-
"renders": {
58-
"visual": {
59-
"title": "Visual Image",
60-
"assets": [
61-
"visual"
62-
],
63-
"asset_bidx": ["visual|1,2,3"],
64-
"minmax_zoom": [
65-
8,
66-
22
67-
],
68-
"tilematrixsets": {
69-
"WebMercatorQuad": [
70-
8,
71-
22
72-
]
73-
}
74-
},
75-
"color": {
76-
"title": "Colored Image",
77-
"assets": [
78-
"visual"
79-
],
80-
"asset_bidx": ["visual|1"],
81-
"colormap": {
82-
"1": [0, 0, 0, 255],
83-
"1000": [255, 255, 255, 255]
84-
}
85-
},
86-
"visualr": {
87-
"title": "Rescaled Image",
88-
"assets": [
89-
"visual"
90-
],
91-
"asset_bidx": ["visual|1"],
92-
"rescale": [
93-
[0, 100]
94-
]
95-
}
96-
},
97-
"description": "Maxar OpenData | Cyclone Mocha, a category five cyclone with 130 mph winds and torrential rain, hit parts of Myanmar and Bangladesh, forcing mass evacuations ahead of the storm. The cyclone, one of the most powerful to hit the region in the last decade, made landfall on Sunday, May 14, 2023, near Sittwe in Myanmar's Rakhine state. Rain and a storm surge caused widespread flooding in low-lying areas. The United National Office Coordination of Humanitarian Affairs stated that there had been extensive damage among already vulnerable communities and that communications with the affected areas have been difficult.",
98-
"item_assets": {
99-
"visual": {
100-
"type": "image/tiff; application=geotiff; profile=cloud-optimized",
101-
"roles": [
102-
"visual"
103-
],
104-
"title": "Visual Image"
105-
},
106-
"data-mask": {
107-
"type": "application/geopackage+sqlite3",
108-
"roles": [
109-
"data-mask"
110-
],
111-
"title": "Data Mask"
112-
},
113-
"ms_analytic": {
114-
"type": "image/tiff; application=geotiff; profile=cloud-optimized",
115-
"roles": [
116-
"data"
117-
],
118-
"title": "Multispectral Image"
119-
},
120-
"pan_analytic": {
121-
"type": "image/tiff; application=geotiff; profile=cloud-optimized",
122-
"roles": [
123-
"data"
124-
],
125-
"title": "Panchromatic Image"
126-
}
127-
},
128-
"stac_version": "1.0.0",
129-
"stac_extensions": [
130-
"https://stac-extensions.github.io/item-assets/v1.0.0/schema.json",
131-
"https://stac-extensions.github.io/render/v1.0.0/schema.json"
132-
]
133-
}
1+
{"id":"MAXAR_BayofBengal_Cyclone_Mocha_May_23","type":"Collection","links":[],"title":"Bay of Bengal Cyclone Mocha 2023","extent":{"spatial":{"bbox":[[91.831615,19.982078842323997,92.97426268500965,21.666101],[92.567815,20.18811887678192,92.74417544237298,20.62968532404085],[92.72278776887262,20.104801,92.893524,20.630214],[92.75855246040959,19.982078842323997,92.89682495377032,20.514473160464657],[92.84253515935835,19.984656587012033,92.97426268500965,20.514418665444474],[91.831615,21.518411,91.957078,21.666101]]},"temporal":{"interval":[["2023-01-03T04:30:17Z","2023-05-22T04:35:25Z"]]}},"license":"CC-BY-NC-4.0","renders":{"visual":{"title":"Visual Image","assets":["visual"],"asset_bidx":["visual|1,2,3"],"minmax_zoom":[8,22],"tilematrixsets":{"WebMercatorQuad":[8,22]}},"color":{"title":"Colored Image","assets":["visual"],"asset_bidx":["visual|1"],"colormap":{"1":[0,0,0,255],"1000":[255,255,255,255]}},"visualr":{"title":"Rescaled Image","assets":["visual"],"asset_bidx":["visual|1"],"rescale":[[0,100]]}},"description":"Maxar OpenData | Cyclone Mocha, a category five cyclone with 130 mph winds and torrential rain, hit parts of Myanmar and Bangladesh, forcing mass evacuations ahead of the storm. The cyclone, one of the most powerful to hit the region in the last decade, made landfall on Sunday, May 14, 2023, near Sittwe in Myanmar's Rakhine state. Rain and a storm surge caused widespread flooding in low-lying areas. The United National Office Coordination of Humanitarian Affairs stated that there had been extensive damage among already vulnerable communities and that communications with the affected areas have been difficult.","item_assets":{"visual":{"type":"image/tiff; application=geotiff; profile=cloud-optimized","roles":["visual"],"title":"Visual Image"},"data-mask":{"type":"application/geopackage+sqlite3","roles":["data-mask"],"title":"Data Mask"},"ms_analytic":{"type":"image/tiff; application=geotiff; profile=cloud-optimized","roles":["data"],"title":"Multispectral Image"},"pan_analytic":{"type":"image/tiff; application=geotiff; profile=cloud-optimized","roles":["data"],"title":"Panchromatic Image"}},"stac_version":"1.0.0","stac_extensions":["https://stac-extensions.github.io/item-assets/v1.0.0/schema.json","https://stac-extensions.github.io/render/v1.0.0/schema.json"]}
Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1 @@
1-
{
2-
"id": "noaa-emergency-response",
3-
"type": "Collection",
4-
"title": "NOAA Emergency Response Imagery",
5-
"description": "NOAA Emergency Response Imagery hosted on AWS Public Dataset.",
6-
"stac_version": "1.0.0",
7-
"license": "public-domain",
8-
"links": [],
9-
"extent": {
10-
"spatial": {
11-
"bbox": [
12-
[
13-
-180,
14-
-90,
15-
180,
16-
90
17-
]
18-
]
19-
},
20-
"temporal": {
21-
"interval": [
22-
[
23-
"2005-01-01T00:00:00Z",
24-
null
25-
]
26-
]
27-
}
28-
},
29-
"stac_extensions": []
30-
}
1+
{"id":"noaa-emergency-response","type":"Collection","title":"NOAA Emergency Response Imagery","description":"NOAA Emergency Response Imagery hosted on AWS Public Dataset.","stac_version":"1.0.0","license":"public-domain","links":[],"extent":{"spatial":{"bbox":[[-180,-90,180,90]]},"temporal":{"interval":[["2005-01-01T00:00:00Z",null]]}},"stac_extensions":[]}

titiler/pgstac/dependencies.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,17 @@ def get_collection_id(pool: ConnectionPool, collection_id: str) -> str: # noqa:
103103

104104
# TODO: adapt Mosaic Backend to accept Search object directly
105105
# we technically don't need to register the search request for /collections
106-
cursor.execute("SELECT pgstac.readonly()")
107-
if cursor.fetchone()["readonly"]:
108-
raise ReadOnlyPgSTACError(
109-
"PgSTAC instance is set to `read-only`, cannot register search query."
110-
)
106+
try:
107+
cursor.execute("SELECT pgstac.readonly()")
108+
if cursor.fetchone()["readonly"]:
109+
raise ReadOnlyPgSTACError(
110+
"PgSTAC instance is set to `read-only`, cannot register search query."
111+
)
112+
113+
# before pgstac 0.8.2, the read-only mode didn't exist
114+
except pgErrors.UndefinedFunction:
115+
conn.rollback()
116+
pass
111117

112118
cursor.row_factory = class_row(model.Search)
113119
cursor.execute(

titiler/pgstac/factory.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from fastapi import Body, Depends, FastAPI, HTTPException, Path, Query
2525
from fastapi.dependencies.utils import get_dependant, request_params_to_args
2626
from geojson_pydantic import Feature, FeatureCollection
27+
from psycopg import errors as pgErrors
2728
from psycopg import sql
2829
from psycopg.rows import class_row, dict_row
2930
from pydantic import conint
@@ -976,7 +977,7 @@ def point(
976977
}
977978

978979

979-
def add_search_register_route(
980+
def add_search_register_route( # noqa: C901
980981
app: FastAPI,
981982
*,
982983
prefix: str = "",
@@ -1003,11 +1004,18 @@ def register_search(request: Request, search_query=Depends(search_dependency)):
10031004

10041005
with request.app.state.dbpool.connection() as conn:
10051006
with conn.cursor(row_factory=dict_row) as cursor:
1006-
cursor.execute("SELECT pgstac.readonly()")
1007-
if cursor.fetchone()["readonly"]:
1008-
raise ReadOnlyPgSTACError(
1009-
"PgSTAC instance is set to `read-only`, cannot register search query."
1010-
)
1007+
1008+
try:
1009+
cursor.execute("SELECT pgstac.readonly()")
1010+
if cursor.fetchone()["readonly"]:
1011+
raise ReadOnlyPgSTACError(
1012+
"PgSTAC instance is set to `read-only`, cannot register search query."
1013+
)
1014+
1015+
# before pgstac 0.8.2, the read-only mode didn't exist
1016+
except pgErrors.UndefinedFunction:
1017+
conn.rollback()
1018+
pass
10111019

10121020
cursor.row_factory = class_row(model.Search)
10131021
cursor.execute(

0 commit comments

Comments
 (0)