From 13d058006191ab3cd03b9f4b0537c4b62d2aa71e Mon Sep 17 00:00:00 2001 From: Hunor Csomortáni Date: Jul 16 2018 15:06:39 +0000 Subject: Enable loading configs from file when testing In conftest.py, when the application to be used for testing was created, create_app() was called with the config_object waiverdb.config.TestConfig. The above allowed no custumization of the test configration by using a configuration file. At the same time, load_config was providing a way to tell, that the current environment is a test environment. In order to be able to use other databases than a local one when running the unit tests, this change sets the 'TEST' env var to 'true' and calls create_app() without a parameter, so that configuration files are considered. Setting the silent argument to True when calling from_pyfile() allows ignoring missing cofig files when the environment is DEV or TEST. Signed-off-by: Hunor Csomortáni --- diff --git a/tests/conftest.py b/tests/conftest.py index 69753f9..e2ba999 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,5 +1,6 @@ # SPDX-License-Identifier: GPL-2.0+ +import os from copy import copy import pytest from sqlalchemy import create_engine @@ -8,7 +9,8 @@ from waiverdb.app import create_app @pytest.fixture(scope='session') def app(request): - app = create_app('waiverdb.config.TestingConfig') + os.environ['TEST'] = 'true' + app = create_app() # Establish an application context before running the tests. ctx = app.app_context() ctx.push() diff --git a/waiverdb/app.py b/waiverdb/app.py index 736f18c..c708fac 100644 --- a/waiverdb/app.py +++ b/waiverdb/app.py @@ -26,15 +26,18 @@ def load_config(app): if os.getenv('DEV') == 'true': default_config_obj = 'waiverdb.config.DevelopmentConfig' default_config_file = os.getcwd() + '/conf/settings.py' + silent = True elif os.getenv('TEST') == 'true': default_config_obj = 'waiverdb.config.TestingConfig' default_config_file = os.getcwd() + '/conf/settings.py' + silent = True else: default_config_obj = 'waiverdb.config.ProductionConfig' default_config_file = '/etc/waiverdb/settings.py' + silent = False app.config.from_object(default_config_obj) config_file = os.environ.get('WAIVERDB_CONFIG', default_config_file) - app.config.from_pyfile(config_file) + app.config.from_pyfile(config_file, silent=silent) if os.environ.get('SECRET_KEY'): app.config['SECRET_KEY'] = os.environ['SECRET_KEY']