Skip to content

Commit 9daac5c

Browse files
committed
Adding new test to have 100% code coverage now that aiohttp transport supports subscriptions
1 parent 057ec8a commit 9daac5c

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

tests/test_httpx_async.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,3 +1447,62 @@ async def handler(request):
14471447
pi = result["pi"]
14481448

14491449
assert pi == Decimal("3.141592653589793238462643383279502884197")
1450+
1451+
1452+
@pytest.mark.aiohttp
1453+
@pytest.mark.asyncio
1454+
async def test_httpx_subscribe_not_supported_cli(aiohttp_server):
1455+
"""Test that the CLI falls back to execute when subscribe is not supported."""
1456+
from aiohttp import web
1457+
1458+
from gql.transport.httpx import HTTPXAsyncTransport
1459+
1460+
async def handler(request):
1461+
return web.Response(text=query1_server_answer, content_type="application/json")
1462+
1463+
app = web.Application()
1464+
app.router.add_route("POST", "/", handler)
1465+
server = await aiohttp_server(app)
1466+
1467+
url = str(server.make_url("/"))
1468+
1469+
transport = HTTPXAsyncTransport(url=url)
1470+
1471+
async with Client(transport=transport) as _:
1472+
1473+
# Define arguments for the CLI
1474+
# We use the query "query getContinents..."
1475+
import io
1476+
import sys
1477+
from io import StringIO
1478+
1479+
from gql import cli
1480+
1481+
test_args = ["gql-cli", url, "--transport", "httpx"]
1482+
1483+
# Mock sys.stdin to provide the query
1484+
sys.stdin = io.StringIO(query1_str)
1485+
1486+
# Capture stdout
1487+
captured_output = StringIO()
1488+
original_stdout = sys.stdout
1489+
sys.stdout = captured_output
1490+
1491+
try:
1492+
# We need to mock sys.argv as well because cli.get_parser() uses
1493+
# usage from sys.argv[0] sometimes,
1494+
# but mainly passing args to parse_args is cleaner.
1495+
parser = cli.get_parser()
1496+
parsed_args = parser.parse_args(test_args[1:]) # skip prog name
1497+
1498+
exit_code = await cli.main(parsed_args)
1499+
assert exit_code == 0
1500+
1501+
except SystemExit:
1502+
pass
1503+
finally:
1504+
sys.stdout = original_stdout
1505+
sys.stdin = sys.__stdin__ # Restore stdin
1506+
1507+
output = captured_output.getvalue()
1508+
assert "Africa" in output

0 commit comments

Comments
 (0)