示例#1
0
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
示例#2
0
def get_count_with_filters(client, entity_type, filters):
    """
    Get the number of results of a given entity, based on the filters provided in the
    request. This acts very much like `get_entity_with_filters()` but returns the number
    of results, as opposed to a JSON object of data.

    :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 filters: The list of filters to be applied to the request
    :type filters: List of specific implementations :class:`QueryFilter`
    :return: The number of records of the given entity (of type integer), using the
        filters to restrict the result of the query
    """
    log.info(
        "Getting the number of results of %s, also using the request's filters",
        entity_type,
    )

    query = ICATQuery(client, entity_type, aggregate="COUNT")

    filter_handler = FilterOrderHandler()
    filter_handler.manage_icat_filters(filters, query.query)

    data = query.execute_query(client, True)

    # Only ever 1 element in a count query result
    return data[0]
示例#3
0
    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]
示例#4
0
    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_invalid_field(self, icat_query):
        test_filter = PythonICATOrderFilter("unknown_field", "DESC")

        filter_handler = FilterOrderHandler()
        filter_handler.add_filter(test_filter)
        with pytest.raises(FilterError):
            filter_handler.apply_filters(icat_query)

        filter_handler.clear_python_icat_order_filters()
    def test_filter_applied_to_query(self, icat_query):
        test_filter = PythonICATOrderFilter("id", "DESC")

        filter_handler = FilterOrderHandler()
        filter_handler.add_filter(test_filter)
        filter_handler.apply_filters(icat_query)

        assert icat_query.order == [("id", "DESC")]

        filter_handler.clear_python_icat_order_filters()
    def test_result_order_appended(self, icat_query):
        id_filter = PythonICATOrderFilter("id", "ASC")
        title_filter = PythonICATOrderFilter("title", "DESC")

        filter_handler = FilterOrderHandler()
        filter_handler.add_filters([id_filter, title_filter])
        filter_handler.apply_filters(icat_query)

        assert PythonICATOrderFilter.result_order == [("id", "ASC"),
                                                      ("title", "DESC")]

        filter_handler.clear_python_icat_order_filters()
示例#8
0
def get_rows_by_filter(table, filters):
    """
    Given a list of filters supplied in json format, returns entities that match the
    filters from the given table

    :param table: The table to checked
    :param filters: The list of filters to be applied
    :return: A list of the rows returned in dictionary form
    """
    with ReadQuery(table) as query:
        filter_handler = FilterOrderHandler()
        return get_filtered_read_query_results(filter_handler, filters, query)
示例#9
0
def get_facility_cycles_for_instrument(instrument_id, filters):
    """
    Given an instrument_id get facility cycles where the instrument has investigations
    that occur within that cycle

    :param filters: The filters to be applied to the query
    :param instrument_id: The id of the instrument
    :return: A list of facility cycle entities
    """
    with InstrumentFacilityCyclesQuery(instrument_id) as query:
        filter_handler = FilterOrderHandler()
        return get_filtered_read_query_results(filter_handler, filters, query)
    def test_limit_and_skip_merge_correctly(self, icat_query, skip_value, limit_value):
        """
        Skip and limit values are set together in Python ICAT, limit value should match
        max entities allowed in one transaction in ICAT as defined in ICAT properties
        """
        skip_filter = PythonICATSkipFilter(skip_value)
        limit_filter = PythonICATLimitFilter(limit_value)

        filter_handler = FilterOrderHandler()
        filter_handler.add_filters([skip_filter, limit_filter])
        filter_handler.merge_python_icat_limit_skip_filters()
        filter_handler.apply_filters(icat_query)

        assert icat_query.limit == (skip_value, limit_value)
    def test_invalid_direction(self, icat_query):
        test_filter = PythonICATOrderFilter("id", "up")

        filter_handler = FilterOrderHandler()
        filter_handler.add_filter(test_filter)
        with pytest.raises(FilterError):
            filter_handler.apply_filters(icat_query)
示例#12
0
    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 == []
示例#13
0
def get_entity_with_filters(client, entity_type, filters):
    """
    Gets all the records of a given entity, based on the filters provided in the request

    :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 filters: The list of filters to be applied to the request
    :type filters: List of specific implementations :class:`QueryFilter`
    :return: The list of records of the given entity, using the filters to restrict the
        result of the query
    """
    log.info("Getting entity using request's filters")

    query = ICATQuery(client, entity_type)

    filter_handler = FilterOrderHandler()
    filter_handler.manage_icat_filters(filters, query.query)

    data = query.execute_query(client, True)

    return data
    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'"]}
示例#15
0
    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]
示例#16
0
    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_direction_is_uppercase(self, icat_query):
        """Direction must be uppercase for Python ICAT to see the input as valid"""
        test_filter = PythonICATOrderFilter("id", "asc")

        filter_handler = FilterOrderHandler()
        filter_handler.add_filter(test_filter)

        assert test_filter.direction == "ASC"

        filter_handler.clear_python_icat_order_filters()
示例#18
0
def get_facility_cycles_for_instrument_count(instrument_id, filters):
    """
    Given an instrument_id get the facility cycles count where the instrument has
    investigations that occur within that cycle

    :param filters: The filters to be applied to the query
    :param instrument_id: The id of the instrument
    :return: The count of the facility cycles
    """
    with InstrumentFacilityCyclesCountQuery(instrument_id) as query:
        filter_handler = FilterOrderHandler()
        filter_handler.add_filters(filters)
        filter_handler.apply_filters(query)
        return query.get_count()
示例#19
0
def get_filtered_row_count(table, filters):
    """
    returns the count of the rows that match a given filter in a given table
    :param table: the table to be checked
    :param filters: the filters to be applied to the query
    :return: int: the count of the rows
    """

    log.info(" getting count for %s", table.__tablename__)
    with CountQuery(table) as count_query:
        filter_handler = FilterOrderHandler()
        filter_handler.add_filters(filters)
        filter_handler.apply_filters(count_query)
        return count_query.get_count()
示例#20
0
def get_investigations_for_instrument_in_facility_cycle(
    instrument_id,
    facility_cycle_id,
    filters,
):
    """
    Given an instrument id and facility cycle id, get investigations that use the given
    instrument in the given cycle

    :param filters: The filters to be applied to the query
    :param instrument_id: The id of the instrument
    :param facility_cycle_id:  the ID of the facility cycle
    :return: The investigations
    """
    filter_handler = FilterOrderHandler()
    with InstrumentFacilityCycleInvestigationsQuery(
            instrument_id,
            facility_cycle_id,
    ) as query:
        return get_filtered_read_query_results(filter_handler, filters, query)
示例#21
0
def get_investigations_for_instrument_in_facility_cycle_count(
    instrument_id,
    facility_cycle_id,
    filters,
):
    """
    Given an instrument id and facility cycle id, get the count of the investigations
    that use the given instrument in the given cycle

    :param filters: The filters to be applied to the query
    :param instrument_id: The id of the instrument
    :param facility_cycle_id:  the ID of the facility cycle
    :return: The investigations count
    """
    with InstrumentFacilityCycleInvestigationsCountQuery(
            instrument_id,
            facility_cycle_id,
    ) as query:
        filter_handler = FilterOrderHandler()
        filter_handler.add_filters(filters)
        filter_handler.apply_filters(query)
        return query.get_count()
示例#22
0
    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)