Skip to content

Commit

Permalink
Merge pull request #202 from ral-facilities/bugfix/fix-teardown-icat-…
Browse files Browse the repository at this point in the history
…create-tests-#201

Add teardown for POST entity endpoint tests
  • Loading branch information
MRichards99 authored Jan 27, 2021
2 parents 89b9d19 + 71a65b3 commit 460b680
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
35 changes: 35 additions & 0 deletions test/icat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
create_api_endpoints,
create_app_infrastructure,
)
from test.icat.endpoints.test_create_icat import TestICATCreateData
from test.icat.test_query import prepare_icat_data_for_assertion


Expand Down Expand Up @@ -154,3 +155,37 @@ def final_facilitycycle_id(flask_test_app_icat, valid_icat_credentials_header):
headers=valid_icat_credentials_header,
)
return final_facilitycycle_result.json["id"]


@pytest.fixture()
def remove_test_created_investigation_data(
flask_test_app_icat, valid_icat_credentials_header,
):
"""
This is used to delete the data created inside `test_valid` test functions in
TestICATCreateData
This is done by fetching the data which has been created in
those functions (by using the investigation name prefix, as defined in the test
class), extracting the IDs from the results, and iterating over those to perform
DELETE by ID requests
"""

yield

created_test_data = flask_test_app_icat.get(
'/investigations?where={"name":{"like":'
f'"{TestICATCreateData.investigation_name_prefix}"'
"}}",
headers=valid_icat_credentials_header,
)

investigation_ids = []
for investigation in created_test_data.json:
investigation_ids.append(investigation["id"])

for investigation_id in investigation_ids:
flask_test_app_icat.delete(
f"/investigations/{investigation_id}",
headers=valid_icat_credentials_header,
)
29 changes: 9 additions & 20 deletions test/icat/endpoints/test_create_icat.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import pytest

from test.icat.test_query import prepare_icat_data_for_assertion


class TestICATCreateData:
investigation_name_prefix = "Test Data for API Testing, Data Creation"

@pytest.mark.usefixtures("remove_test_created_investigation_data")
def test_valid_create_data(
self, flask_test_app_icat, valid_icat_credentials_header,
):
create_investigations_json = [
{
"name": f"Test Data for API Testing, Data Creation {i}",
"name": f"{self.investigation_name_prefix} {i}",
"title": "Test data for the Python ICAT Backend on DataGateway API",
"summary": "Test data for DataGateway API testing",
"releaseDate": "2020-03-03 08:00:08+00:00",
Expand All @@ -27,34 +32,24 @@ def test_valid_create_data(
json=create_investigations_json,
)

test_data_ids = []
for investigation_request, investigation_response in zip(
create_investigations_json, test_response.json,
):
for investigation_request in create_investigations_json:
investigation_request.pop("facility")
investigation_request.pop("type")
test_data_ids.append(investigation_response["id"])

response_json = prepare_icat_data_for_assertion(
test_response.json, remove_id=True,
)

assert create_investigations_json == response_json

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

@pytest.mark.usefixtures("remove_test_created_investigation_data")
def test_valid_boundary_create_data(
self, flask_test_app_icat, valid_icat_credentials_header,
):
"""Create a single investigation, as opposed to multiple"""

create_investigation_json = {
"name": "Test Data for API Testing, Data Creation 0",
"name": f"{self.investigation_name_prefix} 0",
"title": "Test data for the Python ICAT Backend on the API",
"summary": "Test data for DataGateway API testing",
"releaseDate": "2020-03-03 08:00:08+00:00",
Expand All @@ -74,19 +69,13 @@ def test_valid_boundary_create_data(

create_investigation_json.pop("facility")
create_investigation_json.pop("type")
created_test_data_id = test_response.json[0]["id"]

response_json = prepare_icat_data_for_assertion(
test_response.json, remove_id=True,
)

assert [create_investigation_json] == response_json

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

def test_invalid_create_data(
self, flask_test_app_icat, valid_icat_credentials_header,
):
Expand Down

0 comments on commit 460b680

Please sign in to comment.