diff --git a/datagateway_api/common/database/helpers.py b/datagateway_api/common/database/helpers.py index 8a208717..55dbb40f 100644 --- a/datagateway_api/common/database/helpers.py +++ b/datagateway_api/common/database/helpers.py @@ -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): diff --git a/test/db/endpoints/test_findone_db.py b/test/db/endpoints/test_findone_db.py new file mode 100644 index 00000000..3da73274 --- /dev/null +++ b/test/db/endpoints/test_findone_db.py @@ -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 diff --git a/test/icat/endpoints/test_findone_icat.py b/test/icat/endpoints/test_findone_icat.py index 3934eb22..778c7ebb 100644 --- a/test/icat/endpoints/test_findone_icat.py +++ b/test/icat/endpoints/test_findone_icat.py @@ -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,