Skip to content

Commit 7bf3638

Browse files
committed
Add tests for deprecation warnings
1 parent 3d01a58 commit 7bf3638

File tree

3 files changed

+135
-13
lines changed

3 files changed

+135
-13
lines changed

gql/graphql_request.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,13 @@ def __init__(
4141

4242
if isinstance(request, GraphQLRequest):
4343
self.document = request.document
44-
self.variable_values: Optional[Dict[str, Any]] = (
45-
request.variable_values
46-
if request.variable_values is not None
47-
else variable_values
48-
)
49-
self.operation_name: Optional[str] = (
50-
request.operation_name
51-
if request.operation_name is not None
52-
else operation_name
53-
)
54-
else:
55-
self.variable_values = variable_values
56-
self.operation_name = operation_name
44+
if variable_values is None:
45+
variable_values = request.variable_values
46+
if operation_name is None:
47+
operation_name = request.operation_name
48+
49+
self.variable_values: Optional[Dict[str, Any]] = variable_values
50+
self.operation_name: Optional[str] = operation_name
5751

5852
def serialize_variable_values(self, schema: GraphQLSchema) -> "GraphQLRequest":
5953

tests/test_aiohttp.py

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,3 +1824,108 @@ async def handler(request):
18241824
assert africa["code"] == "AF"
18251825

18261826
await connector.close()
1827+
1828+
1829+
@pytest.mark.asyncio
1830+
async def test_aiohttp_deprecation_warning_using_document_node_execute(aiohttp_server):
1831+
from aiohttp import web
1832+
1833+
from gql.transport.aiohttp import AIOHTTPTransport
1834+
1835+
async def handler(request):
1836+
return web.Response(
1837+
text=query1_server_answer,
1838+
content_type="application/json",
1839+
)
1840+
1841+
app = web.Application()
1842+
app.router.add_route("POST", "/", handler)
1843+
server = await aiohttp_server(app)
1844+
1845+
url = server.make_url("/")
1846+
1847+
transport = AIOHTTPTransport(url=url, timeout=10)
1848+
1849+
async with Client(transport=transport) as session:
1850+
1851+
query = gql(query1_str)
1852+
1853+
with pytest.warns(
1854+
DeprecationWarning,
1855+
match="Using a DocumentNode is deprecated",
1856+
):
1857+
result = await session.execute(query.document)
1858+
1859+
continents = result["continents"]
1860+
1861+
africa = continents[0]
1862+
1863+
assert africa["code"] == "AF"
1864+
1865+
1866+
@pytest.mark.asyncio
1867+
async def test_aiohttp_deprecation_warning_execute_variable_values(aiohttp_server):
1868+
from aiohttp import web
1869+
1870+
from gql.transport.aiohttp import AIOHTTPTransport
1871+
1872+
async def handler(request):
1873+
return web.Response(text=query2_server_answer, content_type="application/json")
1874+
1875+
app = web.Application()
1876+
app.router.add_route("POST", "/", handler)
1877+
server = await aiohttp_server(app)
1878+
1879+
url = server.make_url("/")
1880+
1881+
transport = AIOHTTPTransport(url=url, timeout=10)
1882+
1883+
async with Client(transport=transport) as session:
1884+
1885+
params = {"code": "EU"}
1886+
1887+
query = gql(query2_str)
1888+
1889+
with pytest.warns(
1890+
DeprecationWarning,
1891+
match=(
1892+
"Using variable_values and operation_name arguments of "
1893+
"execute and subscribe methods is deprecated"
1894+
),
1895+
):
1896+
result = await session.execute(
1897+
query, variable_values=params, operation_name="getEurope"
1898+
)
1899+
1900+
continent = result["continent"]
1901+
1902+
assert continent["name"] == "Europe"
1903+
1904+
1905+
@pytest.mark.asyncio
1906+
async def test_aiohttp_type_error_execute(aiohttp_server):
1907+
from aiohttp import web
1908+
1909+
from gql.transport.aiohttp import AIOHTTPTransport
1910+
1911+
async def handler(request):
1912+
return web.Response(text=query2_server_answer, content_type="application/json")
1913+
1914+
app = web.Application()
1915+
app.router.add_route("POST", "/", handler)
1916+
server = await aiohttp_server(app)
1917+
1918+
url = server.make_url("/")
1919+
1920+
transport = AIOHTTPTransport(url=url, timeout=10)
1921+
1922+
async with Client(transport=transport) as session:
1923+
1924+
params = {"code": "EU"}
1925+
1926+
query = gql(query2_str)
1927+
1928+
with pytest.raises(TypeError) as exc_info:
1929+
await session.execute("qmlsdkfj")
1930+
1931+
assert "request should be a GraphQLRequest object" in str(exc_info.value)

tests/test_graphql_request.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,3 +210,26 @@ def test_graphql_request_using_string_instead_of_document():
210210
print(request)
211211

212212
assert str(request) == strip_braces_spaces(expected_payload)
213+
214+
215+
def test_graphql_request_init_with_graphql_request():
216+
money_value_1 = Money(10, "DM")
217+
money_value_2 = Money(20, "DM")
218+
219+
request_1 = GraphQLRequest(
220+
"query myquery($money: Money) {toEuros(money: $money)}",
221+
variable_values={"money": money_value_1},
222+
)
223+
request_2 = GraphQLRequest(
224+
request_1,
225+
)
226+
request_3 = GraphQLRequest(
227+
request_1,
228+
variable_values={"money": money_value_2},
229+
)
230+
231+
assert request_1.document == request_2.document
232+
assert request_2.document == request_3.document
233+
assert request_1.variable_values["money"] == money_value_1
234+
assert request_2.variable_values["money"] == money_value_1
235+
assert request_3.variable_values["money"] == money_value_2

0 commit comments

Comments
 (0)