Skip to content

Commit

Permalink
#150: Add findone tests for DB backend
Browse files Browse the repository at this point in the history
- This commit also includes a fixes a bug found on the 404 of that test class
  • Loading branch information
MRichards99 committed Dec 7, 2020
1 parent 8c1941d commit 3eec346
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion datagateway_api/common/database/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,11 @@ def get_first_filtered_row(table, filters):
:return: the first row matching the filter
"""
log.info(" Getting first filtered row for %s", table.__tablename__)
return get_rows_by_filter(table, filters)[0]
try:
result = get_rows_by_filter(table, filters)[0]
except IndexError:
raise MissingRecordError()
return result


def get_filtered_row_count(table, filters):
Expand Down
25 changes: 25 additions & 0 deletions test/db/endpoints/test_findone_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class TestDBFindone:
def test_valid_findone_with_filters(
self,
flask_test_app_db,
valid_db_credentials_header,
single_investigation_test_data_db,
):
test_response = flask_test_app_db.get(
'/investigations/findone?where={"TITLE": {"like": "Title for DataGateway'
' API Testing (DB)"}}',
headers=valid_db_credentials_header,
)

assert test_response.json == single_investigation_test_data_db.to_dict()

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..."}}',
headers=valid_db_credentials_header,
)

assert test_response.status_code == 404
2 changes: 1 addition & 1 deletion test/icat/endpoints/test_findone_icat.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 TestFindone:
class TestICATFindone:
def test_valid_findone_with_filters(
self,
flask_test_app_icat,
Expand Down

0 comments on commit 3eec346

Please sign in to comment.