Skip to content

Commit

Permalink
refactor to avoid caching
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Sep 5, 2024
1 parent 988d5cd commit 58575cb
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 63 deletions.
67 changes: 38 additions & 29 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down
47 changes: 15 additions & 32 deletions tests/test_readonly.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,29 @@
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,
False,
],
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;")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 58575cb

Please sign in to comment.