forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_appsync_resolvers.py
108 lines (83 loc) · 3.04 KB
/
test_appsync_resolvers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import json
import pytest
from requests import Request
from tests.e2e.utils import data_fetcher
@pytest.fixture
def appsync_endpoint(infrastructure: dict) -> str:
return infrastructure["GraphQLHTTPUrl"]
@pytest.fixture
def appsync_access_key(infrastructure: dict) -> str:
return infrastructure["GraphQLAPIKey"]
@pytest.mark.xdist_group(name="event_handler")
def test_appsync_get_all_posts(appsync_endpoint, appsync_access_key):
# GIVEN
body = {
"query": "query MyQuery { allPosts { post_id }}",
"variables": None,
"operationName": "MyQuery",
}
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=appsync_endpoint,
json=body,
headers={"x-api-key": appsync_access_key, "Content-Type": "application/json"},
)
)
# THEN expect a HTTP 200 response and content return list of Posts
assert response.status_code == 200
assert response.content is not None
data = json.loads(response.content.decode("ascii"))["data"]
assert data["allPosts"] is not None
assert len(data["allPosts"]) > 0
@pytest.mark.xdist_group(name="event_handler")
def test_appsync_get_post(appsync_endpoint, appsync_access_key):
# GIVEN
post_id = "1"
body = {
"query": f'query MyQuery {{ getPost(post_id: "{post_id}") {{ post_id }} }}',
"variables": None,
"operationName": "MyQuery",
}
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=appsync_endpoint,
json=body,
headers={"x-api-key": appsync_access_key, "Content-Type": "application/json"},
)
)
# THEN expect a HTTP 200 response and content return Post id
assert response.status_code == 200
assert response.content is not None
data = json.loads(response.content.decode("ascii"))["data"]
assert data["getPost"]["post_id"] == post_id
@pytest.mark.xdist_group(name="event_handler")
def test_appsync_get_related_posts_batch(appsync_endpoint, appsync_access_key):
# GIVEN
post_id = "2"
related_posts_ids = ["3", "5"]
body = {
"query": f'query MyQuery {{ getPost(post_id: "{post_id}") {{ post_id relatedPosts {{ post_id }} }} }}',
"variables": None,
"operationName": "MyQuery",
}
# WHEN
response = data_fetcher.get_http_response(
Request(
method="POST",
url=appsync_endpoint,
json=body,
headers={"x-api-key": appsync_access_key, "Content-Type": "application/json"},
)
)
# THEN expect a HTTP 200 response and content return Post id with dependent Posts id's
assert response.status_code == 200
assert response.content is not None
data = json.loads(response.content.decode("ascii"))["data"]
assert data["getPost"]["post_id"] == post_id
assert len(data["getPost"]["relatedPosts"]) == len(related_posts_ids)
for post in data["getPost"]["relatedPosts"]:
assert post["post_id"] in related_posts_ids