Skip to content

Commit ad080fb

Browse files
committed
fix a few tests
1 parent 2b9d2ec commit ad080fb

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

tests/clients/test_postgres.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -525,17 +525,6 @@ async def test_create_bulk_items_id_mismatch(
525525
# assert item.collection == coll.id
526526

527527

528-
@asynccontextmanager
529-
async def custom_get_connection(
530-
request: Request,
531-
readwrite: Literal["r", "w"],
532-
):
533-
"""An example of customizing the connection getter"""
534-
async with get_connection(request, readwrite) as conn:
535-
await conn.execute("SELECT set_config('api.test', 'added-config', false)")
536-
yield conn
537-
538-
539528
async def test_db_setup_works_with_env_vars(api_client, database, monkeypatch):
540529
"""Test that the application starts successfully if the POSTGRES_* environment variables are set"""
541530
monkeypatch.setenv("POSTGRES_USER", database.user)
@@ -546,12 +535,27 @@ async def test_db_setup_works_with_env_vars(api_client, database, monkeypatch):
546535
monkeypatch.setenv("POSTGRES_DBNAME", database.dbname)
547536

548537
await connect_to_db(api_client.app)
538+
await close_db_connection(api_client.app)
549539

550540

551541
async def test_db_setup_fails_without_env_vars(api_client):
552542
"""Test that the application fails to start if database environment variables are not set."""
553-
with pytest.raises(ValidationError):
543+
try:
554544
await connect_to_db(api_client.app)
545+
except ValidationError:
546+
await close_db_connection(api_client.app)
547+
pytest.raises(ValidationError)
548+
549+
550+
@asynccontextmanager
551+
async def custom_get_connection(
552+
request: Request,
553+
readwrite: Literal["r", "w"],
554+
):
555+
"""An example of customizing the connection getter"""
556+
async with get_connection(request, readwrite) as conn:
557+
await conn.execute("SELECT set_config('api.test', 'added-config', false)")
558+
yield conn
555559

556560

557561
class TestDbConnect:

tests/conftest.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ async def pgstac(database):
117117
],
118118
scope="session",
119119
)
120-
def api_client(request, database):
120+
def api_client(request):
121121
hydrate, prefix, response_model = request.param
122122
api_settings = Settings(
123123
enable_response_models=response_model,
@@ -298,14 +298,8 @@ async def load_test2_item(app_client, load_test_data, load_test2_collection):
298298
@pytest.fixture(
299299
scope="session",
300300
)
301-
def api_client_no_ext(database):
301+
def api_client_no_ext():
302302
api_settings = Settings(
303-
postgres_user=database.user,
304-
postgres_pass=database.password,
305-
postgres_host_reader=database.host,
306-
postgres_host_writer=database.host,
307-
postgres_port=database.port,
308-
postgres_dbname=database.dbname,
309303
testing=True,
310304
)
311305
return StacApi(
@@ -318,11 +312,19 @@ def api_client_no_ext(database):
318312

319313

320314
@pytest.fixture(scope="function")
321-
async def app_no_ext(api_client_no_ext):
315+
async def app_no_ext(api_client_no_ext, database):
316+
postgres_settings = PostgresSettings(
317+
postgres_user=database.user,
318+
postgres_pass=database.password,
319+
postgres_host_reader=database.host,
320+
postgres_host_writer=database.host,
321+
postgres_port=database.port,
322+
postgres_dbname=database.dbname,
323+
)
322324
logger.info("Creating app Fixture")
323325
time.time()
324326
app = api_client_no_ext.app
325-
await connect_to_db(app)
327+
await connect_to_db(app, postgres_settings=postgres_settings)
326328

327329
yield app
328330

0 commit comments

Comments
 (0)