Skip to content

Commit

Permalink
Add tests for multiple updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Reillyhewitson committed Nov 3, 2022
1 parent 5dc78c3 commit 451e618
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
5 changes: 5 additions & 0 deletions datagateway_api/src/datagateway_api/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ def update_from_dict(self, dictionary):
f"Bad request made, cannot modify attribute '{key}'"
)

except KeyError:
raise BadRequestError(
f"Bad request made, attribute'{key}' does not exist"
)

return self.to_dict()


Expand Down
116 changes: 116 additions & 0 deletions test/datagateway_api/db/endpoints/test_update_multiple_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import pytest
from datagateway_api.src.common.config import Config


class TestUpdateMultipleEntities:
def test_valid_multiple_update_data(
self,
flask_test_app_db,
valid_db_credentials_header,
multiple_investigation_test_data_db,
):
expected_doi = "DB Test Data Identifier"
expected_summary = "DB Test summary"

update_data_list = []
test_data_list = []

for investigation_object in multiple_investigation_test_data_db:
investigation = investigation_object.to_dict()
investigation["doi"] = expected_doi
investigation["summary"] = expected_summary

update_entity = {
"id": investigation["id"],
"doi": expected_doi,
"summary": expected_summary,
}
update_data_list.append(update_entity)
test_data_list.append(investigation)

test_response = flask_test_app_db.patch(
f"{Config.config.datagateway_api.extension}/investigations",
headers=valid_db_credentials_header,
json=update_data_list,
)

assert test_response.json == test_data_list

def test_valid_boundary_update_data(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
):
""" Request body is a dictionary, not a list of dictionaries"""

expected_doi = "Test Data Identifier"
expected_summary = "Test Summary"
single_investigation_test_data = single_investigation_test_data_db.to_dict()

update_data_json = {
"id": single_investigation_test_data["id"],
"doi": expected_doi,
"summary": expected_summary,
}
single_investigation_test_data["doi"] = expected_doi
single_investigation_test_data["summary"] = expected_summary

test_response = flask_test_app_db.patch(
f"{Config.config.datagateway_api.extension}/investigations",
headers=valid_db_credentials_header,
json=update_data_json,
)

assert test_response.json == [single_investigation_test_data]

def test_invalid_missing_update_data(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
):
"""There should be an ID in the request body to know which entity to update"""

update_data_json = {
"doi": "Test Data Identifier",
"summary": "Test Summary",
}

test_response = flask_test_app_db.patch(
f"{Config.config.datagateway_api.extension}/investigations",
headers=valid_db_credentials_header,
json=update_data_json,
)

assert test_response.status_code == 400

@pytest.mark.parametrize(
"update_key, update_value",
[
pytest.param("invalidAttr", "Some Value", id="invalid attribute"),
pytest.param("modId", "simple/root", id="meta attribute update"),
],
)
def test_invalid_attribute_update(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
update_key,
update_value,
):

single_investigation_test_data = single_investigation_test_data_db.to_dict()
invalid_update_data_json = {
"id": single_investigation_test_data["id"],
update_key: update_value,
}

test_response = flask_test_app_db.patch(
f"{Config.config.datagateway_api.extension}/investigations",
headers=valid_db_credentials_header,
json=invalid_update_data_json,
)

assert test_response.status_code == 400

0 comments on commit 451e618

Please sign in to comment.