Skip to content

Commit

Permalink
#150: Allow backend type to be set when a flask test fixture runs
Browse files Browse the repository at this point in the history
- This will allow tests to run on a backend set by the fixture instead of relying on the contents of config.json. This means both backends can be tested in one test session and the contents of the config file doesn't matter
  • Loading branch information
MRichards99 committed Dec 3, 2020
1 parent 7a6a8ef commit 70dc393
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
7 changes: 7 additions & 0 deletions datagateway_api/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import requests

# from datagateway_api.src.main import app

log = logging.getLogger()

Expand All @@ -22,6 +23,12 @@ def get_backend_type(self):
except KeyError:
sys.exit("Missing config value, backend")

def set_backend_type(self, backend_type):
"""
TODO - Explain the reason behind the setter
"""
self.config["backend"] = backend_type

def get_db_url(self):
try:
return self.config["DB_URL"]
Expand Down
1 change: 1 addition & 0 deletions datagateway_api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def handle_error(e):
def create_api_endpoints(app, api, spec):
try:
backend_type = app.config["TEST_BACKEND"]
config.set_backend_type(backend_type)
print(f"test backend: {backend_type}")
except KeyError:
backend_type = config.get_backend_type()
Expand Down
17 changes: 9 additions & 8 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,19 @@ def multiple_investigation_test_data(icat_client):
icat_client.delete(investigation)


@pytest.fixture()
@pytest.fixture(scope="package")
def flask_test_app():
my_app = Flask(__name__)
my_app.config["TESTING"] = True
my_app.config["TEST_BACKEND"] = "python_icat"
# my_app = Flask(__name__)
app.config["TESTING"] = True
app.config["TEST_BACKEND"] = "python_icat"

api, spec = create_app_infrastructure(my_app)
create_api_endpoints(my_app, api, spec)
api, spec = create_app_infrastructure(app)
create_api_endpoints(app, api, spec)

yield my_app.test_client()
yield app.test_client()

# app.url_map._rules.clear()
# app.url_map._rules.clear()
# app.url_map._rules_by_endpoint.clear()


@pytest.fixture()
Expand Down

0 comments on commit 70dc393

Please sign in to comment.