Skip to content

Commit

Permalink
Implement more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Reillyhewitson committed Nov 3, 2022
1 parent 451e618 commit 9cc8757
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 54 deletions.
20 changes: 1 addition & 19 deletions datagateway_api/src/datagateway_api/database/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,25 +182,7 @@ def update_from_dict(self, dictionary):
:returns: The updated dict
"""
for key in dictionary:
try:
if hasattr(self.__table__.columns[key.upper()].type, "length"):
if (
len(dictionary[key])
> self.__table__.columns[key.upper()].type.length
):
raise AttributeError

setattr(self, key, dictionary[key])

except AttributeError:
raise BadRequestError(
f"Bad request made, cannot modify attribute '{key}'"
)

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

return self.to_dict()

Expand Down
36 changes: 34 additions & 2 deletions test/datagateway_api/db/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from datagateway_api.src.common.config import Config
from datagateway_api.src.common.constants import Constants
from datagateway_api.src.common.exceptions import MissingRecordError
from datagateway_api.src.datagateway_api.database.helpers import (
delete_row_by_id,
insert_row_into_table,
Expand All @@ -15,6 +16,7 @@
INVESTIGATION,
INVESTIGATIONINSTRUMENT,
)
from test.datagateway_api.db.endpoints.test_create_db import TestDBCreateData


def set_meta_attributes(entity):
Expand Down Expand Up @@ -65,8 +67,11 @@ def single_investigation_test_data_db():
investigation = create_investigation_db_data()

yield investigation

delete_row_by_id(INVESTIGATION, investigation.id)
try:
delete_row_by_id(INVESTIGATION, investigation.id)
except MissingRecordError as e:
# This should occur on DELETE endpoints, normal behaviour for those tests
print(e)


@pytest.fixture()
Expand Down Expand Up @@ -134,3 +139,30 @@ def final_facilitycycle_id(flask_test_app_db, valid_db_credentials_header):
headers=valid_db_credentials_header,
)
return final_facilitycycle_result.json["id"]


@pytest.fixture()
def remove_test_created_investigation_data(
flask_test_app_db, valid_db_credentials_header,
):
yield

created_test_data = flask_test_app_db.get(
f"{Config.config.datagateway_api.extension}/investigations?where="
'{"name":{"like":'
f'"{TestDBCreateData.investigation_name_prefix}"'
"}}",
headers=valid_db_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_db.delete(
f"{Config.config.datagateway_api.extension}/investigations"
f"/{investigation_id}",
headers=valid_db_credentials_header,
)
94 changes: 94 additions & 0 deletions test/datagateway_api/db/endpoints/test_create_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import pytest

from datagateway_api.src.common.config import Config


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

@pytest.mark.usefixtures("remove_test_created_investigation_data")
def test_valid_create_data(self, flask_test_app_db, valid_db_credentials_header):
create_investigations_json = [
{
"name": f"{self.investigation_name_prefix} {i}",
"title": "Test data for the Python DB Backend on DataGateway API",
"summary": "DB Test data for DataGateway API testing",
"releaseDate": "2020-03-03 08:00:08+00:00",
"startDate": "2020-02-02 09:00:09+00:00",
"endDate": "2020-02-03 10:00:10+00:00",
"visitId": "Data Creation Visit DB",
"doi": "DataGateway API DB Test DOI",
"facilityID": 1,
"typeID": 1,
}
for i in range(2)
]

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

response_json = test_response.json

for investigation_request in response_json:
investigation_request.pop("createId")
investigation_request.pop("createTime")
investigation_request.pop("id")
investigation_request.pop("modId")
investigation_request.pop("modTime")

assert create_investigations_json == response_json

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

create_investigation_json = {
"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",
"startDate": "2020-02-02 09:00:09+00:00",
"endDate": "2020-02-03 10:00:10+00:00",
"visitId": "Data Creation Visit",
"doi": "DataGateway API Test DOI",
"facilityID": 1,
"typeID": 1,
}

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

response_json = test_response.json

response_json.pop("createId")
response_json.pop("createTime")
response_json.pop("id")
response_json.pop("modId")
response_json.pop("modTime")

assert create_investigation_json == response_json

def test_invalid_create_data(
self, flask_test_app_db, valid_db_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_db.post(
f"{Config.config.datagateway_api.extension}/investigations",
headers=valid_db_credentials_header,
json=invalid_request_body,
)

assert test_response.status_code == 400
40 changes: 40 additions & 0 deletions test/datagateway_api/db/endpoints/test_delete_by_id_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from datagateway_api.src.common.config import Config


class TestDeleteById:
def test_valid_delete_with_id(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
):
single_investigation_test_data = single_investigation_test_data_db.to_dict()

test_response = flask_test_app_db.delete(
f"{Config.config.datagateway_api.extension}/investigations"
f'/{single_investigation_test_data["id"]}',
headers=valid_db_credentials_header,
)

assert test_response.status_code == 204

def test_invalid_delete_with_id(
self, flask_test_app_db, valid_db_credentials_header,
):
"""Request with a non-existent ID"""

final_investigation_result = flask_test_app_db.get(
f"{Config.config.datagateway_api.extension}/investigations"
'/findone?order="id DESC"',
headers=valid_db_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_db.delete(
f"{Config.config.datagateway_api.extension}/investigations"
f"/{test_data_id + 100}",
headers=valid_db_credentials_header,
)

assert test_response.status_code == 404
2 changes: 1 addition & 1 deletion test/datagateway_api/db/endpoints/test_update_by_id_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ def test_invalid_update_with_id(
json=invalid_update_json,
)

assert test_response.status_code == 400
assert test_response.json["doi"] == "_" * 255
31 changes: 0 additions & 31 deletions test/datagateway_api/db/endpoints/test_update_multiple_db.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import pytest
from datagateway_api.src.common.config import Config


Expand Down Expand Up @@ -84,33 +83,3 @@ def test_invalid_missing_update_data(
)

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
1 change: 0 additions & 1 deletion test/datagateway_api/icat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ def remove_test_created_investigation_data(
"}}",
headers=valid_icat_credentials_header,
)

investigation_ids = []
for investigation in created_test_data.json:
investigation_ids.append(investigation["id"])
Expand Down
1 change: 1 addition & 0 deletions test/search_api/endpoints/test_count_dataset_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class TestSearchAPICountDatasetFilesEndpoint:
def test_valid_count_dataset_files_endpoint(
self, flask_test_app_search_api, pid, request_filter, expected_json,
):

test_response = flask_test_app_search_api.get(
f"{Config.config.search_api.extension}/datasets/{pid}/files/count"
f"?where={request_filter}",
Expand Down

0 comments on commit 9cc8757

Please sign in to comment.