|
1 | 1 | from pathlib import Path |
| 2 | +from typing import AsyncIterator |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +import stacrs |
4 | 6 | from fastapi.testclient import TestClient |
| 7 | +from pgstacrs import Client |
| 8 | +from pypgstac.db import PgstacDB |
| 9 | +from pypgstac.migrate import Migrate |
| 10 | +from pystac import Collection, Extent, Item |
5 | 11 | from pytest import FixtureRequest |
| 12 | +from pytest_postgresql import factories |
| 13 | +from pytest_postgresql.executor import PostgreSQLExecutor |
| 14 | +from pytest_postgresql.janitor import DatabaseJanitor |
6 | 15 |
|
7 | 16 | import tistac.app |
8 | 17 | from tistac import Settings |
9 | 18 |
|
| 19 | +naip_items = Path(__file__).parents[1] / "data" / "naip.parquet" |
10 | 20 |
|
11 | | -@pytest.fixture(params=[str(Path(__file__).parents[1] / "data" / "naip.parquet")]) |
12 | | -def client(request: FixtureRequest) -> TestClient: |
13 | | - settings = Settings(backend=request.param) |
14 | | - return TestClient(tistac.app.build(settings)) |
| 21 | +pgstac_proc = factories.postgresql_proc() |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="session") |
| 25 | +async def pgstac(pgstac_proc: PostgreSQLExecutor) -> AsyncIterator[PostgreSQLExecutor]: |
| 26 | + dsn = f"postgresql://{pgstac_proc.user}:{pgstac_proc.password}@{pgstac_proc.host}:{pgstac_proc.port}/{pgstac_proc.template_dbname}" |
| 27 | + pgstac_db = PgstacDB(dsn) |
| 28 | + Migrate(pgstac_db).run_migration() |
| 29 | + items = stacrs.read(str(naip_items))["features"][ |
| 30 | + 0:100 |
| 31 | + ] # pgstac takes too long to load 10000 items |
| 32 | + extent = Extent.from_items((Item.from_dict(d) for d in items)) |
| 33 | + collection = Collection( |
| 34 | + id="naip", description="Test NAIP collection", extent=extent |
| 35 | + ) |
| 36 | + client = await Client.open(dsn) |
| 37 | + await client.create_collection( |
| 38 | + collection.to_dict(include_self_link=False, transform_hrefs=False) |
| 39 | + ) |
| 40 | + await client.create_items(items) |
| 41 | + yield pgstac_proc |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(params=["stac-geoparquet", "pgstac"]) |
| 45 | +async def client( |
| 46 | + request: FixtureRequest, pgstac: PostgreSQLExecutor |
| 47 | +) -> AsyncIterator[TestClient]: |
| 48 | + if request.param == "stac-geoparquet": |
| 49 | + settings = Settings(backend=str(naip_items)) |
| 50 | + yield TestClient(await tistac.app.build(settings)) |
| 51 | + elif request.param == "pgstac": |
| 52 | + with DatabaseJanitor( |
| 53 | + user=pgstac.user, |
| 54 | + host=pgstac.host, |
| 55 | + port=pgstac.port, |
| 56 | + version=pgstac.version, |
| 57 | + password=pgstac.password, |
| 58 | + dbname="pypgstac_test", |
| 59 | + template_dbname=pgstac.template_dbname, |
| 60 | + ) as database_janitor: |
| 61 | + settings = Settings( |
| 62 | + backend=f"postgresql://{database_janitor.user}:{database_janitor.password}@{database_janitor.host}:{database_janitor.port}/{database_janitor.dbname}" |
| 63 | + ) |
| 64 | + yield TestClient(await tistac.app.build(settings)) |
| 65 | + else: |
| 66 | + raise Exception(f"Unknown backend type: {request.param}") |
0 commit comments