Skip to content

Commit bfc96fa

Browse files
committed
refactor async/sync fixture
1 parent dcead08 commit bfc96fa

File tree

5 files changed

+85
-90
lines changed

5 files changed

+85
-90
lines changed

tests/api/test_api.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ async def search(query: Dict[str, Any]) -> List[Item]:
699699

700700

701701
@pytest.mark.asyncio
702-
async def test_wrapped_function(load_test_data, database) -> None:
702+
async def test_wrapped_function(load_test_data, pgstac) -> None:
703703
# Ensure wrappers, e.g. Planetary Computer's rate limiting, work.
704704
# https://github.com/gadomski/planetary-computer-apis/blob/2719ccf6ead3e06de0784c39a2918d4d1811368b/pccommon/pccommon/redis.py#L205-L238
705705

@@ -738,11 +738,11 @@ async def get_collection(
738738
)
739739

740740
postgres_settings = PostgresSettings(
741-
pguser=database.user,
742-
pgpassword=database.password,
743-
pghost=database.host,
744-
pgport=database.port,
745-
pgdatabase=database.dbname,
741+
pguser=pgstac.user,
742+
pgpassword=pgstac.password,
743+
pghost=pgstac.host,
744+
pgport=pgstac.port,
745+
pgdatabase=pgstac.dbname,
746746
)
747747

748748
extensions = [
@@ -795,19 +795,19 @@ async def get_collection(
795795
@pytest.mark.asyncio
796796
@pytest.mark.parametrize("validation", [True, False])
797797
@pytest.mark.parametrize("hydrate", [True, False])
798-
async def test_no_extension(hydrate, validation, load_test_data, database) -> None:
798+
async def test_no_extension(hydrate, validation, load_test_data, pgstac) -> None:
799799
"""test PgSTAC with no extension."""
800800
settings = Settings(
801801
testing=True,
802802
use_api_hydrate=hydrate,
803803
enable_response_models=validation,
804804
)
805805
postgres_settings = PostgresSettings(
806-
pguser=database.user,
807-
pgpassword=database.password,
808-
pghost=database.host,
809-
pgport=database.port,
810-
pgdatabase=database.dbname,
806+
pguser=pgstac.user,
807+
pgpassword=pgstac.password,
808+
pghost=pgstac.host,
809+
pgport=pgstac.port,
810+
pgdatabase=pgstac.dbname,
811811
)
812812
extensions = [
813813
TransactionExtension(client=TransactionsClient(), settings=settings),

tests/api/test_links_with_root_path.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@
1010

1111

1212
@pytest.fixture(scope="function")
13-
async def app_with_root_path(database, monkeypatch):
13+
async def app_with_root_path(pgstac, monkeypatch):
1414
"""
1515
Provides the global stac_fastapi.pgstac.app.app instance, configured with a
1616
specific ROOT_PATH environment variable and connected to the test database.
1717
"""
1818

1919
monkeypatch.setenv("ROOT_PATH", ROOT_PATH)
20-
monkeypatch.setenv("PGUSER", database.user)
21-
monkeypatch.setenv("PGPASSWORD", database.password)
22-
monkeypatch.setenv("PGHOST", database.host)
23-
monkeypatch.setenv("PGPORT", str(database.port))
24-
monkeypatch.setenv("PGDATABASE", database.dbname)
20+
monkeypatch.setenv("PGUSER", pgstac.user)
21+
monkeypatch.setenv("PGPASSWORD", pgstac.password)
22+
monkeypatch.setenv("PGHOST", pgstac.host)
23+
monkeypatch.setenv("PGPORT", str(pgstac.port))
24+
monkeypatch.setenv("PGDATABASE", pgstac.dbname)
2525
monkeypatch.setenv("ENABLE_TRANSACTIONS_EXTENSIONS", "TRUE")
2626

2727
# Reload the app module to pick up the new environment variables

tests/clients/test_postgres.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -539,13 +539,13 @@ async def test_create_bulk_items_id_mismatch(
539539
# assert item.collection == coll.id
540540

541541

542-
async def test_db_setup_works_with_env_vars(api_client, database, monkeypatch):
542+
async def test_db_setup_works_with_env_vars(api_client, pgstac, monkeypatch):
543543
"""Test that the application starts successfully if the POSTGRES_* environment variables are set"""
544-
monkeypatch.setenv("PGUSER", database.user)
545-
monkeypatch.setenv("PGPASSWORD", database.password)
546-
monkeypatch.setenv("PGHOST", database.host)
547-
monkeypatch.setenv("PGPORT", str(database.port))
548-
monkeypatch.setenv("PGDATABASE", database.dbname)
544+
monkeypatch.setenv("PGUSER", pgstac.user)
545+
monkeypatch.setenv("PGPASSWORD", pgstac.password)
546+
monkeypatch.setenv("PGHOST", pgstac.host)
547+
monkeypatch.setenv("PGPORT", str(pgstac.port))
548+
monkeypatch.setenv("PGDATABASE", pgstac.dbname)
549549

550550
await connect_to_db(api_client.app)
551551
await close_db_connection(api_client.app)
@@ -573,16 +573,16 @@ async def custom_get_connection(
573573

574574
class TestDbConnect:
575575
@pytest.fixture
576-
async def app(self, api_client, database):
576+
async def app(self, api_client, pgstac):
577577
"""
578578
app fixture override to setup app with a customized db connection getter
579579
"""
580580
postgres_settings = PostgresSettings(
581-
pguser=database.user,
582-
pgpassword=database.password,
583-
pghost=database.host,
584-
pgport=database.port,
585-
pgdatabase=database.dbname,
581+
pguser=pgstac.user,
582+
pgpassword=pgstac.password,
583+
pghost=pgstac.host,
584+
pgport=pgstac.port,
585+
pgdatabase=pgstac.dbname,
586586
)
587587

588588
logger.debug("Customizing app setup")

tests/conftest.py

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from urllib.parse import quote_plus as quote
66
from urllib.parse import urljoin
77

8-
import asyncpg
8+
import psycopg
99
import pytest
1010
from fastapi import APIRouter
1111
from httpx import ASGITransport, AsyncClient
@@ -63,12 +63,6 @@ def database(postgresql_proc):
6363
version=postgresql_proc.version,
6464
password="a2Vw:yk=)CdSis[fek]tW=/o",
6565
) as jan:
66-
connection = f"postgresql://{jan.user}:{quote(jan.password)}@{jan.host}:{jan.port}/{jan.dbname}"
67-
with PgstacDB(dsn=connection) as db:
68-
migrator = Migrate(db)
69-
version = migrator.run_migration()
70-
assert version
71-
7266
yield jan
7367

7468

@@ -77,25 +71,26 @@ def database(postgresql_proc):
7771
# "0.8.6",
7872
"0.9.8",
7973
],
80-
autouse=True,
74+
# autouse=True,
8175
)
82-
async def pgstac(request, database):
76+
def pgstac(request, database):
77+
pgstac_version = request.param
78+
8379
connection = f"postgresql://{database.user}:{quote(database.password)}@{database.host}:{database.port}/{database.dbname}"
84-
yield
85-
conn = await asyncpg.connect(dsn=connection)
86-
await conn.execute(
87-
"""
88-
DROP SCHEMA IF EXISTS pgstac CASCADE;
89-
"""
90-
)
91-
await conn.close()
80+
# Clear PgSTAC
81+
with psycopg.connect(connection) as conn:
82+
with conn.cursor() as cur:
83+
cur.execute("DROP SCHEMA IF EXISTS pgstac CASCADE;")
84+
9285
with PgstacDB(dsn=connection) as db:
9386
migrator = Migrate(db)
94-
version = migrator.run_migration(toversion=request.param)
87+
version = migrator.run_migration(toversion=pgstac_version)
9588

9689
assert version == request.param
9790
logger.info(f"PGStac Migrated to {version}")
9891

92+
yield database
93+
9994

10095
# Run all the tests that use the api_client in both db hydrate and api hydrate mode
10196
@pytest.fixture(
@@ -199,13 +194,13 @@ def api_client(request):
199194

200195

201196
@pytest.fixture(scope="function")
202-
async def app(api_client, database):
197+
async def app(api_client, pgstac):
203198
postgres_settings = PostgresSettings(
204-
pguser=database.user,
205-
pgpassword=database.password,
206-
pghost=database.host,
207-
pgport=database.port,
208-
pgdatabase=database.dbname,
199+
pguser=pgstac.user,
200+
pgpassword=pgstac.password,
201+
pghost=pgstac.host,
202+
pgport=pgstac.port,
203+
pgdatabase=pgstac.dbname,
209204
)
210205
logger.info("Creating app Fixture")
211206
app = api_client.app
@@ -294,7 +289,7 @@ async def load_test2_item(app_client, load_test_data, load_test2_collection):
294289

295290

296291
@pytest.fixture(scope="function")
297-
async def app_no_ext(database):
292+
async def app_no_ext(pgstac):
298293
"""Default stac-fastapi-pgstac application without only the transaction extensions."""
299294
api_settings = Settings(testing=True)
300295
api_client_no_ext = StacApi(
@@ -307,11 +302,11 @@ async def app_no_ext(database):
307302
)
308303

309304
postgres_settings = PostgresSettings(
310-
pguser=database.user,
311-
pgpassword=database.password,
312-
pghost=database.host,
313-
pgport=database.port,
314-
pgdatabase=database.dbname,
305+
pguser=pgstac.user,
306+
pgpassword=pgstac.password,
307+
pghost=pgstac.host,
308+
pgport=pgstac.port,
309+
pgdatabase=pgstac.dbname,
315310
)
316311
logger.info("Creating app Fixture")
317312
await connect_to_db(
@@ -335,7 +330,7 @@ async def app_client_no_ext(app_no_ext):
335330

336331

337332
@pytest.fixture(scope="function")
338-
async def app_no_transaction(database):
333+
async def app_no_transaction(pgstac):
339334
"""Default stac-fastapi-pgstac application without any extensions."""
340335
api_settings = Settings(testing=True)
341336
api = StacApi(
@@ -346,11 +341,11 @@ async def app_no_transaction(database):
346341
)
347342

348343
postgres_settings = PostgresSettings(
349-
pguser=database.user,
350-
pgpassword=database.password,
351-
pghost=database.host,
352-
pgport=database.port,
353-
pgdatabase=database.dbname,
344+
pguser=pgstac.user,
345+
pgpassword=pgstac.password,
346+
pghost=pgstac.host,
347+
pgport=pgstac.port,
348+
pgdatabase=pgstac.dbname,
354349
)
355350
logger.info("Creating app Fixture")
356351
await connect_to_db(
@@ -374,13 +369,13 @@ async def app_client_no_transaction(app_no_transaction):
374369

375370

376371
@pytest.fixture(scope="function")
377-
async def default_app(database, monkeypatch):
372+
async def default_app(pgstac, monkeypatch):
378373
"""Test default stac-fastapi-pgstac application."""
379-
monkeypatch.setenv("PGUSER", database.user)
380-
monkeypatch.setenv("PGPASSWORD", database.password)
381-
monkeypatch.setenv("PGHOST", database.host)
382-
monkeypatch.setenv("PGPORT", str(database.port))
383-
monkeypatch.setenv("PGDATABASE", database.dbname)
374+
monkeypatch.setenv("PGUSER", pgstac.user)
375+
monkeypatch.setenv("PGPASSWORD", pgstac.password)
376+
monkeypatch.setenv("PGHOST", pgstac.host)
377+
monkeypatch.setenv("PGPORT", str(pgstac.port))
378+
monkeypatch.setenv("PGDATABASE", pgstac.dbname)
384379
monkeypatch.delenv("ENABLED_EXTENSIONS", raising=False)
385380

386381
monkeypatch.setenv("ENABLE_TRANSACTIONS_EXTENSIONS", "TRUE")
@@ -403,7 +398,7 @@ async def default_client(default_app):
403398

404399

405400
@pytest.fixture(scope="function")
406-
async def app_advanced_freetext(database):
401+
async def app_advanced_freetext(pgstac):
407402
"""Default stac-fastapi-pgstac application without only the transaction extensions."""
408403
api_settings = Settings(testing=True)
409404

@@ -429,11 +424,11 @@ async def app_advanced_freetext(database):
429424
)
430425

431426
postgres_settings = PostgresSettings(
432-
pguser=database.user,
433-
pgpassword=database.password,
434-
pghost=database.host,
435-
pgport=database.port,
436-
pgdatabase=database.dbname,
427+
pguser=pgstac.user,
428+
pgpassword=pgstac.password,
429+
pghost=pgstac.host,
430+
pgport=pgstac.port,
431+
pgdatabase=pgstac.dbname,
437432
)
438433
logger.info("Creating app Fixture")
439434
await connect_to_db(
@@ -457,7 +452,7 @@ async def app_client_advanced_freetext(app_advanced_freetext):
457452

458453

459454
@pytest.fixture(scope="function")
460-
async def app_transaction_validation_ext(database):
455+
async def app_transaction_validation_ext(pgstac):
461456
"""Default stac-fastapi-pgstac application with extension validation in transaction."""
462457
api_settings = Settings(testing=True, validate_extensions=True)
463458
api = StacApi(
@@ -473,11 +468,11 @@ async def app_transaction_validation_ext(database):
473468
)
474469

475470
postgres_settings = PostgresSettings(
476-
pguser=database.user,
477-
pgpassword=database.password,
478-
pghost=database.host,
479-
pgport=database.port,
480-
pgdatabase=database.dbname,
471+
pguser=pgstac.user,
472+
pgpassword=pgstac.password,
473+
pghost=pgstac.host,
474+
pgport=pgstac.port,
475+
pgdatabase=pgstac.dbname,
481476
)
482477
logger.info("Creating app Fixture")
483478
await connect_to_db(

tests/resources/test_mgmt.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async def test_health(app_client):
3333
assert body["pgstac"]["pgstac_version"]
3434

3535

36-
async def test_health_503(database):
36+
async def test_health_503(pgstac):
3737
"""Test health endpoint error."""
3838

3939
# No lifespan so no `get_connection` is application state
@@ -58,11 +58,11 @@ async def test_health_503(database):
5858

5959
# No lifespan so no `get_connection` is application state
6060
postgres_settings = PostgresSettings(
61-
pguser=database.user,
62-
pgpassword=database.password,
63-
pghost=database.host,
64-
pgport=database.port,
65-
pgdatabase=database.dbname,
61+
pguser=pgstac.user,
62+
pgpassword=pgstac.password,
63+
pghost=pgstac.host,
64+
pgport=pgstac.port,
65+
pgdatabase=pgstac.dbname,
6666
)
6767
# Create connection pool but close it just after
6868
await connect_to_db(api.app, postgres_settings=postgres_settings)

0 commit comments

Comments
 (0)