Skip to content

Commit

Permalink
#150: Add separate flask app fixtures for each backend
Browse files Browse the repository at this point in the history
- The DB app will be used when I rewrite the old tests into pytest, tests which will remain for testing the DB backend
  • Loading branch information
MRichards99 committed Dec 3, 2020
1 parent 70dc393 commit 3890c55
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 108 deletions.
23 changes: 11 additions & 12 deletions datagateway_api/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@

app = Flask(__name__)

swaggerui_blueprint = get_swaggerui_blueprint(
"", "/openapi.json", config={"app_name": "DataGateway API OpenAPI Spec"},
)
app.register_blueprint(swaggerui_blueprint, url_prefix="/")


def create_app_infrastructure(app):
def create_app_infrastructure(flask_app):
swaggerui_blueprint = get_swaggerui_blueprint(
"", "/openapi.json", config={"app_name": "DataGateway API OpenAPI Spec"},
)
flask_app.register_blueprint(swaggerui_blueprint, url_prefix="/")
spec = APISpec(
title="DataGateway API",
version="1.0",
Expand All @@ -52,11 +51,11 @@ def create_app_infrastructure(app):
security=[{"session_id": []}],
)

cors = CORS(app)
app.url_map.strict_slashes = False
api = Api(app)
cors = CORS(flask_app)
flask_app.url_map.strict_slashes = False
api = Api(flask_app)

app.register_error_handler(ApiError, handle_error)
flask_app.register_error_handler(ApiError, handle_error)

initialise_spec(spec)

Expand All @@ -67,9 +66,9 @@ def handle_error(e):
return str(e), e.status_code


def create_api_endpoints(app, api, spec):
def create_api_endpoints(flask_app, api, spec):
try:
backend_type = app.config["TEST_BACKEND"]
backend_type = flask_app.config["TEST_BACKEND"]
config.set_backend_type(backend_type)
print(f"test backend: {backend_type}")
except KeyError:
Expand Down
27 changes: 18 additions & 9 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,27 @@ def multiple_investigation_test_data(icat_client):


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

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

yield app.test_client()
yield icat_app.test_client()

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

@pytest.fixture(scope="package")
def flask_test_app_db():
db_app = Flask(__name__)
db_app.config["TESTING"] = True
db_app.config["TEST_BACKEND"] = "db"

api, spec = create_app_infrastructure(db_app)
create_api_endpoints(db_app, api, spec)

yield db_app.test_client()


@pytest.fixture()
Expand Down
10 changes: 6 additions & 4 deletions test/icat/endpoints/test_count_with_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

class TestCountWithFilters:
@pytest.mark.usefixtures("single_investigation_test_data")
def test_valid_count_with_filters(self, flask_test_app, valid_credentials_header):
test_response = flask_test_app.get(
def test_valid_count_with_filters(
self, flask_test_app_icat, valid_credentials_header
):
test_response = flask_test_app_icat.get(
'/investigations/count?where={"title": {"like": "Test data for the Python'
' ICAT Backend on DataGateway API"}}',
headers=valid_credentials_header,
Expand All @@ -13,9 +15,9 @@ def test_valid_count_with_filters(self, flask_test_app, valid_credentials_header
assert test_response.json == 1

def test_valid_no_results_count_with_filters(
self, flask_test_app, valid_credentials_header,
self, flask_test_app_icat, valid_credentials_header,
):
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations/count?where={"title": {"like": "This filter should cause a'
'404 for testing purposes..."}}',
headers=valid_credentials_header,
Expand Down
25 changes: 15 additions & 10 deletions test/icat/endpoints/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class TestCreateData:
def test_valid_create_data(self, flask_test_app, valid_credentials_header):
def test_valid_create_data(self, flask_test_app_icat, valid_credentials_header):
create_investigations_json = [
{
"name": "Test Data for API Testing, Data Creation 1",
Expand Down Expand Up @@ -30,7 +30,7 @@ def test_valid_create_data(self, flask_test_app, valid_credentials_header):
},
]

test_response = flask_test_app.post(
test_response = flask_test_app_icat.post(
"/investigations",
headers=valid_credentials_header,
json=create_investigations_json,
Expand All @@ -52,11 +52,13 @@ def test_valid_create_data(self, flask_test_app, valid_credentials_header):

# Delete the entities created by this test
for investigation_id in test_data_ids:
flask_test_app.delete(
flask_test_app_icat.delete(
f"/investigations/{investigation_id}", headers=valid_credentials_header,
)

def test_valid_boundary_create_data(self, flask_test_app, valid_credentials_header):
def test_valid_boundary_create_data(
self, flask_test_app_icat, valid_credentials_header
):
"""Create a single investigation, as opposed to multiple"""

create_investigation_json = {
Expand All @@ -72,7 +74,7 @@ def test_valid_boundary_create_data(self, flask_test_app, valid_credentials_head
"type": 1,
}

test_response = flask_test_app.post(
test_response = flask_test_app_icat.post(
"/investigations",
headers=valid_credentials_header,
json=create_investigation_json,
Expand All @@ -88,18 +90,18 @@ def test_valid_boundary_create_data(self, flask_test_app, valid_credentials_head

assert [create_investigation_json] == response_json

flask_test_app.delete(
flask_test_app_icat.delete(
f"/investigations/{created_test_data_id}", headers=valid_credentials_header,
)

def test_invalid_create_data(self, flask_test_app, valid_credentials_header):
def test_invalid_create_data(self, flask_test_app_icat, valid_credentials_header):
"""An investigation requires a minimum of: name, visitId, facility, type"""

invalid_request_body = {
"title": "Test Title for DataGateway API Backend testing",
}

test_response = flask_test_app.post(
test_response = flask_test_app_icat.post(
"/investigations",
headers=valid_credentials_header,
json=invalid_request_body,
Expand All @@ -108,7 +110,10 @@ def test_invalid_create_data(self, flask_test_app, valid_credentials_header):
assert test_response.status_code == 400

def test_invalid_existing_data_create(
self, flask_test_app, valid_credentials_header, single_investigation_test_data,
self,
flask_test_app_icat,
valid_credentials_header,
single_investigation_test_data,
):
"""This test targets raising ICATObjectExistsError, causing a 400"""

Expand All @@ -122,7 +127,7 @@ def test_invalid_existing_data_create(
"type": 1,
}

test_response = flask_test_app.post(
test_response = flask_test_app_icat.post(
"/investigations",
headers=valid_credentials_header,
json=existing_object_json,
Expand Down
15 changes: 10 additions & 5 deletions test/icat/endpoints/test_delete_by_id.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
class TestDeleteByID:
def test_valid_delete_with_id(
self, flask_test_app, valid_credentials_header, single_investigation_test_data,
self,
flask_test_app_icat,
valid_credentials_header,
single_investigation_test_data,
):
test_response = flask_test_app.delete(
test_response = flask_test_app_icat.delete(
f'/investigations/{single_investigation_test_data[0]["id"]}',
headers=valid_credentials_header,
)

assert test_response.status_code == 204

def test_invalid_delete_with_id(self, flask_test_app, valid_credentials_header):
def test_invalid_delete_with_id(
self, flask_test_app_icat, valid_credentials_header
):
"""Request with a non-existent ID"""

final_investigation_result = flask_test_app.get(
final_investigation_result = flask_test_app_icat.get(
'/investigations/findone?order="id DESC"', headers=valid_credentials_header,
)
test_data_id = final_investigation_result.json["id"]

# Adding 100 onto the ID to the most recent result should ensure a 404
test_response = flask_test_app.delete(
test_response = flask_test_app_icat.delete(
f"/investigations/{test_data_id + 100}", headers=valid_credentials_header,
)

Expand Down
11 changes: 7 additions & 4 deletions test/icat/endpoints/test_findone.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

class TestFindone:
def test_valid_findone_with_filters(
self, flask_test_app, valid_credentials_header, single_investigation_test_data,
self,
flask_test_app_icat,
valid_credentials_header,
single_investigation_test_data,
):
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations/findone?where={"title": {"like": "Test data for the Python'
' ICAT Backend on DataGateway API"}}',
headers=valid_credentials_header,
Expand All @@ -15,9 +18,9 @@ def test_valid_findone_with_filters(
assert response_json == single_investigation_test_data

def test_valid_no_results_findone_with_filters(
self, flask_test_app, valid_credentials_header,
self, flask_test_app_icat, valid_credentials_header,
):
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations/findone?where={"title": {"eq": "This filter should cause a'
'404 for testing purposes..."}}',
headers=valid_credentials_header,
Expand Down
15 changes: 9 additions & 6 deletions test/icat/endpoints/test_get_by_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@

class TestGetByID:
def test_valid_get_with_id(
self, flask_test_app, valid_credentials_header, single_investigation_test_data,
self,
flask_test_app_icat,
valid_credentials_header,
single_investigation_test_data,
):
# Need to identify the ID given to the test data
investigation_data = flask_test_app.get(
investigation_data = flask_test_app_icat.get(
'/investigations?where={"title": {"like": "Test data for the Python ICAT'
' Backend on DataGateway API"}}',
headers=valid_credentials_header,
)
test_data_id = investigation_data.json[0]["id"]

test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
f"/investigations/{test_data_id}", headers=valid_credentials_header,
)
# Get with ID gives a dictionary response (only ever one result from that kind
Expand All @@ -22,16 +25,16 @@ def test_valid_get_with_id(

assert response_json == single_investigation_test_data

def test_invalid_get_with_id(self, flask_test_app, valid_credentials_header):
def test_invalid_get_with_id(self, flask_test_app_icat, valid_credentials_header):
"""Request with a non-existent ID"""

final_investigation_result = flask_test_app.get(
final_investigation_result = flask_test_app_icat.get(
'/investigations/findone?order="id DESC"', headers=valid_credentials_header,
)
test_data_id = final_investigation_result.json["id"]

# Adding 100 onto the ID to the most recent result should ensure a 404
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
f"/investigations/{test_data_id + 100}", headers=valid_credentials_header,
)

Expand Down
19 changes: 11 additions & 8 deletions test/icat/endpoints/test_get_with_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

class TestGetWithFilters:
def test_valid_get_with_filters(
self, flask_test_app, valid_credentials_header, single_investigation_test_data,
self,
flask_test_app_icat,
valid_credentials_header,
single_investigation_test_data,
):
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations?where={"title": {"like": "Test data for the Python ICAT'
' Backend on DataGateway API"}}',
headers=valid_credentials_header,
Expand All @@ -17,9 +20,9 @@ def test_valid_get_with_filters(
assert response_json == single_investigation_test_data

def test_valid_no_results_get_with_filters(
self, flask_test_app, valid_credentials_header,
self, flask_test_app_icat, valid_credentials_header,
):
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations?where={"title": {"eq": "This filter should cause a 404 for'
'testing purposes..."}}',
headers=valid_credentials_header,
Expand All @@ -29,9 +32,9 @@ def test_valid_no_results_get_with_filters(

@pytest.mark.usefixtures("multiple_investigation_test_data")
def test_valid_get_with_filters_distinct(
self, flask_test_app, valid_credentials_header,
self, flask_test_app_icat, valid_credentials_header,
):
test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations?where={"title": {"like": "Test data for the Python ICAT'
' Backend on DataGateway API"}}&distinct="title"',
headers=valid_credentials_header,
Expand All @@ -49,14 +52,14 @@ def test_valid_get_with_filters_distinct(

def test_limit_skip_merge_get_with_filters(
self,
flask_test_app,
flask_test_app_icat,
valid_credentials_header,
multiple_investigation_test_data,
):
skip_value = 1
limit_value = 2

test_response = flask_test_app.get(
test_response = flask_test_app_icat.get(
'/investigations?where={"title": {"like": "Test data for the Python ICAT'
' Backend on DataGateway API"}}'
f'&skip={skip_value}&limit={limit_value}&order="id ASC"',
Expand Down
Loading

0 comments on commit 3890c55

Please sign in to comment.