Example #1
0
async def test_websocket_using_cli_invalid_query(
    event_loop, server, monkeypatch, capsys
):

    url = f"ws://{server.hostname}:{server.port}/graphql"
    print(f"url = {url}")

    from gql.cli import main, get_parser
    import io

    parser = get_parser(with_examples=True)
    args = parser.parse_args([url])

    # Monkeypatching sys.stdin to simulate getting the query
    # via the standard input
    monkeypatch.setattr("sys.stdin", io.StringIO(invalid_query_str))

    # Flush captured output
    captured = capsys.readouterr()

    await main(args)

    # Check that the error has been printed on stdout
    captured = capsys.readouterr()
    captured_err = str(captured.err).strip()
    print(f"Captured: {captured_err}")

    expected_error = 'Cannot query field "bloh" on type "Continent"'

    assert expected_error in captured_err
Example #2
0
async def test_gql_cli_print_schema(event_loop, aiohttp_server, capsys):

    from gql.cli import get_parser, main

    server = await make_money_backend(aiohttp_server)

    url = str(server.make_url("/"))

    parser = get_parser(with_examples=True)
    args = parser.parse_args([url, "--print-schema"])

    exit_code = await main(args)

    assert exit_code == 0

    # Check that the result has been printed on stdout
    captured = capsys.readouterr()
    captured_out = str(captured.out).strip()

    print(captured_out)
    assert (
        """
type Subscription {
  spend(money: Money): Money
}
""".strip()
        in captured_out
    )
Example #3
0
async def test_aiohttp_using_cli_invalid_query(event_loop, aiohttp_server,
                                               monkeypatch, capsys):
    async def handler(request):
        return web.Response(text=query1_server_answer,
                            content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = str(server.make_url("/"))

    parser = get_parser(with_examples=True)
    args = parser.parse_args([url])

    # Send invalid query on standard input
    monkeypatch.setattr("sys.stdin", io.StringIO("BLAHBLAH"))

    exit_code = await main(args)

    assert exit_code == 1

    # Check that the error has been printed on stdout
    captured = capsys.readouterr()
    captured_err = str(captured.err).strip()
    print(f"Captured: {captured_err}")

    expected_error = "Syntax Error: Unexpected Name 'BLAHBLAH'"

    assert expected_error in captured_err
Example #4
0
async def test_aiohttp_using_cli_invalid_param(event_loop, aiohttp_server,
                                               monkeypatch, capsys):
    async def handler(request):
        return web.Response(text=query1_server_answer,
                            content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = str(server.make_url("/"))

    parser = get_parser(with_examples=True)
    args = parser.parse_args([url, "--variables", "invalid_param"])

    # Monkeypatching sys.stdin to simulate getting the query
    # via the standard input
    monkeypatch.setattr("sys.stdin", io.StringIO(query1_str))

    # Checking that sys.exit() is called
    with pytest.raises(SystemExit):
        await main(args)

    # Check that the error has been printed on stdout
    captured = capsys.readouterr()
    captured_err = str(captured.err).strip()
    print(f"Captured: {captured_err}")

    expected_error = "Error: Invalid variable: invalid_param"

    assert expected_error in captured_err
Example #5
0
async def test_aiohttp_using_cli(event_loop, aiohttp_server, monkeypatch,
                                 capsys):
    async def handler(request):
        return web.Response(text=query1_server_answer,
                            content_type="application/json")

    app = web.Application()
    app.router.add_route("POST", "/", handler)
    server = await aiohttp_server(app)

    url = str(server.make_url("/"))

    parser = get_parser(with_examples=True)
    args = parser.parse_args([url, "--verbose"])

    # Monkeypatching sys.stdin to simulate getting the query
    # via the standard input
    monkeypatch.setattr("sys.stdin", io.StringIO(query1_str))

    exit_code = await main(args)

    assert exit_code == 0

    # Check that the result has been printed on stdout
    captured = capsys.readouterr()
    captured_out = str(captured.out).strip()

    expected_answer = json.loads(query1_server_answer_data)
    print(f"Captured: {captured_out}")
    received_answer = json.loads(captured_out)

    assert received_answer == expected_answer
async def test_websocket_using_cli(event_loop, server, monkeypatch, capsys):

    url = f"ws://{server.hostname}:{server.port}/graphql"
    print(f"url = {url}")

    from gql.cli import main, get_parser
    import io
    import json

    parser = get_parser(with_examples=True)
    args = parser.parse_args([url])

    # Monkeypatching sys.stdin to simulate getting the query
    # via the standard input
    monkeypatch.setattr("sys.stdin", io.StringIO(query1_str))

    # Flush captured output
    captured = capsys.readouterr()

    exit_code = await main(args)

    assert exit_code == 0

    # Check that the result has been printed on stdout
    captured = capsys.readouterr()
    captured_out = str(captured.out).strip()

    expected_answer = json.loads(query1_server_answer_data)
    print(f"Captured: {captured_out}")
    received_answer = json.loads(captured_out)

    assert received_answer == expected_answer
Example #7
0
def parser():
    return get_parser()