Skip to content

Commit

Permalink
test: fix failing tests #283
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktor Bozhinov committed Nov 18, 2021
1 parent 81fa469 commit e69af78
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 124 deletions.
7 changes: 5 additions & 2 deletions test/datagateway_api/db/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

from datagateway_api.src.common.config import config
from datagateway_api.src.common.constants import Constants
from datagateway_api.src.datagateway_api.database.helpers import (
delete_row_by_id,
Expand Down Expand Up @@ -116,14 +117,16 @@ def isis_specific_endpoint_data_db():
@pytest.fixture()
def final_instrument_id(flask_test_app_db, valid_db_credentials_header):
final_instrument_result = flask_test_app_db.get(
'/instruments/findone?order="id DESC"', headers=valid_db_credentials_header,
f'{config.datagateway_api.extension}/instruments/findone?order="id DESC"',
headers=valid_db_credentials_header,
)
return final_instrument_result.json["id"]


@pytest.fixture()
def final_facilitycycle_id(flask_test_app_db, valid_db_credentials_header):
final_facilitycycle_result = flask_test_app_db.get(
'/facilitycycles/findone?order="id DESC"', headers=valid_db_credentials_header,
f'{config.datagateway_api.extension}/facilitycycles/findone?order="id DESC"',
headers=valid_db_credentials_header,
)
return final_facilitycycle_result.json["id"]
11 changes: 7 additions & 4 deletions test/datagateway_api/db/endpoints/test_count_with_filters_db.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import pytest

from datagateway_api.src.common.config import config


class TestDBCountWithFilters:
@pytest.mark.usefixtures("single_investigation_test_data_db")
def test_valid_count_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations/count?where={"title": {"like": "Title for DataGateway API'
' Testing (DB)"}}',
f"{config.datagateway_api.extension}/investigations/count?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}',
headers=valid_db_credentials_header,
)

Expand All @@ -18,8 +20,9 @@ def test_valid_no_results_count_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations/count?where={"title": {"like": "This filter should cause a'
'404 for testing purposes..."}}',
f"{config.datagateway_api.extension}/investigations/count?where="
'{"title": {"like": "This filter should cause a404 for testing '
'purposes..."}}',
headers=valid_db_credentials_header,
)

Expand Down
12 changes: 8 additions & 4 deletions test/datagateway_api/db/endpoints/test_findone_db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datagateway_api.src.common.config import config


class TestDBFindone:
def test_valid_findone_with_filters(
self,
Expand All @@ -6,8 +9,8 @@ def test_valid_findone_with_filters(
single_investigation_test_data_db,
):
test_response = flask_test_app_db.get(
'/investigations/findone?where={"title": {"like": "Title for DataGateway'
' API Testing (DB)"}}',
f"{config.datagateway_api.extension}/investigations/findone?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}',
headers=valid_db_credentials_header,
)

Expand All @@ -17,8 +20,9 @@ def test_valid_no_results_findone_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations/findone?where={"title": {"eq": "This filter should cause a'
'404 for testing purposes..."}}',
f"{config.datagateway_api.extension}/investigations/findone?where="
'{"title": {"eq": "This filter should cause a404 for testing '
'purposes..."}}',
headers=valid_db_credentials_header,
)

Expand Down
15 changes: 10 additions & 5 deletions test/datagateway_api/db/endpoints/test_get_by_id_db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datagateway_api.src.common.config import config


class TestDBGetByID:
def test_valid_get_with_id(
self,
Expand All @@ -7,14 +10,15 @@ def test_valid_get_with_id(
):
# 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)"}}',
f"{config.datagateway_api.extension}/investigations?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}',
headers=valid_db_credentials_header,
)
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,
f"{config.datagateway_api.extension}/investigations/{test_data_id}",
headers=valid_db_credentials_header,
)

assert test_response.json == single_investigation_test_data_db.to_dict()
Expand All @@ -23,14 +27,15 @@ 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"',
f"{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.get(
f"/investigations/{test_data_id + 100}",
f"{config.datagateway_api.extension}/investigations/{test_data_id + 100}",
headers=valid_db_credentials_header,
)

Expand Down
23 changes: 13 additions & 10 deletions test/datagateway_api/db/endpoints/test_get_with_filters.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

from datagateway_api.src.common.config import config
from datagateway_api.src.common.constants import Constants
from datagateway_api.src.common.date_handler import DateHandler

Expand All @@ -12,8 +13,8 @@ def test_valid_get_with_filters(
single_investigation_test_data_db,
):
test_response = flask_test_app_db.get(
'/investigations?where={"title": {"like": "Title for DataGateway API'
' Testing (DB)"}}',
f"{config.datagateway_api.extension}/investigations?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}',
headers=valid_db_credentials_header,
)

Expand All @@ -23,8 +24,9 @@ def test_valid_no_results_get_with_filters(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations?where={"title": {"eq": "This filter should cause a 404 for'
'testing purposes..."}}',
f"{config.datagateway_api.extension}/investigations?where="
'{"title": {"eq": "This filter should cause a 404 fortesting '
'purposes..."}}',
headers=valid_db_credentials_header,
)

Expand All @@ -35,8 +37,9 @@ def test_valid_get_with_filters_multiple_distinct(
self, flask_test_app_db, valid_db_credentials_header,
):
test_response = flask_test_app_db.get(
'/investigations?where={"title": {"like": "Title for DataGateway API'
' Testing (DB)"}}&distinct="title"',
f"{config.datagateway_api.extension}/investigations?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}'
'&distinct="title"',
headers=valid_db_credentials_header,
)

Expand Down Expand Up @@ -125,8 +128,8 @@ def test_valid_get_with_filters_related_distinct(
expected_response,
):
test_response = flask_test_app_db.get(
'/investigations?where={"title": {"like": "Title for DataGateway API'
' Testing (DB)"}}'
f"{config.datagateway_api.extension}/investigations?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}'
f"&distinct={distinct_param}",
headers=valid_db_credentials_header,
)
Expand All @@ -145,8 +148,8 @@ def test_limit_skip_merge_get_with_filters(
limit_value = 2

test_response = flask_test_app_db.get(
'/investigations?where={"title": {"like": "Title for DataGateway API'
' Testing (DB)"}}'
f"{config.datagateway_api.extension}/investigations?where="
'{"title": {"like": "Title for DataGateway API Testing (DB)"}}'
f'&skip={skip_value}&limit={limit_value}&order="id ASC"',
headers=valid_db_credentials_header,
)
Expand Down
5 changes: 4 additions & 1 deletion test/datagateway_api/db/endpoints/test_ping_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
import pytest
from sqlalchemy.exc import SQLAlchemyError

from datagateway_api.src.common.config import config
from datagateway_api.src.common.constants import Constants
from datagateway_api.src.common.exceptions import DatabaseError
from datagateway_api.src.datagateway_api.backends import create_backend


class TestICATPing:
def test_valid_ping(self, flask_test_app_db):
test_response = flask_test_app_db.get("/ping")
test_response = flask_test_app_db.get(
f"{config.datagateway_api.extension}/ping",
)

assert test_response.json == Constants.PING_OK_RESPONSE

Expand Down
36 changes: 24 additions & 12 deletions test/datagateway_api/db/endpoints/test_table_endpoints_db.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from datagateway_api.src.common.config import config


class TestDBTableEndpoints:
"""
This class tests the endpoints defined in table_endpoints.py, commonly referred to
Expand All @@ -11,7 +14,8 @@ def test_valid_get_facility_cycles_with_filters(
isis_specific_endpoint_data_db,
):
test_response = flask_test_app_db.get(
f"/instruments/{int(isis_specific_endpoint_data_db[0])}/facilitycycles",
f"{config.datagateway_api.extension}/instruments"
f"/{int(isis_specific_endpoint_data_db[0])}/facilitycycles",
headers=valid_db_credentials_header,
)

Expand All @@ -21,7 +25,8 @@ def test_invalid_get_facility_cycles_with_filters(
self, flask_test_app_db, valid_db_credentials_header, final_instrument_id,
):
test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles",
f"{config.datagateway_api.extension}/instruments"
f"/{final_instrument_id + 100}/facilitycycles",
headers=valid_db_credentials_header,
)

Expand All @@ -34,7 +39,8 @@ def test_valid_get_facility_cycles_count_with_filters(
isis_specific_endpoint_data_db,
):
test_response = flask_test_app_db.get(
f"/instruments/{isis_specific_endpoint_data_db[0]}/facilitycycles/count",
f"{config.datagateway_api.extension}/instruments"
f"/{isis_specific_endpoint_data_db[0]}/facilitycycles/count",
headers=valid_db_credentials_header,
)

Expand All @@ -44,7 +50,8 @@ def test_invalid_get_facility_cycles_count_with_filters(
self, flask_test_app_db, valid_db_credentials_header, final_instrument_id,
):
test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles/count",
f"{config.datagateway_api.extension}/instruments"
f"/{final_instrument_id + 100}/facilitycycles/count",
headers=valid_db_credentials_header,
)

Expand All @@ -57,8 +64,9 @@ def test_valid_get_investigations_with_filters(
isis_specific_endpoint_data_db,
):
test_response = flask_test_app_db.get(
f"/instruments/{isis_specific_endpoint_data_db[0]}/facilitycycles/"
f"{isis_specific_endpoint_data_db[1].to_dict()['id']}/investigations",
f"{config.datagateway_api.extension}/instruments"
f"/{isis_specific_endpoint_data_db[0]}/facilitycycles"
f"/{isis_specific_endpoint_data_db[1].to_dict()['id']}/investigations",
headers=valid_db_credentials_header,
)

Expand All @@ -72,8 +80,9 @@ def test_invalid_get_investigations_with_filters(
final_facilitycycle_id,
):
test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles/"
f"{final_facilitycycle_id + 100}/investigations",
f"{config.datagateway_api.extension}/instruments"
f"/{final_instrument_id + 100}/facilitycycles"
f"/{final_facilitycycle_id + 100}/investigations",
headers=valid_db_credentials_header,
)

Expand All @@ -86,8 +95,10 @@ def test_valid_get_investigations_count_with_filters(
isis_specific_endpoint_data_db,
):
test_response = flask_test_app_db.get(
f"/instruments/{isis_specific_endpoint_data_db[0]}/facilitycycles/"
f"{isis_specific_endpoint_data_db[1].to_dict()['id']}/investigations/count",
f"{config.datagateway_api.extension}/instruments"
f"/{isis_specific_endpoint_data_db[0]}/facilitycycles"
f"/{isis_specific_endpoint_data_db[1].to_dict()['id']}"
"/investigations/count",
headers=valid_db_credentials_header,
)

Expand All @@ -101,8 +112,9 @@ def test_invalid_get_investigations_count_with_filters(
final_facilitycycle_id,
):
test_response = flask_test_app_db.get(
f"/instruments/{final_instrument_id + 100}/facilitycycles/"
f"{final_facilitycycle_id + 100}/investigations/count",
f"{config.datagateway_api.extension}/instruments"
f"/{final_instrument_id + 100}/facilitycycles"
f"/{final_facilitycycle_id + 100}/investigations/count",
headers=valid_db_credentials_header,
)

Expand Down
16 changes: 12 additions & 4 deletions test/datagateway_api/db/test_requires_session_id.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
from datagateway_api.src.common.config import config


class TestRequiresSessionID:
"""
This class tests the session decorator used for the database backend. The equivalent
decorator for the Python ICAT backend is tested in `test_session_handling.py`
"""

def test_invalid_missing_credentials(self, flask_test_app_db):
test_response = flask_test_app_db.get("/datafiles")
test_response = flask_test_app_db.get(
f"{config.datagateway_api.extension}/datafiles",
)

assert test_response.status_code == 401

def test_invalid_credentials(self, flask_test_app_db, invalid_credentials_header):
test_response = flask_test_app_db.get(
"/datafiles", headers=invalid_credentials_header,
f"{config.datagateway_api.extension}/datafiles",
headers=invalid_credentials_header,
)

assert test_response.status_code == 403

def test_bad_credentials(self, flask_test_app_db, bad_credentials_header):
test_response = flask_test_app_db.get(
"/datafiles", headers=bad_credentials_header,
f"{config.datagateway_api.extension}/datafiles",
headers=bad_credentials_header,
)

assert test_response.status_code == 403

def test_valid_credentials(self, flask_test_app_db, valid_db_credentials_header):
test_response = flask_test_app_db.get(
"/datafiles?limit=0", headers=valid_db_credentials_header,
f"{config.datagateway_api.extension}/datafiles?limit=0",
headers=valid_db_credentials_header,
)

assert test_response.status_code == 200
10 changes: 6 additions & 4 deletions test/datagateway_api/icat/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,16 @@ def isis_specific_endpoint_data(icat_client):
@pytest.fixture()
def final_instrument_id(flask_test_app_icat, valid_icat_credentials_header):
final_instrument_result = flask_test_app_icat.get(
'/instruments/findone?order="id DESC"', headers=valid_icat_credentials_header,
f'{config.datagateway_api.extension}/instruments/findone?order="id DESC"',
headers=valid_icat_credentials_header,
)
return final_instrument_result.json["id"]


@pytest.fixture()
def final_facilitycycle_id(flask_test_app_icat, valid_icat_credentials_header):
final_facilitycycle_result = flask_test_app_icat.get(
'/facilitycycles/findone?order="id DESC"',
f'{config.datagateway_api.extension}/facilitycycles/findone?order="id DESC"',
headers=valid_icat_credentials_header,
)
return final_facilitycycle_result.json["id"]
Expand All @@ -180,7 +181,8 @@ def remove_test_created_investigation_data(
yield

created_test_data = flask_test_app_icat.get(
'/investigations?where={"name":{"like":'
f"{config.datagateway_api.extension}/investigations?where="
'{"name":{"like":'
f'"{TestICATCreateData.investigation_name_prefix}"'
"}}",
headers=valid_icat_credentials_header,
Expand All @@ -192,6 +194,6 @@ def remove_test_created_investigation_data(

for investigation_id in investigation_ids:
flask_test_app_icat.delete(
f"/investigations/{investigation_id}",
f"{config.datagateway_api.extension}/investigations/{investigation_id}",
headers=valid_icat_credentials_header,
)
Loading

0 comments on commit e69af78

Please sign in to comment.