Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leszekhanusz committed Sep 9, 2023
1 parent 9fd57a4 commit c310552
Showing 1 changed file with 158 additions and 1 deletion.
159 changes: 158 additions & 1 deletion tests/test_requests_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@
'{"code":"SA","name":"South America"}]}}]'
)

query1_server_answer_twice_list = (
"["
'{"data":{"continents":['
'{"code":"AF","name":"Africa"},{"code":"AN","name":"Antarctica"},'
'{"code":"AS","name":"Asia"},{"code":"EU","name":"Europe"},'
'{"code":"NA","name":"North America"},{"code":"OC","name":"Oceania"},'
'{"code":"SA","name":"South America"}]}},'
'{"data":{"continents":['
'{"code":"AF","name":"Africa"},{"code":"AN","name":"Antarctica"},'
'{"code":"AS","name":"Asia"},{"code":"EU","name":"Europe"},'
'{"code":"NA","name":"North America"},{"code":"OC","name":"Oceania"},'
'{"code":"SA","name":"South America"}]}}'
"]"
)


@pytest.mark.aiohttp
@pytest.mark.asyncio
Expand Down Expand Up @@ -74,6 +89,108 @@ def test_code():
await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_query_auto_batch_enabled(
event_loop, aiohttp_server, run_sync_test
):
from aiohttp import web
from gql.transport.requests import RequestsHTTPTransport

async def handler(request):
return web.Response(
text=query1_server_answer_list,
content_type="application/json",
headers={"dummy": "test1234"},
)

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

url = server.make_url("/")

def test_code():
transport = RequestsHTTPTransport(url=url)

with Client(
transport=transport,
batch_interval=0.01,
) as session:

query = gql(query1_str)

# Execute query synchronously
result = session.execute(query)

continents = result["continents"]

africa = continents[0]

assert africa["code"] == "AF"

# Checking response headers are saved in the transport
assert hasattr(transport, "response_headers")
assert isinstance(transport.response_headers, Mapping)
assert transport.response_headers["dummy"] == "test1234"

await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_query_auto_batch_enabled_two_requests(
event_loop, aiohttp_server, run_sync_test
):
from aiohttp import web
from gql.transport.requests import RequestsHTTPTransport
from threading import Thread

async def handler(request):
return web.Response(
text=query1_server_answer_twice_list,
content_type="application/json",
headers={"dummy": "test1234"},
)

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

url = server.make_url("/")

def test_code():
transport = RequestsHTTPTransport(url=url)

with Client(
transport=transport,
batch_interval=0.01,
) as session:

def test_thread():
query = gql(query1_str)

# Execute query synchronously
result = session.execute(query)

continents = result["continents"]

africa = continents[0]

assert africa["code"] == "AF"

# Checking response headers are saved in the transport
assert hasattr(transport, "response_headers")
assert isinstance(transport.response_headers, Mapping)
assert transport.response_headers["dummy"] == "test1234"

for _ in range(2):
thread = Thread(target=test_thread)
thread.start()

await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_cookies(event_loop, aiohttp_server, run_sync_test):
Expand Down Expand Up @@ -148,6 +265,46 @@ def test_code():
await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_error_code_401_auto_batch_enabled(
event_loop, aiohttp_server, run_sync_test
):
from aiohttp import web
from gql.transport.requests import RequestsHTTPTransport

async def handler(request):
# Will generate http error code 401
return web.Response(
text='{"error":"Unauthorized","message":"401 Client Error: Unauthorized"}',
content_type="application/json",
status=401,
)

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

url = server.make_url("/")

def test_code():
transport = RequestsHTTPTransport(url=url)

with Client(
transport=transport,
batch_interval=0.01,
) as session:

query = gql(query1_str)

with pytest.raises(TransportServerError) as exc_info:
session.execute(query)

assert "401 Client Error: Unauthorized" in str(exc_info.value)

await run_sync_test(event_loop, server, test_code)


@pytest.mark.aiohttp
@pytest.mark.asyncio
async def test_requests_error_code_429(event_loop, aiohttp_server, run_sync_test):
Expand Down Expand Up @@ -425,7 +582,7 @@ def get_continent_name(session, continent_code):
)
thread.start()

# Doing it twice to check that everything is closing correctly
# Doing it twice to check that everything is closing and reconnecting correctly
with client as session:

for continent_code in continent_codes:
Expand Down

0 comments on commit c310552

Please sign in to comment.