Skip to content

Improve logging of pgSTAC tests #564

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ run_pgstac = docker-compose run --rm \
-e APP_PORT=${APP_PORT} \
app-pgstac

LOG_LEVEL ?= warning

.PHONY: image
image:
docker-compose build
Expand Down Expand Up @@ -44,15 +46,15 @@ docker-shell-pgstac:

.PHONY: test-sqlalchemy
test-sqlalchemy: run-joplin-sqlalchemy
$(run_sqlalchemy) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/stac_fastapi/sqlalchemy/tests/ && pytest -vvv'
$(run_sqlalchemy) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/stac_fastapi/sqlalchemy/tests/ && pytest -vvv --log-cli-level $(LOG_LEVEL)'

.PHONY: test-pgstac
test-pgstac:
$(run_pgstac) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/stac_fastapi/pgstac/tests/ && pytest -vvv'
$(run_pgstac) /bin/bash -c 'export && ./scripts/wait-for-it.sh database:5432 && cd /app/stac_fastapi/pgstac/tests/ && pytest -vvv --log-cli-level $(LOG_LEVEL)'

.PHONY: test-api
test-api:
$(run_sqlalchemy) /bin/bash -c 'cd /app/stac_fastapi/api && pytest -svvv'
$(run_sqlalchemy) /bin/bash -c 'cd /app/stac_fastapi/api && pytest -svvv --log-cli-level $(LOG_LEVEL)'

.PHONY: run-database
run-database:
Expand Down
28 changes: 15 additions & 13 deletions stac_fastapi/pgstac/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import logging
import os
import time
from typing import Callable, Dict
Expand Down Expand Up @@ -33,6 +34,8 @@
from stac_fastapi.pgstac.transactions import BulkTransactionsClient, TransactionsClient
from stac_fastapi.pgstac.types.search import PgstacSearch

logger = logging.getLogger(__name__)

DATA_DIR = os.path.join(os.path.dirname(__file__), "data")

settings = Settings(testing=True)
Expand All @@ -46,7 +49,7 @@ def event_loop():

@pytest.fixture(scope="session")
async def pg():
print(f"Connecting to write database {settings.writer_connection_string}")
logger.info(f"Connecting to write database {settings.writer_connection_string}")
os.environ["orig_postgres_dbname"] = settings.postgres_dbname
conn = await asyncpg.connect(dsn=settings.writer_connection_string)
try:
Expand All @@ -64,21 +67,20 @@ async def pg():
"ALTER DATABASE pgstactestdb SET search_path to pgstac, public;"
)
await conn.close()
print("migrating...")
logger.info("migrating...")
os.environ["postgres_dbname"] = "pgstactestdb"
conn = await asyncpg.connect(dsn=settings.testing_connection_string)
val = await conn.fetchval("SELECT true")
print(val)
await conn.execute("SELECT true")
await conn.close()
db = PgstacDB(dsn=settings.testing_connection_string)
migrator = Migrate(db)
version = migrator.run_migration()
db.close()
print(f"PGStac Migrated to {version}")
logger.info(f"PGStac Migrated to {version}")

yield settings.testing_connection_string

print("Getting rid of test database")
logger.info("Getting rid of test database")
os.environ["postgres_dbname"] = os.environ["orig_postgres_dbname"]
conn = await asyncpg.connect(dsn=settings.writer_connection_string)
try:
Expand All @@ -94,9 +96,9 @@ async def pg():

@pytest.fixture(autouse=True)
async def pgstac(pg):
print(f"{os.environ['postgres_dbname']}")
logger.info(f"{os.environ['postgres_dbname']}")
yield
print("Truncating Data")
logger.info("Truncating Data")
conn = await asyncpg.connect(dsn=settings.testing_connection_string)
await conn.execute(
"""
Expand All @@ -107,7 +109,7 @@ async def pgstac(pg):
with PgstacDB(dsn=settings.testing_connection_string) as db:
migrator = Migrate(db)
version = migrator.run_migration()
print(f"PGStac Migrated to {version}")
logger.info(f"PGStac Migrated to {version}")


# Run all the tests that use the api_client in both db hydrate and api hydrate mode
Expand All @@ -126,7 +128,7 @@ def api_client(request, pg):
api_settings.openapi_url = prefix + api_settings.openapi_url
api_settings.docs_url = prefix + api_settings.docs_url

print(
logger.info(
"creating client with settings, hydrate: {}, router prefix: '{}'".format(
api_settings.use_api_hydrate, prefix
)
Expand Down Expand Up @@ -159,7 +161,7 @@ def api_client(request, pg):

@pytest.fixture(scope="function")
async def app(api_client):
print("Creating app Fixture")
logger.info("Creating app Fixture")
time.time()
app = api_client.app
await connect_to_db(app)
Expand All @@ -168,12 +170,12 @@ async def app(api_client):

await close_db_connection(app)

print("Closed Pools.")
logger.info("Closed Pools.")


@pytest.fixture(scope="function")
async def app_client(app):
print("creating app_client")
logger.info("creating app_client")

base_url = "http://test"
if app.state.router_prefix != "":
Expand Down