Skip to content

Commit 022308c

Browse files
committed
Test queries with variable values
1 parent 3c0bb27 commit 022308c

File tree

5 files changed

+195
-107
lines changed

5 files changed

+195
-107
lines changed

tests/fixtures/vcr_cassettes/client.yaml

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ interactions:
44
headers:
55
Accept:
66
- text/html
7-
Accept-Encoding:
8-
- gzip, deflate
9-
Connection:
10-
- keep-alive
117
Host:
128
- swapi.graphene-python.org
139
User-Agent:
14-
- python-requests/2.22.0
10+
- python-requests/2.23.0
1511
method: GET
1612
uri: http://127.0.0.1:8000/graphql
1713
response:
@@ -68,39 +64,33 @@ interactions:
6864
- '3808'
6965
Content-Type:
7066
- text/html; charset=utf-8
71-
Date:
72-
- Tue, 03 Dec 2019 08:22:54 GMT
73-
Server:
74-
- WSGIServer/0.1 Python/2.7.16
7567
Set-Cookie:
7668
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz;
77-
expires=Tue, 01-Dec-2020 08:22:54 GMT; Max-Age=31449600; Path=/
78-
Vary:
79-
- Cookie
80-
X-Frame-Options:
81-
- SAMEORIGIN
69+
Max-Age=31449600; Path=/
8270
status:
8371
code: 200
8472
message: OK
8573
- request:
86-
body: query=query+IntrospectionQuery+%7B%0A++__schema+%7B%0A++++queryType+%7B%0A++++++name%0A++++%7D%0A++++mutationType+%7B%0A++++++name%0A++++%7D%0A++++subscriptionType+%7B%0A++++++name%0A++++%7D%0A++++types+%7B%0A++++++...FullType%0A++++%7D%0A++++directives+%7B%0A++++++name%0A++++++description%0A++++++locations%0A++++++args+%7B%0A++++++++...InputValue%0A++++++%7D%0A++++%7D%0A++%7D%0A%7D%0A%0Afragment+FullType+on+__Type+%7B%0A++kind%0A++name%0A++description%0A++fields%28includeDeprecated%3A+true%29+%7B%0A++++name%0A++++description%0A++++args+%7B%0A++++++...InputValue%0A++++%7D%0A++++type+%7B%0A++++++...TypeRef%0A++++%7D%0A++++isDeprecated%0A++++deprecationReason%0A++%7D%0A++inputFields+%7B%0A++++...InputValue%0A++%7D%0A++interfaces+%7B%0A++++...TypeRef%0A++%7D%0A++enumValues%28includeDeprecated%3A+true%29+%7B%0A++++name%0A++++description%0A++++isDeprecated%0A++++deprecationReason%0A++%7D%0A++possibleTypes+%7B%0A++++...TypeRef%0A++%7D%0A%7D%0A%0Afragment+InputValue+on+__InputValue+%7B%0A++name%0A++description%0A++type+%7B%0A++++...TypeRef%0A++%7D%0A++defaultValue%0A%7D%0A%0Afragment+TypeRef+on+__Type+%7B%0A++kind%0A++name%0A++ofType+%7B%0A++++kind%0A++++name%0A++++ofType+%7B%0A++++++kind%0A++++++name%0A++++++ofType+%7B%0A++++++++kind%0A++++++++name%0A++++++++ofType+%7B%0A++++++++++kind%0A++++++++++name%0A++++++++++ofType+%7B%0A++++++++++++kind%0A++++++++++++name%0A++++++++++++ofType+%7B%0A++++++++++++++kind%0A++++++++++++++name%0A++++++++++++++ofType+%7B%0A++++++++++++++++kind%0A++++++++++++++++name%0A++++++++++++++%7D%0A++++++++++++%7D%0A++++++++++%7D%0A++++++++%7D%0A++++++%7D%0A++++%7D%0A++%7D%0A%7D%0A
74+
body: '{"query": "query IntrospectionQuery {\n __schema {\n queryType {\n name\n }\n mutationType
75+
{\n name\n }\n subscriptionType {\n name\n }\n types {\n ...FullType\n }\n directives
76+
{\n name\n description\n locations\n args {\n ...InputValue\n }\n }\n }\n}\n\nfragment
77+
FullType on __Type {\n kind\n name\n description\n fields(includeDeprecated:
78+
true) {\n name\n description\n args {\n ...InputValue\n }\n type
79+
{\n ...TypeRef\n }\n isDeprecated\n deprecationReason\n }\n inputFields
80+
{\n ...InputValue\n }\n interfaces {\n ...TypeRef\n }\n enumValues(includeDeprecated:
81+
true) {\n name\n description\n isDeprecated\n deprecationReason\n }\n possibleTypes
82+
{\n ...TypeRef\n }\n}\n\nfragment InputValue on __InputValue {\n name\n description\n type
83+
{\n ...TypeRef\n }\n defaultValue\n}\n\nfragment TypeRef on __Type {\n kind\n name\n ofType
84+
{\n kind\n name\n ofType {\n kind\n name\n ofType {\n kind\n name\n ofType
85+
{\n kind\n name\n ofType {\n kind\n name\n ofType
86+
{\n kind\n name\n ofType {\n kind\n name\n }\n }\n }\n }\n }\n }\n }\n}\n"}'
8787
headers:
8888
Accept:
8989
- '*/*'
90-
Accept-Encoding:
91-
- gzip, deflate
92-
Connection:
93-
- keep-alive
94-
Content-Length:
95-
- '1625'
9690
Content-Type:
97-
- application/x-www-form-urlencoded
98-
Cookie:
99-
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
91+
- application/json
10092
User-Agent:
101-
- python-requests/2.22.0
102-
x-csrftoken:
103-
- hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
93+
- python-requests/2.23.0
10494
method: POST
10595
uri: http://127.0.0.1:8000/graphql
10696
response:
@@ -113,7 +103,7 @@ interactions:
113103
ID of the object","args":[{"name":"id","description":null,"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"ID","ofType":null}},"defaultValue":null}],"type":{"kind":"OBJECT","name":"Planet","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"starship","description":"The
114104
ID of the object","args":[{"name":"id","description":null,"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"ID","ofType":null}},"defaultValue":null}],"type":{"kind":"OBJECT","name":"Starship","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"hero","description":"The
115105
ID of the object","args":[{"name":"id","description":null,"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"ID","ofType":null}},"defaultValue":null}],"type":{"kind":"OBJECT","name":"Hero","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"node","description":"The
116-
ID of the object","args":[{"name":"id","description":null,"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"ID","ofType":null}},"defaultValue":null}],"type":{"kind":"INTERFACE","name":"Node","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"viewer","description":null,"args":[],"type":{"kind":"OBJECT","name":"Query","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"__debug","description":null,"args":[],"type":{"kind":"OBJECT","name":"DjangoDebug","ofType":null},"isDeprecated":false,"deprecationReason":null}],"inputFields":null,"interfaces":[],"enumValues":null,"possibleTypes":null},{"kind":"OBJECT","name":"FilmConnection","description":null,"fields":[{"name":"pageInfo","description":"Pagination
106+
ID of the object","args":[{"name":"id","description":null,"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"ID","ofType":null}},"defaultValue":null}],"type":{"kind":"INTERFACE","name":"Node","ofType":null},"isDeprecated":false,"deprecationReason":null},{"name":"viewer","description":null,"args":[],"type":{"kind":"OBJECT","name":"Query","ofType":null},"isDeprecated":false,"deprecationReason":null}],"inputFields":null,"interfaces":[],"enumValues":null,"possibleTypes":null},{"kind":"OBJECT","name":"FilmConnection","description":null,"fields":[{"name":"pageInfo","description":"Pagination
117107
data for this connection.","args":[],"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"OBJECT","name":"PageInfo","ofType":null}},"isDeprecated":false,"deprecationReason":null},{"name":"edges","description":"Contains
118108
the nodes in this connection.","args":[],"type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"LIST","name":null,"ofType":{"kind":"OBJECT","name":"FilmEdge","ofType":null}}},"isDeprecated":false,"deprecationReason":null},{"name":"totalCount","description":null,"args":[],"type":{"kind":"SCALAR","name":"Int","ofType":null},"isDeprecated":false,"deprecationReason":null}],"inputFields":null,"interfaces":[],"enumValues":null,"possibleTypes":null},{"kind":"OBJECT","name":"PageInfo","description":"The
119109
Relay compliant `PageInfo` type, containing data necessary to paginate this
@@ -254,20 +244,9 @@ interactions:
254244
when true.","type":{"kind":"NON_NULL","name":null,"ofType":{"kind":"SCALAR","name":"Boolean","ofType":null}},"defaultValue":null}]}]}}}'
255245
headers:
256246
Content-Length:
257-
- '69554'
247+
- '69400'
258248
Content-Type:
259249
- application/json
260-
Date:
261-
- Tue, 03 Dec 2019 08:22:54 GMT
262-
Server:
263-
- WSGIServer/0.1 Python/2.7.16
264-
Set-Cookie:
265-
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz;
266-
expires=Tue, 01-Dec-2020 08:22:54 GMT; Max-Age=31449600; Path=/
267-
Vary:
268-
- Cookie
269-
X-Frame-Options:
270-
- SAMEORIGIN
271250
status:
272251
code: 200
273252
message: OK

tests/fixtures/vcr_cassettes/execute.yaml

Lines changed: 0 additions & 27 deletions
This file was deleted.

tests/fixtures/vcr_cassettes/execute2.yaml

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
interactions:
2+
- request:
3+
body: '{"query": "{\n myFavoriteFilm: film(id: \"RmlsbToz\") {\n id\n title\n episodeId\n characters(first:
4+
5) {\n edges {\n node {\n name\n }\n }\n }\n }\n}\n"}'
5+
headers:
6+
Accept:
7+
- '*/*'
8+
Accept-Encoding:
9+
- gzip, deflate
10+
Connection:
11+
- keep-alive
12+
Content-Length:
13+
- '204'
14+
Content-Type:
15+
- application/json
16+
Cookie:
17+
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
18+
User-Agent:
19+
- python-requests/2.23.0
20+
x-csrftoken:
21+
- hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
22+
method: POST
23+
uri: http://127.0.0.1:8000/graphql
24+
response:
25+
body:
26+
string: '{"data":{"myFavoriteFilm":{"id":"RmlsbToz","title":"Return of the Jedi","episodeId":6,"characters":{"edges":[{"node":{"name":"Luke
27+
Skywalker"}},{"node":{"name":"C-3PO"}},{"node":{"name":"R2-D2"}},{"node":{"name":"Darth
28+
Vader"}},{"node":{"name":"Leia Organa"}}]}}}}'
29+
headers:
30+
Content-Length:
31+
- '264'
32+
Content-Type:
33+
- application/json
34+
Date:
35+
- Sat, 16 May 2020 15:28:52 GMT
36+
Server:
37+
- WSGIServer/0.2 CPython/3.8.3
38+
Set-Cookie:
39+
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz;
40+
expires=Sat, 15 May 2021 15:28:52 GMT; Max-Age=31449600; Path=/; SameSite=Lax
41+
Vary:
42+
- Cookie
43+
X-Content-Type-Options:
44+
- nosniff
45+
X-Frame-Options:
46+
- DENY
47+
status:
48+
code: 200
49+
message: OK
50+
- request:
51+
body: '{"query": "query Planet($id: ID!) {\n planet(id: $id) {\n id\n name\n }\n}\n",
52+
"variables": {"id": "UGxhbmV0OjEw"}}'
53+
headers:
54+
Accept:
55+
- '*/*'
56+
Accept-Encoding:
57+
- gzip, deflate
58+
Connection:
59+
- keep-alive
60+
Content-Length:
61+
- '123'
62+
Content-Type:
63+
- application/json
64+
Cookie:
65+
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
66+
User-Agent:
67+
- python-requests/2.23.0
68+
x-csrftoken:
69+
- hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
70+
method: POST
71+
uri: http://127.0.0.1:8000/graphql
72+
response:
73+
body:
74+
string: '{"data":{"planet":{"id":"UGxhbmV0OjEw","name":"Kamino"}}}'
75+
headers:
76+
Content-Length:
77+
- '57'
78+
Content-Type:
79+
- application/json
80+
Date:
81+
- Sat, 16 May 2020 15:28:52 GMT
82+
Server:
83+
- WSGIServer/0.2 CPython/3.8.3
84+
Set-Cookie:
85+
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz;
86+
expires=Sat, 15 May 2021 15:28:52 GMT; Max-Age=31449600; Path=/; SameSite=Lax
87+
Vary:
88+
- Cookie
89+
X-Content-Type-Options:
90+
- nosniff
91+
X-Frame-Options:
92+
- DENY
93+
status:
94+
code: 200
95+
message: OK
96+
- request:
97+
body: '{"query": "query Planet1 {\n planet(id: \"UGxhbmV0OjEw\") {\n id\n name\n }\n}\n\nquery
98+
Planet2 {\n planet(id: \"UGxhbmV0OjEx\") {\n id\n name\n }\n}\n", "operationName":
99+
"Planet2"}'
100+
headers:
101+
Accept:
102+
- '*/*'
103+
Accept-Encoding:
104+
- gzip, deflate
105+
Connection:
106+
- keep-alive
107+
Content-Length:
108+
- '197'
109+
Content-Type:
110+
- application/json
111+
Cookie:
112+
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
113+
User-Agent:
114+
- python-requests/2.23.0
115+
x-csrftoken:
116+
- hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz
117+
method: POST
118+
uri: http://127.0.0.1:8000/graphql
119+
response:
120+
body:
121+
string: '{"data":{"planet":{"id":"UGxhbmV0OjEx","name":"Geonosis"}}}'
122+
headers:
123+
Content-Length:
124+
- '59'
125+
Content-Type:
126+
- application/json
127+
Date:
128+
- Sat, 16 May 2020 15:28:52 GMT
129+
Server:
130+
- WSGIServer/0.2 CPython/3.8.3
131+
Set-Cookie:
132+
- csrftoken=hRIez34v4hg2Wbl8XhrbshvDIB3HmLR2L9WNTJ3SdrIQHxAKtoukxiuwQlwRJewz;
133+
expires=Sat, 15 May 2021 15:28:52 GMT; Max-Age=31449600; Path=/; SameSite=Lax
134+
Vary:
135+
- Cookie
136+
X-Content-Type-Options:
137+
- nosniff
138+
X-Frame-Options:
139+
- DENY
140+
status:
141+
code: 200
142+
message: OK
143+
version: 1

tests/test_transport.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@
1111
URL = "http://127.0.0.1:8000/graphql"
1212

1313

14-
def use_cassette(name):
14+
query_vcr = vcr.VCR(
15+
cassette_library_dir=os.path.join(
16+
os.path.dirname(__file__), "fixtures", "vcr_cassettes"
17+
),
18+
record_mode="new_episodes",
19+
match_on=["uri", "method", "body"],
20+
)
1521

16-
return vcr.use_cassette(
17-
os.path.join(
18-
os.path.dirname(__file__), "fixtures", "vcr_cassettes", name + ".yaml"
19-
),
20-
record_mod="new_episodes",
21-
)
22+
23+
def use_cassette(name):
24+
return query_vcr.use_cassette(name + ".yaml")
2225

2326

2427
@pytest.fixture
@@ -28,8 +31,7 @@ def client():
2831
URL, headers={"Host": "swapi.graphene-python.org", "Accept": "text/html"}
2932
)
3033
response.raise_for_status()
31-
csrf = response.cookies["csrftoken"]
32-
34+
csrf = response.cookies.get("csrftoken")
3335
return Client(
3436
transport=RequestsHTTPTransport(
3537
url=URL, cookies={"csrftoken": csrf}, headers={"x-csrftoken": csrf}
@@ -73,12 +75,29 @@ def test_hero_name_query(client):
7375
},
7476
}
7577
}
76-
with use_cassette("execute"):
78+
with use_cassette("queries"):
7779
result = client.execute(query)
78-
assert result == expected
80+
assert result == expected
81+
82+
83+
def test_query_with_variable(client):
84+
query = gql(
85+
"""
86+
query Planet($id: ID!) {
87+
planet(id: $id) {
88+
id
89+
name
90+
}
91+
}
92+
"""
93+
)
94+
expected = {"planet": {"id": "UGxhbmV0OjEw", "name": "Kamino"}}
95+
with use_cassette("queries"):
96+
result = client.execute(query, variable_values={"id": "UGxhbmV0OjEw"})
97+
assert result == expected
7998

8099

81-
def test_planet_names_query(client):
100+
def test_planet_query_with_name(client):
82101
query = gql(
83102
"""
84103
query Planet1 {
@@ -96,6 +115,6 @@ def test_planet_names_query(client):
96115
"""
97116
)
98117
expected = {"planet": {"id": "UGxhbmV0OjEx", "name": "Geonosis"}}
99-
with use_cassette("execute2"):
118+
with use_cassette("queries"):
100119
result = client.execute(query, operation_name="Planet2")
101-
assert result == expected
120+
assert result == expected

0 commit comments

Comments
 (0)