diff --git a/tests/conftest.py b/tests/conftest.py index c177e48..4b58541 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -59,41 +59,53 @@ def database(postgresql_proc): password="a2Vw:yk=)CdSis[fek]tW=/o", ) as jan: connection = f"postgresql://{jan.user}:{quote(jan.password)}@{jan.host}:{jan.port}/{jan.dbname}" - # make sure the DB is set to use UTC with psycopg.connect(connection) as conn: with conn.cursor() as cur: cur.execute(f"ALTER DATABASE {jan.dbname} SET TIMEZONE='UTC';") - print("Running to PgSTAC migration...") - with PgstacDB(dsn=connection) as db: - migrator = Migrate(db) - version = migrator.run_migration() - assert version - print(f"PgSTAC version: {version}") - - print("Load items and collection into PgSTAC") - loader = Loader(db=db) - loader.load_collections(collection) - loader.load_collections(collection_maxar) - loader.load_items(items) - - # Make sure we have 1 collection and 163 items in pgstac - with psycopg.connect(connection) as conn: - with conn.cursor() as cur: - cur.execute("SELECT COUNT(*) FROM pgstac.collections") - val = cur.fetchone()[0] - assert val == 2 + yield jan - cur.execute("SELECT COUNT(*) FROM pgstac.items") - val = cur.fetchone()[0] - assert val == 163 - yield jan +@pytest.fixture(scope="session") +def pgstac(database): + """Create PgSTAC fixture.""" + connection = f"postgresql://{database.user}:{quote(database.password)}@{database.host}:{database.port}/{database.dbname}" + + # Clear PgSTAC + with psycopg.connect(connection) as conn: + with conn.cursor() as cur: + cur.execute("DROP SCHEMA IF EXISTS pgstac CASCADE;") + + print("Running to PgSTAC migration...") + with PgstacDB(dsn=connection) as db: + migrator = Migrate(db) + version = migrator.run_migration() + assert version + print(f"PgSTAC version: {version}") + + print("Load items and collection into PgSTAC") + loader = Loader(db=db) + loader.load_collections(collection) + loader.load_collections(collection_maxar) + loader.load_items(items) + + # Make sure we have 1 collection and 163 items in pgstac + with psycopg.connect(connection) as conn: + with conn.cursor() as cur: + cur.execute("SELECT COUNT(*) FROM pgstac.collections") + val = cur.fetchone()[0] + assert val == 2 + + cur.execute("SELECT COUNT(*) FROM pgstac.items") + val = cur.fetchone()[0] + assert val == 163 + + yield connection @pytest.fixture(autouse=True) -def app(database, monkeypatch): +def app(pgstac, monkeypatch): """Create app with connection to the pytest database.""" monkeypatch.setenv("AWS_ACCESS_KEY_ID", "jqt") monkeypatch.setenv("AWS_SECRET_ACCESS_KEY", "rde") @@ -104,10 +116,7 @@ def app(database, monkeypatch): monkeypatch.setenv("TITILER_PGSTAC_API_DEBUG", "TRUE") monkeypatch.setenv("TITILER_PGSTAC_API_ENABLE_ASSETS_ENDPOINTS", "TRUE") monkeypatch.setenv("TITILER_PGSTAC_API_ENABLE_EXTERNAL_DATASET_ENDPOINTS", "TRUE") - monkeypatch.setenv( - "DATABASE_URL", - f"postgresql://{database.user}:{quote(database.password)}@{database.host}:{database.port}/{database.dbname}", - ) + monkeypatch.setenv("DATABASE_URL", pgstac) from titiler.pgstac.main import app diff --git a/tests/test_items.py b/tests/test_items.py index fae82e3..501be74 100644 --- a/tests/test_items.py +++ b/tests/test_items.py @@ -4,13 +4,13 @@ import pystac -from titiler.pgstac.dependencies import get_stac_item - from .conftest import mock_rasterio_open def test_get_stac_item(app): """test get_stac_item.""" + from titiler.pgstac.dependencies import get_stac_item + item = get_stac_item( app.app.state.dbpool, "noaa-emergency-response", "20200307aC0853900w361030" ) diff --git a/tests/test_readonly.py b/tests/test_readonly.py index 1df04b2..36e85dd 100644 --- a/tests/test_readonly.py +++ b/tests/test_readonly.py @@ -11,46 +11,17 @@ from pypgstac.db import PgstacDB from pypgstac.load import Loader from pypgstac.migrate import Migrate -from pytest_postgresql.janitor import DatabaseJanitor from starlette.requests import Request from starlette.testclient import TestClient -from titiler.pgstac.db import close_db_connection, connect_to_db -from titiler.pgstac.dependencies import CollectionIdParams, SearchIdParams from titiler.pgstac.errors import ReadOnlyPgSTACError -from titiler.pgstac.extensions import searchInfoExtension -from titiler.pgstac.factory import ( - MosaicTilerFactory, - add_search_list_route, - add_search_register_route, -) from titiler.pgstac.model import Metadata, PgSTACSearch, Search -from titiler.pgstac.settings import PostgresSettings DATA_DIR = os.path.join(os.path.dirname(__file__), "fixtures") collection = os.path.join(DATA_DIR, "noaa-emergency-response.json") items = os.path.join(DATA_DIR, "noaa-eri-nashville2020.json") -@pytest.fixture(scope="session") -def database_ro(postgresql_proc): - """Create Database fixture.""" - with DatabaseJanitor( - user=postgresql_proc.user, - host=postgresql_proc.host, - port=postgresql_proc.port, - dbname="test_db", - version=postgresql_proc.version, - password="a2Vw:yk=)CdSis[fek]tW=/o", - ) as jan: - connection = f"postgresql://{jan.user}:{quote(jan.password)}@{jan.host}:{jan.port}/{jan.dbname}" - with psycopg.connect(connection) as conn: - with conn.cursor() as cur: - cur.execute(f"ALTER DATABASE {jan.dbname} SET TIMEZONE='UTC';") - - yield jan - - @pytest.fixture( params=[ True, @@ -58,11 +29,11 @@ def database_ro(postgresql_proc): ], scope="session", ) -def pgstac_ro(request, database_ro): +def pgstac_ro(request, database): """Create PgSTAC fixture.""" read_only = request.param - connection = f"postgresql://{database_ro.user}:{quote(database_ro.password)}@{database_ro.host}:{database_ro.port}/{database_ro.dbname}" + connection = f"postgresql://{database.user}:{quote(database.password)}@{database.host}:{database.port}/{database.dbname}" with psycopg.connect(connection) as conn: with conn.cursor() as cur: cur.execute("DROP SCHEMA IF EXISTS pgstac CASCADE;") @@ -105,10 +76,22 @@ def pgstac_ro(request, database_ro): @pytest.fixture(autouse=True) -def app_ro(pgstac_ro): +def app_ro(pgstac_ro, monkeypatch): """create app fixture.""" + monkeypatch.setenv("TITILER_PGSTAC_CACHE_DISABLE", "TRUE") dsn, ro = pgstac_ro + + from titiler.pgstac.db import close_db_connection, connect_to_db + from titiler.pgstac.dependencies import CollectionIdParams, SearchIdParams + from titiler.pgstac.extensions import searchInfoExtension + from titiler.pgstac.factory import ( + MosaicTilerFactory, + add_search_list_route, + add_search_register_route, + ) + from titiler.pgstac.settings import PostgresSettings + postgres_settings = PostgresSettings(database_url=dsn) @asynccontextmanager