Ejemplo n.º 1
0
    def test_non_string(self, event_loop):
        query = quiz.Query(DogQuery, _.dog[_.name.bark_volume])
        client = MockClient(
            snug.Response(
                200,
                json.dumps({
                    'data': {
                        'dog': {
                            'name': 'Fred',
                            'bark_volume': 8,
                        }
                    }
                }).encode()))

        future = quiz.execute_async(query,
                                    url='https://my.url/api',
                                    client=client)
        result = event_loop.run_until_complete(future)
        assert result == DogQuery(dog=Dog(
            name='Fred',
            bark_volume=8,
        ))

        request = client.request
        assert request.url == 'https://my.url/api'
        assert request.method == 'POST'
        assert json.loads(request.content.decode()) == {
            'query': quiz.gql(query)
        }
        assert request.headers == {'Content-Type': 'application/json'}
Ejemplo n.º 2
0
    def test_errors(self, event_loop):
        client = MockAsyncClient(
            snug.Response(
                200,
                json.dumps({
                    "data": {
                        "foo": 4
                    },
                    "errors": [{
                        "message": "foo"
                    }]
                }).encode(),
            ))
        future = quiz.execute_async(
            "my query",
            url="https://my.url/api",
            client=client,
            auth=token_auth("foo"),
        )
        with pytest.raises(quiz.ErrorResponse) as exc:
            event_loop.run_until_complete(future)

        assert exc.value == quiz.ErrorResponse({"foo": 4}, [{
            "message": "foo"
        }])
Ejemplo n.º 3
0
    def test_non_string(self, event_loop):
        query = quiz.Query(DogQuery, _.dog[_.name.bark_volume])
        client = MockAsyncClient(
            snug.Response(
                200,
                json.dumps({
                    "data": {
                        "dog": {
                            "name": "Fred",
                            "bark_volume": 8
                        }
                    }
                }).encode(),
            ))

        future = quiz.execute_async(query,
                                    url="https://my.url/api",
                                    client=client)
        result = event_loop.run_until_complete(future)
        assert result == DogQuery(dog=Dog(name="Fred", bark_volume=8))

        request = client.request
        assert request.url == "https://my.url/api"
        assert request.method == "POST"
        assert json.loads(request.content.decode()) == {
            "query": quiz.gql(query)
        }
        assert request.headers == {"Content-Type": "application/json"}
Ejemplo n.º 4
0
    def test_success(self, event_loop):
        response = snug.Response(200, b'{"data": {"foo": 4, "bar": ""}}')
        client = MockAsyncClient(response)
        future = quiz.execute_async(
            "my query",
            url="https://my.url/api",
            auth=token_auth("foo"),
            client=client,
        )
        result = event_loop.run_until_complete(future)
        assert isinstance(result, quiz.RawResult)
        assert result == {"foo": 4, "bar": ""}
        assert len(result) == 2
        assert result["foo"] == 4
        assert set(result) == {"foo", "bar"}
        assert isinstance(result, Mapping)
        assert result.__metadata__ == quiz.QueryMetadata(
            response=response,
            request=snug.POST(
                "https://my.url/api",
                headers={"Content-Type": "application/json"},
                content=b'{"query": "my query"}',
            ),
        )

        request = client.request
        assert request.url == "https://my.url/api"
        assert request.method == "POST"
        assert json.loads(request.content.decode()) == {"query": "my query"}
        assert request.headers == {
            "Authorization": "token foo",
            "Content-Type": "application/json",
        }
Ejemplo n.º 5
0
    def test_success(self, event_loop):
        client = MockClient(snug.Response(200, b'{"data": {"foo": 4}}'))
        future = quiz.execute_async('my query',
                                    url='https://my.url/api',
                                    auth=token_auth('foo'),
                                    client=client)
        result = event_loop.run_until_complete(future)
        assert result == {'foo': 4}

        request = client.request
        assert request.url == 'https://my.url/api'
        assert request.method == 'POST'
        assert json.loads(request.content.decode()) == {'query': 'my query'}
        assert request.headers == {
            'Authorization': 'token foo',
            'Content-Type': 'application/json'
        }
Ejemplo n.º 6
0
    def test_errors(self, event_loop):
        client = MockClient(
            snug.Response(
                200,
                json.dumps({
                    'data': {
                        'foo': 4
                    },
                    'errors': [{
                        'message': 'foo'
                    }]
                }).encode()))
        future = quiz.execute_async('my query',
                                    url='https://my.url/api',
                                    client=client,
                                    auth=token_auth('foo'))
        with pytest.raises(quiz.ErrorResponse) as exc:
            event_loop.run_until_complete(future)

        assert exc.value == quiz.ErrorResponse({'foo': 4}, [{
            'message': 'foo'
        }])
Ejemplo n.º 7
0
def execute_async(obj, auth, url=URL, **kwargs):
    return quiz.execute_async(obj, auth=auth_factory(auth), url=url, **kwargs)