def test_execute(self):
        mock_client = MockClient({
            ('max: 10', 'cursor: 0'): {
                'objects': list(range(3, 13)),
                'next_cursor': 11
            },
            ('max: 10', 'cursor: 11'): {
                'objects': list(range(13, 23)),
                'next_cursor': '22'
            },
            ('max: 10', 'cursor: 22'): {
                'objects': [1, 4],
                'next_cursor': None
            },
        })
        paginated = snug.paginated(mylist(max_items=10))
        assert isinstance(paginated, snug.Query)
        paginator = snug.execute(paginated, client=mock_client)

        result = list(paginator)
        assert result == [
            list(range(3, 13)),
            list(range(13, 23)),
            [1, 4],
        ]

        # is reusable
        assert list(snug.execute(paginated, client=mock_client))
示例#2
0
文件: execution.py 项目: g-k/quiz
def execute(obj, url, **kwargs):
    """Execute a GraphQL executable

    Parameters
    ----------
    obj: :data:`~quiz.execution.Executable`
        The object to execute.
        This may be a raw string or a query
    url: str
        The URL of the target endpoint
    **kwargs
         ``auth`` and/or ``client``, passed to :func:`snug.query.execute`.

    Returns
    -------
    JSON
        The response data

    Raises
    ------
    ErrorResponse
        If errors are present in the response
    HTTPError
        If the response has a non 2xx response code
    """
    snug_query = irelay(_exec(obj), partial(middleware, url))
    return snug.execute(snug_query, **kwargs)
示例#3
0
    def test_auth(self):
        client = MockClient(snug.Response(204))

        result = snug.execute(myquery(), auth=('user', 'pw'), client=client)
        assert result == snug.Response(204)
        assert client.request == snug.GET(
            'my/url', headers={'Authorization': 'Basic dXNlcjpwdw=='})
示例#4
0
    def test_defaults(self, mocker):
        send = mocker.patch('snug.query.send', autospec=True)

        assert snug.execute(myquery()) == send.return_value
        client, req = send.call_args[0]
        assert isinstance(client, urllib.OpenerDirector)
        assert req == snug.GET('my/url')
示例#5
0
def execute(obj, url, **kwargs):
    """Execute a GraphQL executable

    Parameters
    ----------
    obj: :data:`~quiz.execution.Executable`
        The object to execute.
        This may be a raw string or a query
    url: str
        The URL of the target endpoint
    **kwargs
         ``auth`` and/or ``client``, passed to :func:`snug.query.execute`.

    Returns
    -------
    RawResult (a dict) or the schema's return type
        In case of a raw string, a raw result.
        Otherwise, an instance of the schema's type queried for.

    Raises
    ------
    ErrorResponse
        If errors are present in the response
    HTTPError
        If the response has a non 2xx response code
    """
    snug_query = irelay(_exec(obj), partial(middleware, url))
    return snug.execute(snug_query, **kwargs)
示例#6
0
    def test_auth_callable(self):
        client = MockClient(snug.Response(204))
        auther = methodcaller('with_headers', {'X-My-Auth': 'letmein'})

        result = snug.execute(myquery(), auth=auther, client=client)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url',
                                          headers={'X-My-Auth': 'letmein'})
示例#7
0
    def test_custom_execute(self):
        client = MockClient(snug.Response(204))

        class MyQuery(object):
            def __execute__(self, client, auth):
                return client.send(snug.GET('my/url'))

        result = snug.execute(MyQuery(), client=client)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url')
示例#8
0
    def test_none_auth(self):
        client = MockClient(snug.Response(204))

        result = snug.execute(myquery(), auth=None, client=client)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url')
示例#9
0
    def test_custom_client(self):
        client = MockClient(snug.Response(204))

        result = snug.execute(myquery(), client=client)
        assert result == snug.Response(204)
        assert client.request == snug.GET('my/url')