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))
Example #2
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
    -------
    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)
Example #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=='})
Example #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')
Example #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)
Example #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'})
Example #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')
Example #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')
Example #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')