3
3
import os
4
4
import warnings
5
5
from typing import Any , Dict
6
+ from urllib .parse import quote_plus as quote
6
7
7
8
import psycopg
8
9
import pytest
9
- import pytest_pgsql
10
10
import rasterio
11
11
from pypgstac .db import PgstacDB
12
12
from pypgstac .load import Loader
13
13
from pypgstac .migrate import Migrate
14
+ from pytest_postgresql .janitor import DatabaseJanitor
14
15
from rasterio .errors import NotGeoreferencedWarning
15
16
from rasterio .io import MemoryFile
16
17
from starlette .testclient import TestClient
20
21
collection_maxar = os .path .join (DATA_DIR , "maxar_BayOfBengal.json" )
21
22
items = os .path .join (DATA_DIR , "noaa-eri-nashville2020.json" )
22
23
23
- test_db = pytest_pgsql .TransactedPostgreSQLTestDB .create_fixture (
24
- "test_db" , scope = "session" , use_restore_state = False
25
- )
26
-
27
24
28
25
def parse_img (content : bytes ) -> Dict [Any , Any ]:
29
26
"""Read tile image and return metadata."""
@@ -51,42 +48,52 @@ def mock_rasterio_open(asset):
51
48
52
49
53
50
@pytest .fixture (scope = "session" )
54
- def database_url (test_db ):
55
- """
56
- Session scoped fixture to launch a postgresql database in a separate process. We use psycopg2 to ingest test data
57
- because pytest-asyncio event loop is a function scoped fixture and cannot be called within the current scope. Yields
58
- a database url which we pass to our application through a monkeypatched environment variable.
59
- """
60
- with PgstacDB (dsn = str (test_db .connection .engine .url )) as db :
51
+ def database (postgresql_proc ):
52
+ """Create Database fixture."""
53
+ with DatabaseJanitor (
54
+ user = postgresql_proc .user ,
55
+ host = postgresql_proc .host ,
56
+ port = postgresql_proc .port ,
57
+ dbname = "pgstacrw" ,
58
+ version = postgresql_proc .version ,
59
+ password = "a2Vw:yk=)CdSis[fek]tW=/o" ,
60
+ ) as jan :
61
+ connection = f"postgresql://{ jan .user } :{ quote (jan .password )} @{ jan .host } :{ jan .port } /{ jan .dbname } "
62
+
63
+ # make sure the DB is set to use UTC
64
+ with psycopg .connect (connection ) as conn :
65
+ with conn .cursor () as cur :
66
+ cur .execute (f"ALTER DATABASE { jan .dbname } SET TIMEZONE='UTC';" )
67
+
61
68
print ("Running to PgSTAC migration..." )
62
- migrator = Migrate ( db )
63
- version = migrator . run_migration ( )
64
- assert version
65
- assert test_db . has_schema ( "pgstac" )
66
- print (f"PgSTAC version: { version } " )
69
+ with PgstacDB ( dsn = connection ) as db :
70
+ migrator = Migrate ( db )
71
+ version = migrator . run_migration ()
72
+ assert version
73
+ print (f"PgSTAC version: { version } " )
67
74
68
- print ("Load items and collection into PgSTAC" )
69
- loader = Loader (db = db )
70
- loader .load_collections (collection )
71
- loader .load_collections (collection_maxar )
72
- loader .load_items (items )
75
+ print ("Load items and collection into PgSTAC" )
76
+ loader = Loader (db = db )
77
+ loader .load_collections (collection )
78
+ loader .load_collections (collection_maxar )
79
+ loader .load_items (items )
73
80
74
- # Make sure we have 1 collection and 163 items in pgstac
75
- with psycopg .connect (str ( test_db . connection . engine . url ) ) as conn :
76
- with conn .cursor () as cur :
77
- cur .execute ("SELECT COUNT(*) FROM pgstac.collections" )
78
- val = cur .fetchone ()[0 ]
79
- assert val == 2
81
+ # Make sure we have 1 collection and 163 items in pgstac
82
+ with psycopg .connect (connection ) as conn :
83
+ with conn .cursor () as cur :
84
+ cur .execute ("SELECT COUNT(*) FROM pgstac.collections" )
85
+ val = cur .fetchone ()[0 ]
86
+ assert val == 2
80
87
81
- cur .execute ("SELECT COUNT(*) FROM pgstac.items" )
82
- val = cur .fetchone ()[0 ]
83
- assert val == 163
88
+ cur .execute ("SELECT COUNT(*) FROM pgstac.items" )
89
+ val = cur .fetchone ()[0 ]
90
+ assert val == 163
84
91
85
- return test_db . connection . engine . url
92
+ yield jan
86
93
87
94
88
95
@pytest .fixture (autouse = True )
89
- def app (database_url , monkeypatch ):
96
+ def app (database , monkeypatch ):
90
97
"""Create app with connection to the pytest database."""
91
98
monkeypatch .setenv ("AWS_ACCESS_KEY_ID" , "jqt" )
92
99
monkeypatch .setenv ("AWS_SECRET_ACCESS_KEY" , "rde" )
@@ -97,8 +104,10 @@ def app(database_url, monkeypatch):
97
104
monkeypatch .setenv ("TITILER_PGSTAC_API_DEBUG" , "TRUE" )
98
105
monkeypatch .setenv ("TITILER_PGSTAC_API_ENABLE_ASSETS_ENDPOINTS" , "TRUE" )
99
106
monkeypatch .setenv ("TITILER_PGSTAC_API_ENABLE_EXTERNAL_DATASET_ENDPOINTS" , "TRUE" )
100
-
101
- monkeypatch .setenv ("DATABASE_URL" , str (database_url ))
107
+ monkeypatch .setenv (
108
+ "DATABASE_URL" ,
109
+ f"postgresql://{ database .user } :{ quote (database .password )} @{ database .host } :{ database .port } /{ database .dbname } " ,
110
+ )
102
111
103
112
from titiler .pgstac .main import app
104
113
0 commit comments