Skip to content

Commit

Permalink
#217: Add tests for rollback functionality on update and create endp…
Browse files Browse the repository at this point in the history
…oints
  • Loading branch information
MRichards99 committed Apr 8, 2021
1 parent af49cd1 commit 63b3537
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/icat/endpoints/test_create_icat.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,39 @@ def test_invalid_existing_data_create(
)

assert test_response.status_code == 400

def test_valid_rollback_behaviour(
self, flask_test_app_icat, valid_icat_credentials_header,
):
request_body = [
{
"name": "Test Investigation DG API Testing Name Test",
"title": "My New Investigation with Title",
"visitId": "Visit ID for Testing",
"facility": 1,
"type": 1,
},
{
"name": "Invalid Investigation for testing",
"title": "My New Investigation with Title",
"visitId": "Visit ID for Testing",
"doi": "_" * 256,
"facility": 1,
"type": 1,
},
]

create_response = flask_test_app_icat.post(
"/investigations", headers=valid_icat_credentials_header, json=request_body,
)

get_response = flask_test_app_icat.get(
'/investigations?where={"title": {"eq": "'
f'{request_body[0]["title"]}'
'"}}',
headers=valid_icat_credentials_header,
)
get_response_json = prepare_icat_data_for_assertion(get_response.json)

assert create_response.status_code == 400
assert get_response_json == []
41 changes: 41 additions & 0 deletions test/icat/endpoints/test_update_multiple_icat.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,44 @@ def test_invalid_attribute_update(
)

assert test_response.status_code == 400

def test_valid_rollback_behaviour(
self,
flask_test_app_icat,
valid_icat_credentials_header,
multiple_investigation_test_data,
):
"""
Testing the rollback functionality when an `ICATValidationError` is thrown when
trying to update data as per the request body.
In this test, the first dictionary in the request body contains valid data. This
record should be successfully updated. The second dictionary contains data that
will throw an ICAT related exception. At this point, the rollback behaviour
should execute, restoring the state of the first record (i.e. un-updating it)
"""

request_body = [
{
"id": multiple_investigation_test_data[0]["id"],
"summary": "An example summary for an investigation used for testing.",
},
{"id": multiple_investigation_test_data[1]["id"], "doi": "_" * 256,},
]

update_response = flask_test_app_icat.patch(
"/investigations", headers=valid_icat_credentials_header, json=request_body,
)

# Get first entity that would've been successfully updated to ensure the changes
# were rolled back when the ICATValidationError occurred for the second entity
# in the request body
get_response = flask_test_app_icat.get(
f"/investigations/{multiple_investigation_test_data[0]['id']}",
headers=valid_icat_credentials_header,
)
get_response_json = prepare_icat_data_for_assertion([get_response.json])

assert update_response.status_code == 500
# RHS encased in a list as prepare_icat_data_for_assertion() always returns list
assert get_response_json == [multiple_investigation_test_data[0]]

0 comments on commit 63b3537

Please sign in to comment.