def test_valid_operations( self, icat_query, operation, value, expected_condition_value, ): test_filter = PythonICATWhereFilter("id", value, operation) test_filter.apply_filter(icat_query) assert icat_query.conditions == {"id": expected_condition_value}
def get_investigations_for_instrument_in_facility_cycle( client, instrument_id, facilitycycle_id, filters, count_query=False, ): """ Given Instrument and Facility Cycle IDs, get investigations that use the given instrument in the given cycle :param client: ICAT client containing an authenticated user :type client: :class:`icat.client.Client` :param instrument_id: ID of the instrument from the request :type instrument_id: :class:`int` :param facilitycycle_id: ID of the facilityCycle from the request :type facilitycycle_id: :class:`int` :param filters: The list of filters to be applied to the request :type filters: List of specific implementations :class:`QueryFilter` :param count_query: Flag to determine if the query in this function should be used as a count query. Used for `get_investigations_for_instrument_in_facility_cycle_count()` :type count_query: :class:`bool` :return: A list of Investigations that match the query """ log.info( "Getting a list of investigations from the specified instrument and facility" " cycle, for ISIS", ) query_aggregate = "COUNT:DISTINCT" if count_query else "DISTINCT" query = ICATQuery(client, "Investigation", aggregate=query_aggregate) query.isis_endpoint = True instrument_id_check = PythonICATWhereFilter( "facility.instruments.id", instrument_id, "eq", ) investigation_instrument_id_check = PythonICATWhereFilter( "investigationInstruments.instrument.id", instrument_id, "eq", ) facility_cycle_id_check = PythonICATWhereFilter( "facility.facilityCycles.id", facilitycycle_id, "eq", ) facility_cycle_start_date_check = PythonICATWhereFilter( "facility.facilityCycles.startDate", "o.startDate", "lte", ) facility_cycle_end_date_check = PythonICATWhereFilter( "facility.facilityCycles.endDate", "o.startDate", "gte", ) required_filters = [ instrument_id_check, investigation_instrument_id_check, facility_cycle_id_check, facility_cycle_start_date_check, facility_cycle_end_date_check, ] filters.extend(required_filters) filter_handler = FilterOrderHandler() filter_handler.manage_icat_filters(filters, query.query) data = query.execute_query(client, True) return data
def test_add_filters(self): test_handler = FilterOrderHandler() id_filter = PythonICATWhereFilter("id", 2, "eq") name_filter = PythonICATWhereFilter("name", "New Name", "like") filter_list = [id_filter, name_filter] test_handler.add_filters(filter_list) assert test_handler.filters == filter_list
def test_multiple_conditions_per_field(self, icat_query): lt_filter = PythonICATWhereFilter("id", 10, "lt") gt_filter = PythonICATWhereFilter("id", 5, "gt") filter_handler = FilterOrderHandler() filter_handler.add_filters([lt_filter, gt_filter]) filter_handler.apply_filters(icat_query) assert icat_query.conditions == {"id": ["< '10'", "> '5'"]}
def test_add_filter(self, icat_query): test_handler = FilterOrderHandler() test_filter = PythonICATWhereFilter("id", 2, "eq") test_handler.add_filter(test_filter) assert test_handler.filters == [test_filter]
def test_json_format_execution_output( self, icat_client, single_investigation_test_data, ): test_query = ICATQuery(icat_client, "Investigation") test_data_filter = PythonICATWhereFilter( "title", "Test data for the Python ICAT Backend on DataGateway API", "like", ) test_data_filter.apply_filter(test_query.query) query_data = test_query.execute_query(icat_client, True) query_output_json = prepare_icat_data_for_assertion(query_data) assert query_output_json == single_investigation_test_data
def test_remove_filter(self): test_filter = PythonICATWhereFilter("id", 2, "eq") test_handler = FilterOrderHandler() test_handler.add_filter(test_filter) test_handler.remove_filter(test_filter) assert test_handler.filters == []
def test_apply_filters(self, icat_query): where_filter = PythonICATWhereFilter("id", 2, "eq") limit_filter = PythonICATLimitFilter(10) test_handler = FilterOrderHandler() test_handler.add_filters([where_filter, limit_filter]) test_handler.apply_filters(icat_query) assert icat_query.conditions == {"id": "= '2'"} and icat_query.limit == (0, 10)
def test_sort_filters(self): limit_filter = PythonICATLimitFilter(10) where_filter = PythonICATWhereFilter("id", 2, "eq") test_handler = FilterOrderHandler() test_handler.add_filters([limit_filter, where_filter]) test_handler.sort_filters() assert test_handler.filters == [where_filter, limit_filter]
def test_valid_login( self, flask_test_app_icat, icat_client, icat_query, request_body, ): login_response = flask_test_app_icat.post("/sessions", json=request_body) icat_client.sessionId = login_response.json["sessionID"] icat_query.setAggregate("COUNT") title_filter = PythonICATWhereFilter( "title", "Test data for the Python ICAT Backend on DataGateway API", "like", ) title_filter.apply_filter(icat_query) test_query = icat_client.search(icat_query) assert test_query == [1] and login_response.status_code == 201
def get_entity_by_id( client, entity_type, id_, return_json_formattable_data, return_related_entities=False, ): """ Gets a record of a given ID from the specified entity :param client: ICAT client containing an authenticated user :type client: :class:`icat.client.Client` :param entity_type: The type of entity requested to manipulate data with :type entity_type: :class:`str` :param id_: ID number of the entity to retrieve :type id_: :class:`int` :param return_json_formattable_data: Flag to determine whether the data should be returned as a list of data ready to be converted straight to JSON (i.e. if the data will be used as a response for an API call) or whether to leave the data in a Python ICAT format :type return_json_formattable_data: :class:`bool` :param return_related_entities: Flag to determine whether related entities should automatically be returned or not. Returning related entities used as a bug fix for an `IcatException` where ICAT attempts to set a field to null because said field hasn't been included in the updated data :type return_related_entities: :class:`bool` :return: The record of the specified ID from the given entity :raises: MissingRecordError: If Python ICAT cannot find a record of the specified ID """ log.info("Getting %s of the ID %s", entity_type, id_) log.debug("Return related entities set to: %s", return_related_entities) # Set query condition for the selected ID id_condition = PythonICATWhereFilter.create_condition("id", "=", id_) includes_value = "1" if return_related_entities else None id_query = ICATQuery( client, entity_type, conditions=id_condition, includes=includes_value, ) entity_by_id_data = id_query.execute_query(client, return_json_formattable_data) if not entity_by_id_data: # Cannot find any data matching the given ID raise MissingRecordError("No result found") else: return entity_by_id_data[0]
def test_invalid_field(self, icat_query): test_filter = PythonICATWhereFilter("random_field", "my_value", "eq") with pytest.raises(FilterError): test_filter.apply_filter(icat_query)
def test_valid_field(self, icat_query): test_filter = PythonICATWhereFilter("title", "Investigation Title", "eq") test_filter.apply_filter(icat_query) assert icat_query.conditions == {"title": "= 'Investigation Title'"}
def test_valid_internal_icat_value(self, icat_query): """Check that values that point to other values in the schema are applied""" test_filter = PythonICATWhereFilter("startDate", "o.endDate", "lt") test_filter.apply_filter(icat_query) assert icat_query.conditions == {"startDate": "< o.endDate"}
def test_invalid_operation(self, icat_query): test_filter = PythonICATWhereFilter("id", 10, "non") with pytest.raises(FilterError): test_filter.apply_filter(icat_query)
def test_invalid_in_operation(self, icat_query): with pytest.raises(BadRequestError): PythonICATWhereFilter("id", "1, 2, 3, 4, 5", "in")
def test_remove_not_added_filter(self): test_handler = FilterOrderHandler() test_filter = PythonICATWhereFilter("id", 2, "eq") with pytest.raises(ValueError): test_handler.remove_filter(test_filter)