def isis_specific_endpoint_data(icat_client): facility_cycle = icat_client.new("facilityCycle") facility_cycle.name = "Test cycle for DataGateway API testing" facility_cycle.startDate = datetime( year=2020, month=1, day=1, hour=1, minute=1, second=1, tzinfo=tzlocal(), ) facility_cycle.endDate = datetime( year=2020, month=2, day=1, hour=1, minute=1, second=1, tzinfo=tzlocal(), ) facility_cycle.facility = icat_client.get("Facility", 1) facility_cycle.create() investigation = create_investigation_test_data(icat_client) investigation_dict = prepare_icat_data_for_assertion([investigation]) instrument = icat_client.new("instrument") instrument.name = "Test Instrument for DataGateway API Endpoint Testing" instrument.facility = icat_client.get("Facility", 1) instrument.create() investigation_instrument = icat_client.new("investigationInstrument") investigation_instrument.investigation = investigation investigation_instrument.instrument = instrument investigation_instrument.create() facility_cycle_dict = prepare_icat_data_for_assertion([facility_cycle]) yield (instrument.id, facility_cycle_dict, facility_cycle.id, investigation_dict) try: # investigation_instrument removed when deleting the objects its related objects icat_client.delete(facility_cycle) icat_client.delete(investigation) icat_client.delete(instrument) except ICATNoObjectError as e: print(e)
def test_valid_multiple_update_data( self, flask_test_app_icat, valid_icat_credentials_header, multiple_investigation_test_data, ): expected_doi = "Test Data Identifier" expected_summary = "Test Summary" update_data_list = [] for investigation in multiple_investigation_test_data: investigation["doi"] = expected_doi investigation["summary"] = expected_summary update_entity = { "id": investigation["id"], "doi": expected_doi, "summary": expected_summary, } update_data_list.append(update_entity) test_response = flask_test_app_icat.patch( "/investigations", headers=valid_icat_credentials_header, json=update_data_list, ) response_json = prepare_icat_data_for_assertion(test_response.json) assert response_json == multiple_investigation_test_data
def test_valid_boundary_update_data( self, flask_test_app_icat, valid_icat_credentials_header, single_investigation_test_data, ): """ Request body is a dictionary, not a list of dictionaries""" expected_doi = "Test Data Identifier" expected_summary = "Test Summary" update_data_json = { "id": single_investigation_test_data[0]["id"], "doi": expected_doi, "summary": expected_summary, } single_investigation_test_data[0]["doi"] = expected_doi single_investigation_test_data[0]["summary"] = expected_summary test_response = flask_test_app_icat.patch( "/investigations", headers=valid_icat_credentials_header, json=update_data_json, ) response_json = prepare_icat_data_for_assertion(test_response.json) assert response_json == single_investigation_test_data
def test_limit_skip_merge_get_with_filters( self, flask_test_app_icat, valid_icat_credentials_header, multiple_investigation_test_data, ): skip_value = 1 limit_value = 2 test_response = flask_test_app_icat.get( '/investigations?where={"title": {"like": "Test data for the Python ICAT' ' Backend on DataGateway API"}}' f'&skip={skip_value}&limit={limit_value}&order="id ASC"', headers=valid_icat_credentials_header, ) response_json = prepare_icat_data_for_assertion(test_response.json) filtered_investigation_data = [] filter_count = 0 while filter_count < limit_value: filtered_investigation_data.append( multiple_investigation_test_data.pop(skip_value), ) filter_count += 1 assert response_json == filtered_investigation_data
def test_valid_create_data( self, flask_test_app_icat, valid_icat_credentials_header, ): create_investigations_json = [{ "name": f"{self.investigation_name_prefix} {i}", "title": "Test data for the Python ICAT Backend on DataGateway API", "summary": "Test data for DataGateway API testing", "releaseDate": "2020-03-03 08:00:08+00:00", "startDate": "2020-02-02 09:00:09+00:00", "endDate": "2020-02-03 10:00:10+00:00", "visitId": "Data Creation Visit", "doi": "DataGateway API Test DOI", "facility": 1, "type": 1, } for i in range(2)] test_response = flask_test_app_icat.post( "/investigations", headers=valid_icat_credentials_header, json=create_investigations_json, ) for investigation_request in create_investigations_json: investigation_request.pop("facility") investigation_request.pop("type") response_json = prepare_icat_data_for_assertion( test_response.json, remove_id=True, ) assert create_investigations_json == response_json
def test_valid_boundary_create_data( self, flask_test_app_icat, valid_icat_credentials_header, ): """Create a single investigation, as opposed to multiple""" create_investigation_json = { "name": f"{self.investigation_name_prefix} 0", "title": "Test data for the Python ICAT Backend on the API", "summary": "Test data for DataGateway API testing", "releaseDate": "2020-03-03 08:00:08+00:00", "startDate": "2020-02-02 09:00:09+00:00", "endDate": "2020-02-03 10:00:10+00:00", "visitId": "Data Creation Visit", "doi": "DataGateway API Test DOI", "facility": 1, "type": 1, } test_response = flask_test_app_icat.post( "/investigations", headers=valid_icat_credentials_header, json=create_investigation_json, ) create_investigation_json.pop("facility") create_investigation_json.pop("type") response_json = prepare_icat_data_for_assertion( test_response.json, remove_id=True, ) assert [create_investigation_json] == response_json
def multiple_investigation_test_data(icat_client): investigation_dicts = [] investigations = create_investigation_test_data(icat_client, num_entities=5) investigation_dicts = prepare_icat_data_for_assertion(investigations) yield investigation_dicts for investigation in investigations: icat_client.delete(investigation)
def single_investigation_test_data(icat_client): investigation = create_investigation_test_data(icat_client) investigation_dict = prepare_icat_data_for_assertion([investigation]) yield investigation_dict # Remove data from ICAT try: icat_client.delete(investigation) except ICATNoObjectError as e: # This should occur on DELETE endpoints, normal behaviour for those tests print(e)
def test_valid_get_facility_cycles_with_filters( self, flask_test_app_icat, valid_icat_credentials_header, isis_specific_endpoint_data, ): test_response = flask_test_app_icat.get( f"/instruments/{isis_specific_endpoint_data[0]}/facilitycycles", headers=valid_icat_credentials_header, ) response_json = prepare_icat_data_for_assertion(test_response.json) assert response_json == isis_specific_endpoint_data[1]
def test_valid_findone_with_filters( self, flask_test_app_icat, valid_icat_credentials_header, single_investigation_test_data, ): test_response = flask_test_app_icat.get( '/investigations/findone?where={"title": {"like": "Test data for the Python' ' ICAT Backend on DataGateway API"}}', headers=valid_icat_credentials_header, ) response_json = prepare_icat_data_for_assertion([test_response.json]) assert response_json == single_investigation_test_data
def test_valid_update_with_id( self, flask_test_app_icat, valid_icat_credentials_header, single_investigation_test_data, ): update_data_json = { "doi": "Test Data Identifier", "summary": "Test Summary", "startDate": "2019-01-04 01:01:01+00:00", } single_investigation_test_data[0].update(update_data_json) test_response = flask_test_app_icat.patch( f"/investigations/{single_investigation_test_data[0]['id']}", headers=valid_icat_credentials_header, json=update_data_json, ) response_json = prepare_icat_data_for_assertion([test_response.json]) assert response_json == single_investigation_test_data
def test_valid_rollback_behaviour( self, flask_test_app_icat, valid_icat_credentials_header, multiple_investigation_test_data, ): """ Testing the rollback functionality when an `ICATValidationError` is thrown when trying to update data as per the request body. In this test, the first dictionary in the request body contains valid data. This record should be successfully updated. The second dictionary contains data that will throw an ICAT related exception. At this point, the rollback behaviour should execute, restoring the state of the first record (i.e. un-updating it) """ request_body = [ { "id": multiple_investigation_test_data[0]["id"], "summary": "An example summary for an investigation used for testing.", }, {"id": multiple_investigation_test_data[1]["id"], "doi": "_" * 256}, ] update_response = flask_test_app_icat.patch( "/investigations", headers=valid_icat_credentials_header, json=request_body, ) # Get first entity that would've been successfully updated to ensure the changes # were rolled back when the ICATValidationError occurred for the second entity # in the request body get_response = flask_test_app_icat.get( f"/investigations/{multiple_investigation_test_data[0]['id']}", headers=valid_icat_credentials_header, ) get_response_json = prepare_icat_data_for_assertion([get_response.json]) assert update_response.status_code == 500 # RHS encased in a list as prepare_icat_data_for_assertion() always returns list assert get_response_json == [multiple_investigation_test_data[0]]
def test_valid_rollback_behaviour( self, flask_test_app_icat, valid_icat_credentials_header, ): request_body = [ { "name": "Test Investigation DG API Testing Name Test", "title": "My New Investigation with Title", "visitId": "Visit ID for Testing", "facility": 1, "type": 1, }, { "name": "Invalid Investigation for testing", "title": "My New Investigation with Title", "visitId": "Visit ID for Testing", "doi": "_" * 256, "facility": 1, "type": 1, }, ] create_response = flask_test_app_icat.post( "/investigations", headers=valid_icat_credentials_header, json=request_body, ) get_response = flask_test_app_icat.get( '/investigations?where={"title": {"eq": "' f'{request_body[0]["title"]}' '"}}', headers=valid_icat_credentials_header, ) get_response_json = prepare_icat_data_for_assertion(get_response.json) assert create_response.status_code == 400 assert get_response_json == []
def test_valid_get_with_id( self, flask_test_app_icat, valid_icat_credentials_header, single_investigation_test_data, ): # Need to identify the ID given to the test data investigation_data = flask_test_app_icat.get( '/investigations?where={"title": {"like": "Test data for the Python ICAT' ' Backend on DataGateway API"}}', headers=valid_icat_credentials_header, ) test_data_id = investigation_data.json[0]["id"] test_response = flask_test_app_icat.get( f"/investigations/{test_data_id}", headers=valid_icat_credentials_header, ) # Get with ID gives a dictionary response (only ever one result from that kind # of request), so list around json is required for the call response_json = prepare_icat_data_for_assertion([test_response.json]) assert response_json == single_investigation_test_data