Skip to content

Commit

Permalink
#150: Add get by ID tests for DB backend
Browse files Browse the repository at this point in the history
  • Loading branch information
MRichards99 committed Dec 4, 2020
1 parent f5c4558 commit b6c7c66
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
29 changes: 28 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
delete_row_by_id,
insert_row_into_table,
)
from datagateway_api.common.database.models import SESSION
from datagateway_api.common.database.models import INVESTIGATION, SESSION
from datagateway_api.src.main import create_api_endpoints, create_app_infrastructure
from test.icat.test_query import prepare_icat_data_for_assertion

Expand Down Expand Up @@ -103,6 +103,33 @@ def single_investigation_test_data(icat_client):
print(e)


@pytest.fixture()
def single_investigation_test_data_db():
investigation = INVESTIGATION()
investigation.NAME = "Test Data for DataGateway API Testing (DB)"
investigation.TITLE = "Title for DataGateway API Testing (DB)"
investigation.STARTDATE = datetime(
year=2020, month=1, day=4, hour=1, minute=1, second=1,
)
investigation.ENDDATE = datetime(
year=2020, month=1, day=8, hour=1, minute=1, second=1,
)
investigation.VISIT_ID = str(uuid.uuid1())
investigation.FACILITY_ID = 1
investigation.TYPE_ID = 1

investigation.CREATE_TIME = datetime(2000, 1, 1)
investigation.MOD_TIME = datetime(2000, 1, 1)
investigation.CREATE_ID = "test create id"
investigation.MOD_ID = "test mod id"

insert_row_into_table(INVESTIGATION, investigation)

yield investigation

delete_row_by_id(INVESTIGATION, investigation.ID)


@pytest.fixture()
def multiple_investigation_test_data(icat_client):
investigation_dicts = []
Expand Down
38 changes: 38 additions & 0 deletions test/db/endpoints/test_get_by_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class TestDBGetByID:
def test_valid_get_with_id(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
):
# Need to identify the ID given to the test data
investigation_data = flask_test_app_db.get(
'/investigations?where={"TITLE": {"like": "Title for DataGateway API'
' Testing (DB)"}}',
headers=valid_db_credentials_header,
)
print(investigation_data.json)
test_data_id = investigation_data.json[0]["ID"]

test_response = flask_test_app_db.get(
f"/investigations/{test_data_id}", headers=valid_db_credentials_header,
)

assert test_response.json == single_investigation_test_data_db.to_dict()

def test_invalid_get_with_id(
self, flask_test_app_db, valid_db_credentials_header,
):
final_investigation_result = flask_test_app_db.get(
'/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.get(
f"/investigations/{test_data_id + 100}",
headers=valid_db_credentials_header,
)

assert test_response.status_code == 404
2 changes: 1 addition & 1 deletion test/icat/endpoints/test_get_by_id.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from test.icat.test_query import prepare_icat_data_for_assertion


class TestGetByID:
class TestICATGetByID:
def test_valid_get_with_id(
self,
flask_test_app_icat,
Expand Down

0 comments on commit b6c7c66

Please sign in to comment.