Skip to content

Commit

Permalink
#1436 Run unit tests with one test database for all Pytest threads.
Browse files Browse the repository at this point in the history
  • Loading branch information
kalbfled committed Nov 2, 2023
1 parent a938fc0 commit e22d65e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
8 changes: 3 additions & 5 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,18 @@
authenticated_service = LocalProxy(lambda: g.authenticated_service)


def create_app(application, worker_id=None):
def create_app(application):
from app.config import configs

notify_environment = os.getenv("NOTIFY_ENVIRONMENT", "development")

application.config.from_object(configs[notify_environment])
if notify_environment == "test":
assert worker_id is not None
application.config["SQLALCHEMY_DATABASE_URI"] += f"_{worker_id}"
# set read-db to be the same as write/default instance for testing
application.config["SQLALCHEMY_BINDS"] = {"read-db": application.config["SQLALCHEMY_DATABASE_URI"]}
assert "test_notification_api" in application.config["SQLALCHEMY_DATABASE_URI"], \
assert application.config["SQLALCHEMY_DATABASE_URI"].endswith("test_notification_api"), \
"Don't run tests against the main database."
assert "test_notification_api" in application.config["SQLALCHEMY_BINDS"]["read-db"], \
assert application.config["SQLALCHEMY_BINDS"]["read-db"].endswith("test_notification_api"), \
"Don't run tests against the main database."

application.config["NOTIFY_APP_NAME"] = application.name
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


@pytest.fixture(scope='session')
def notify_api(worker_id):
def notify_api():
"""
Initialize a Flask application with the Flask-SQLAlchemy extension.
Expand All @@ -18,7 +18,7 @@ def notify_api(worker_id):
"""

app = Flask('test')
create_app(app, worker_id)
create_app(app)

# deattach server-error error handlers - error_handler_spec looks like:
# {'blueprint_name': {
Expand Down Expand Up @@ -81,7 +81,7 @@ def notify_db(notify_api):
# Import current_app only after the app has been created and initialized via the notify_api fixture.
from flask import current_app

# Create a database for this worker thread.
# Create the database.
create_test_db(current_app.config['SQLALCHEMY_DATABASE_URI'])

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
Expand Down

0 comments on commit e22d65e

Please sign in to comment.