repair test config

This commit is contained in:
stvnrlly
2023-11-13 11:35:24 -05:00
parent e2f27cc940
commit a03dbe3c2d

View File

@@ -2,6 +2,7 @@ import os
from contextlib import contextmanager from contextlib import contextmanager
import pytest import pytest
import sqlalchemy
from alembic.command import upgrade from alembic.command import upgrade
from alembic.config import Config from alembic.config import Config
from flask import Flask from flask import Flask
@@ -11,8 +12,26 @@ from app.dao.provider_details_dao import get_provider_details_by_identifier
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def notify_app(): def notify_app(worker_id):
app = Flask("test") app = Flask("test")
# Override the SQLALCHEMY_DATABASE_URI config before the app is
# initialized to account for Flask-SQLAlchemy 3.0.x changes.
# What is ultimately happening is the create_engine call made with
# SQLAlchemy itself is now only happening at the time of calling
# init_app with Flask instead of at the time it is first accessed,
# which the _notify_db fixture method was relying on.
# See the following for more information:
# https://github.com/pallets-eco/flask-sqlalchemy/pull/1087
# https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/api/#module-flask_sqlalchemy
app.config["SQLALCHEMY_DATABASE_URI"] = "{}_{}".format(
os.getenv("SQLALCHEMY_DATABASE_TEST_URI", "").replace(
"postgres://", "postgresql://"
),
worker_id,
)
create_app(app) create_app(app)
return app return app
@@ -44,18 +63,50 @@ def client(notify_api):
yield client yield client
def create_test_db(database_uri):
# get the
db_uri_parts = database_uri.split("/")
postgres_db_uri = "/".join(db_uri_parts[:-1] + ["postgres"])
postgres_db = sqlalchemy.create_engine(
postgres_db_uri,
echo=False,
isolation_level="AUTOCOMMIT",
client_encoding="utf8",
)
try:
result = postgres_db.execute(
sqlalchemy.sql.text("CREATE DATABASE {}".format(db_uri_parts[-1]))
)
result.close()
except sqlalchemy.exc.ProgrammingError:
# database "test_notification_api_master" already exists
pass
finally:
postgres_db.dispose()
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def _notify_db(notify_api): def _notify_db(notify_api, worker_id):
""" """
Manages the connection to the database. Generally this shouldn't be used, instead you should use the Manages the connection to the database. Generally this shouldn't be used, instead you should use the
`notify_db_session` fixture which also cleans up any data you've got left over after your test run. `notify_db_session` fixture which also cleans up any data you've got left over after your test run.
""" """
# Create a database for this worker thread; note that we still have
# to reset it here to point to the correct database.
from flask import current_app
current_app.config["SQLALCHEMY_DATABASE_URI"] += "_{}".format(worker_id)
create_test_db(current_app.config["SQLALCHEMY_DATABASE_URI"])
with notify_api.app_context() as app_context: with notify_api.app_context() as app_context:
db = app_context.app.extensions["sqlalchemy"] db = app_context.app.extensions["sqlalchemy"]
assert ( assert (
"test_notification_api" in db.engine.url.database "test_notification_api" in db.engine.url.database
), "dont run tests against main db" ), "dont run tests against main db"
db.engine.url = current_app.config["SQLALCHEMY_DATABASE_URI"]
BASE_DIR = os.path.dirname(os.path.dirname(__file__)) BASE_DIR = os.path.dirname(os.path.dirname(__file__))
ALEMBIC_CONFIG = os.path.join(BASE_DIR, "migrations") ALEMBIC_CONFIG = os.path.join(BASE_DIR, "migrations")
config = Config(ALEMBIC_CONFIG + "/alembic.ini") config = Config(ALEMBIC_CONFIG + "/alembic.ini")