Exemple #1
0
def get_distinct_query(field, collection, query_args_func, valid_keys):
    """Perform a distinct query on the collection for the specified field.

    It will first search the database with the provided query arguments.

    :param field: The field to get the unique values of.
    :type field: str
    :param collection: The database collection.
    :param query_args_func: The function to get the query arguments.
    :type query_args_func: function
    :param valid_keys: The valid keys for the resource.
    :type valid_keys: list or dict
    :return The number of results.
    :rtype int
    """
    fields = None
    limit = 0
    skip = 0
    sort = None
    spec = {}

    spec, sort, fields, skip, limit, _ = \
        get_all_query_values(query_args_func, valid_keys)

    result = collection.find(
        spec, fields, limit=limit, skip=skip, sort=sort)
    if result:
        result = len(result.distinct(field))
    else:
        result = 0

    return result
def get_distinct_query(field, collection, query_args_func, valid_keys):
    """Perform a distinct query on the collection for the specified field.

    It will first search the database with the provided query arguments.

    :param field: The field to get the unique values of.
    :type field: str
    :param collection: The database collection.
    :param query_args_func: The function to get the query arguments.
    :type query_args_func: function
    :param valid_keys: The valid keys for the resource.
    :type valid_keys: list or dict
    :return The number of results.
    :rtype int
    """
    fields = None
    limit = 0
    skip = 0
    sort = None
    spec = {}

    spec, sort, fields, skip, limit, _ = \
        get_all_query_values(query_args_func, valid_keys)

    result = collection.find(
        spec=spec, limit=limit, skip=skip, fields=fields, sort=sort)
    if result:
        result = len(result.distinct(field))
    else:
        result = 0

    return result
    def test_get_all_query_values(self):
        def query_args_func(key):
            args = {
                "skip": [10, 20, 30],
                "sort": ["job"],
            }
            return args.get(key, [])

        valid_keys = []
        expected = ({}, [("job", -1)], None, 30, 0, None)

        return_value = get_all_query_values(query_args_func, valid_keys)
        self.assertTupleEqual(tuple(return_value), expected)