set conftest back to main for another pr

This commit is contained in:
stvnrlly
2023-11-14 09:27:59 -05:00
parent 6913aeb238
commit d5cd8f1c35

View File

@@ -2,36 +2,17 @@ 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
from app import create_app, db from app import create_app
from app.dao.provider_details_dao import get_provider_details_by_identifier from app.dao.provider_details_dao import get_provider_details_by_identifier
@pytest.fixture(scope="session") @pytest.fixture(scope="session")
def notify_app(worker_id): def notify_app():
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
@@ -63,45 +44,14 @@ 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, worker_id): def _notify_db(notify_api):
""" """
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"
@@ -110,6 +60,9 @@ def _notify_db(notify_api, worker_id):
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")
config.set_main_option("script_location", ALEMBIC_CONFIG) config.set_main_option("script_location", ALEMBIC_CONFIG)
config.set_main_option(
"sqlalchemy.url", app_context.app.config["SQLALCHEMY_DATABASE_URI"]
)
# Run migrations on the test database. # Run migrations on the test database.
upgrade(config, "head") upgrade(config, "head")
@@ -117,7 +70,7 @@ def _notify_db(notify_api, worker_id):
yield db yield db
db.session.remove() db.session.remove()
db.get_engine(notify_api).dispose() db.engine.dispose()
@pytest.fixture(scope="function") @pytest.fixture(scope="function")