diff --git a/test/conftest.py b/test/conftest.py index cd59e43a..6d7950d0 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -51,6 +51,36 @@ def single_investigation_test_data(icat_client): icat_client.delete(investigation) +@pytest.fixture() +def multiple_investigation_test_data(icat_client): + investigation_test_data = [] + investigation_dicts = [] + meta_attributes = Entity.MetaAttr + + for i in range(5): + investigation = icat_client.new("investigation") + investigation.name = f"Test Data for DataGateway API Testing {i}" + investigation.title = ( + f"Test data for the Python ICAT Backend on DataGateway API {i}" + ) + investigation.visitId = str(uuid.uuid1()) + investigation.facility = icat_client.get("Facility", 1) + investigation.type = icat_client.get("InvestigationType", 1) + investigation.create() + investigation_test_data.append(investigation) + investigation_dict = investigation.as_dict() + + for attr in meta_attributes: + investigation_dict.pop(attr) + + investigation_dicts.append(investigation_dict) + + yield investigation_dicts + + for entity in investigation_test_data: + icat_client.delete(entity) + + @pytest.fixture() def flask_test_app(): app.config["TESTING"] = True diff --git a/test/icat/test_standard_endpoints.py b/test/icat/test_standard_endpoints.py index a48d177d..5a76945d 100644 --- a/test/icat/test_standard_endpoints.py +++ b/test/icat/test_standard_endpoints.py @@ -44,6 +44,26 @@ def test_valid_get_with_filters( assert response_json == single_investigation_test_data + @pytest.mark.usefixtures("multiple_investigation_test_data") + def test_valid_get_with_filters_distinct( + self, flask_test_app, valid_credentials_header, + ): + test_response = flask_test_app.get( + '/investigations?where={"title": {"like": "Test data for the Python ICAT' + ' Backend on DataGateway API"}}&distinct="title"', + headers=valid_credentials_header, + ) + + expected = [ + { + "title": f"Test data for the Python ICAT Backend on DataGateway API {i}" + for i in range(5) + }, + ] + + for title in expected: + assert title in test_response.json + def test_invalid_get_with_filters(self): # Invalid data? pass @@ -113,10 +133,21 @@ def test_valid_get_with_id( assert response_json == single_investigation_test_data - def test_invalid_get_with_id(self): + def test_invalid_get_with_id(self, flask_test_app, valid_credentials_header): # Do a get one with filters (order desc), extract the id of that, add 5 and do a # request for that - pass + # Need to identify the ID given to the test data + final_investigation_result = flask_test_app.get( + '/investigations/findone?order="id DESC"', headers=valid_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.get( + f"/investigations/{test_data_id + 100}", headers=valid_credentials_header, + ) + + assert test_response.status_code == 404 def test_valid_delete_with_id(self): pass