Skip to content

Commit 6b7965a

Browse files
committed
more aiohttp tests
1 parent 60717ca commit 6b7965a

File tree

1 file changed

+92
-4
lines changed

1 file changed

+92
-4
lines changed

tests/integrations/aiohttp/test_aiohttp.py

Lines changed: 92 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
import mock # python < 3.3
1919

2020

21-
def requires_python_version(major, minor, reason=None):
21+
def min_python_version(major, minor, reason=None):
2222
if reason is None:
23-
reason = "Requires Python {}.{}".format(major, minor)
23+
reason = "Requires Python {}.{} or higher".format(major, minor)
2424
return pytest.mark.skipif(sys.version_info < (major, minor), reason=reason)
2525

2626

27+
def max_python_version(major, minor, reason=None):
28+
if reason is None:
29+
reason = "Requires Python {}.{} or lower".format(major, minor)
30+
return pytest.mark.skipif(sys.version_info > (major, minor), reason=reason)
31+
32+
2733
@pytest.mark.asyncio
2834
async def test_basic(sentry_init, aiohttp_client, capture_events):
2935
sentry_init(integrations=[AioHttpIntegration()])
@@ -544,7 +550,7 @@ async def handler(request):
544550
)
545551

546552

547-
@requires_python_version(3, 8, "GraphQL aiohttp integration requires py>=3.8")
553+
@min_python_version(3, 8, "GraphQL aiohttp integration requires py>=3.8")
548554
@pytest.mark.asyncio
549555
async def test_graphql_get_client_error_captured(
550556
sentry_init, capture_events, aiohttp_raw_server, aiohttp_client
@@ -594,7 +600,7 @@ async def handler(request):
594600
)
595601

596602

597-
@requires_python_version(3, 8, "GraphQL aiohttp integration requires py>=3.8")
603+
@min_python_version(3, 8, "GraphQL aiohttp integration requires py>=3.8")
598604
@pytest.mark.asyncio
599605
async def test_graphql_post_client_error_captured(
600606
sentry_init, capture_events, aiohttp_client, aiohttp_raw_server
@@ -654,3 +660,85 @@ async def handler(request):
654660
event["exception"]["values"][0]["value"]
655661
== "GraphQL request failed, name: AddPet, type: mutation"
656662
)
663+
664+
665+
@max_python_version(3, 7, "No GraphQL aiohttp integration for py<=3.7")
666+
@pytest.mark.asyncio
667+
async def test_graphql_get_client_error_not_captured(
668+
sentry_init, capture_events, aiohttp_raw_server, aiohttp_client
669+
):
670+
"""Test that firing GraphQL requests works, there will just be no event."""
671+
sentry_init(integrations=[AioHttpIntegration()])
672+
673+
graphql_response = {
674+
"data": None,
675+
"errors": [
676+
{
677+
"message": "some error",
678+
"locations": [{"line": 2, "column": 3}],
679+
"path": ["pet"],
680+
}
681+
],
682+
}
683+
684+
async def handler(request):
685+
return json_response(graphql_response)
686+
687+
raw_server = await aiohttp_raw_server(handler)
688+
events = capture_events()
689+
690+
client = await aiohttp_client(raw_server)
691+
response = await client.get(
692+
"/graphql", params={"query": "query GetPet {pet{name}}"}
693+
)
694+
695+
assert response.status == 200
696+
assert await response.json() == graphql_response
697+
assert not events
698+
699+
700+
@max_python_version(3, 7, "No GraphQL aiohttp integration for py<=3.7")
701+
@pytest.mark.asyncio
702+
async def test_graphql_post_client_error_not_captured(
703+
sentry_init, capture_events, aiohttp_client, aiohttp_raw_server
704+
):
705+
"""Test that firing GraphQL requests works, there will just be no event."""
706+
sentry_init(integrations=[AioHttpIntegration()])
707+
708+
graphql_request = {
709+
"query": dedent(
710+
"""
711+
mutation AddPet ($name: String!) {
712+
addPet(name: $name) {
713+
id
714+
name
715+
}
716+
}
717+
"""
718+
),
719+
"variables": {
720+
"name": "Lucy",
721+
},
722+
}
723+
graphql_response = {
724+
"data": None,
725+
"errors": [
726+
{
727+
"message": "already have too many pets",
728+
"locations": [{"line": 1, "column": 1}],
729+
}
730+
],
731+
}
732+
733+
async def handler(request):
734+
return json_response(graphql_response)
735+
736+
raw_server = await aiohttp_raw_server(handler)
737+
events = capture_events()
738+
739+
client = await aiohttp_client(raw_server)
740+
response = await client.post("/graphql", json=graphql_request)
741+
742+
assert response.status == 200
743+
assert await response.json() == graphql_response
744+
assert not events

0 commit comments

Comments
 (0)