Exemplo n.º 1
0
def test_queryable_where_manyfunc():
    q = Queryable(range(10)).where(lambda x: x % 2 == 0)
    q = q.where(lambda x: x % 3 == 0)
    assert next(q) == 0
    assert next(q) == 6
    with pytest.raises(StopIteration):
        next(q)
Exemplo n.º 2
0
    def default(self, entity=None, action=None, **kwargs):
        if entity is None:
            return self.help()

        response = {
            'request': {
                'status': 'ok',
                'entity': entity,
                'params': kwargs,
            }
        }

        try:
            to_expand = (kwargs.pop('_expand').split(',')
                         if '_expand' in kwargs else [])
            if to_expand:
                response['request']['expand'] = to_expand

            limit = kwargs.pop('_limit', None)
            if limit:
                try:
                    limit = int(limit)
                    if limit <= 0:
                        raise ValueError()
                except ValueError:
                    raise APIError('_limit must be a positive integer', 422)
                response['request']['limit'] = limit

            if 'realtime' in kwargs:
                if kwargs['realtime'] in ('y', 'Y', 'yes', 'Yes', 'YES',
                                          'true', 'True', 'TRUE', 'on', 'On',
                                          'ON'):
                    kwargs['realtime'] = True
                elif kwargs['realtime'] in ('n', 'N', 'no', 'No', 'NO',
                                            'false', 'False', 'FALSE', 'off',
                                            'Off', 'OFF'):
                    kwargs['realtime'] = False
                else:
                    raise APIError('realtime is not a boolean', 422)

            if action:
                response['request']['action'] = action
                if (entity, action) in self._entity_actions:
                    func, entity = self._entity_actions[(entity, action)]
                    result = func(**kwargs)
                else:
                    raise EndpointNotFoundError(entity, action)
            else:
                if 'provider.id' in kwargs:
                    provider_id = kwargs.pop('provider.id')
                    if provider_id in self._providers:
                        provider = self._providers[provider_id]
                        entity_func = getattr(provider, entity, None)
                    else:
                        entity_func = Queryable(())
                else:
                    entity_func = getattr(self, entity, None)
                if entity_func is not None:
                    result = entity_func.where(**kwargs)
                else:
                    raise EndpointNotFoundError(entity)

            if limit:
                result = itertools.islice(result, limit)

            response[entity] = unexpand_init(result, to_expand)
        except APIError as exc:
            response['request']['status'] = 'error'
            response['error'] = exc.msg
            cherrypy.response.status = exc.error_code

        return response
Exemplo n.º 3
0
def test_queryable_dict_obj_kwargs():
    q = Queryable({'id': x, 'odd': bool(x % 2)} for x in range(10))
    assert next(q.where(odd=True))['id'] == 1
Exemplo n.º 4
0
def test_queryable_where_none():
    q = Queryable(range(10))
    q_prime = q.where()
    assert q is q_prime