Skip to content

Commit

Permalink
Fix deprecation warnings caused by tests (#3727)
Browse files Browse the repository at this point in the history
* Fix passing types to union is deprecated

* Fix `is_unset` deprecation warnings in tests

* Fix `asserts_errors` deprecation warnings in tests

* Fix `info.field_nodes` deprecation warnings

* Fix `graphiql` deprecation warnings in tests

* Fix `listen_to_channel` deprecation warnings

* Fix name-based matching of `value` deprecations

* Test directives still work as expected
  • Loading branch information
DoctorJohn authored Dec 15, 2024
1 parent a0573da commit 3ec03a7
Show file tree
Hide file tree
Showing 16 changed files with 113 additions and 60 deletions.
5 changes: 3 additions & 2 deletions tests/channels/test_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ async def test_no_layers():
"Check https://channels.readthedocs.io/en/stable/topics/channel_layers.html "
"for more information"
)
with pytest.raises(RuntimeError, match=msg):
await consumer.channel_listen("foobar").__anext__()
with pytest.deprecated_call(match="Use listen_to_channel instead"):
with pytest.raises(RuntimeError, match=msg):
await consumer.channel_listen("foobar").__anext__()

with pytest.raises(RuntimeError, match=msg):
async with consumer.listen_to_channel("foobar"):
Expand Down
2 changes: 1 addition & 1 deletion tests/fastapi/test_openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_disable_graphiql_view_and_allow_queries_via_get():
app = FastAPI()
schema = strawberry.Schema(query=Query)
graphql_app = GraphQLRouter[None, None](
schema, graphiql=False, allow_queries_via_get=False
schema, graphql_ide=None, allow_queries_via_get=False
)
app.include_router(graphql_app, prefix="/graphql")

Expand Down
4 changes: 3 additions & 1 deletion tests/federation/test_entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,9 @@ def resolve_reference(cls, info: Info, id: strawberry.ID) -> "Product":
exception = Exception("Foo bar")
exception.extensions = {"baz": "qux"}
raise located_error(
exception, nodes=info.field_nodes[0], path=["_entities_override", 0]
exception,
nodes=info._raw_info.field_nodes[0],
path=["_entities_override", 0],
)

@strawberry.federation.type(extend=True)
Expand Down
13 changes: 10 additions & 3 deletions tests/fields/test_arguments.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import warnings
from typing import List, Optional, Union
from typing_extensions import Annotated

Expand Down Expand Up @@ -488,11 +487,19 @@ def test_unset_deprecation_warning():


def test_deprecated_unset():
with pytest.deprecated_call():
warning = "`is_unset` is deprecated use `value is UNSET` instead"

with pytest.deprecated_call(match=warning):
from strawberry.types.unset import is_unset

with warnings.catch_warnings(record=False):
with pytest.deprecated_call(match=warning):
assert is_unset(UNSET)

with pytest.deprecated_call(match=warning):
assert not is_unset(None)

with pytest.deprecated_call(match=warning):
assert not is_unset(False)

with pytest.deprecated_call(match=warning):
assert not is_unset("hello world")
10 changes: 7 additions & 3 deletions tests/litestar/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ async def echo(
yield message

@strawberry.subscription
async def request_ping(self, info) -> typing.AsyncGenerator[bool, None]:
async def request_ping(
self, info: strawberry.Info
) -> typing.AsyncGenerator[bool, None]:
ws = info.context["ws"]
await ws.send_json(PingMessage().as_dict())
yield True
Expand All @@ -111,7 +113,7 @@ async def infinity(self, message: str) -> typing.AsyncGenerator[str, None]:
await asyncio.sleep(1)

@strawberry.subscription
async def context(self, info) -> typing.AsyncGenerator[str, None]:
async def context(self, info: strawberry.Info) -> typing.AsyncGenerator[str, None]:
yield info.context["custom_value"]

@strawberry.subscription
Expand All @@ -132,7 +134,9 @@ async def flavors(self) -> typing.AsyncGenerator[Flavor, None]:
yield Flavor.CHOCOLATE

@strawberry.subscription
async def debug(self, info) -> typing.AsyncGenerator[DebugInfo, None]:
async def debug(
self, info: strawberry.Info
) -> typing.AsyncGenerator[DebugInfo, None]:
active_result_handlers = [
task for task in info.context["get_tasks"]() if not task.done()
]
Expand Down
2 changes: 1 addition & 1 deletion tests/schema/test_arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ class Query:
@strawberry.field
def hello(
self,
info,
info: strawberry.Info,
input: Annotated[str, strawberry.argument(metadata={"test": "foo"})],
) -> str:
nonlocal field_definition
Expand Down
57 changes: 42 additions & 15 deletions tests/schema/test_directives.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def person(self) -> Person:
)

assert not result.errors
assert result.data
assert result.data["person"] == {"name": "Jess"}

query = """query ($skipPoints: Boolean!){
Expand All @@ -53,6 +54,7 @@ def person(self) -> Person:
result = schema.execute_sync(query, variable_values={"skipPoints": False})

assert not result.errors
assert result.data
assert result.data["person"] == {"name": "Jess", "points": 2000}


Expand Down Expand Up @@ -80,6 +82,7 @@ def person(self) -> Person:
result = await schema.execute(query, variable_values={"includePoints": False})

assert not result.errors
assert result.data
assert result.data["person"] == {"name": "Jess"}

query = """query ($skipPoints: Boolean!){
Expand All @@ -93,18 +96,21 @@ def person(self) -> Person:
result = await schema.execute(query, variable_values={"skipPoints": False})

assert not result.errors
assert result.data
assert result.data["person"] == {"name": "Jess", "points": 2000}


def test_can_declare_directives():
@strawberry.type
class Query:
cake: str = "made_in_switzerland"
@strawberry.field
def cake(self) -> str:
return "made_in_switzerland"

@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def uppercase(value: str, example: str):
def uppercase(value: DirectiveValue[str], example: str):
return value.upper()

schema = strawberry.Schema(query=Query, directives=[uppercase])
Expand All @@ -120,6 +126,10 @@ def uppercase(value: str, example: str):

assert schema.as_str() == textwrap.dedent(expected_schema).strip()

result = schema.execute_sync('query { cake @uppercase(example: "foo") }')
assert result.errors is None
assert result.data == {"cake": "MADE_IN_SWITZERLAND"}


def test_directive_arguments_without_value_param():
"""Regression test for Strawberry Issue #1666.
Expand Down Expand Up @@ -171,11 +181,11 @@ def person(self) -> Person:
@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def turn_uppercase(value: str):
def turn_uppercase(value: DirectiveValue[str]):
return value.upper()

@strawberry.directive(locations=[DirectiveLocation.FIELD])
def replace(value: str, old: str, new: str):
def replace(value: DirectiveValue[str], old: str, new: str):
return value.replace(old, new)

schema = strawberry.Schema(query=Query, directives=[turn_uppercase, replace])
Expand All @@ -195,6 +205,7 @@ def replace(value: str, old: str, new: str):
result = schema.execute_sync(query, variable_values={"identified": False})

assert not result.errors
assert result.data
assert result.data["person"]["name"] == "JESS"
assert result.data["jess"]["name"] == "Jessica"
assert result.data["johnDoe"].get("name") is None
Expand All @@ -214,11 +225,11 @@ def person(self) -> Person:
@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def turn_uppercase(value: str):
def turn_uppercase(value: DirectiveValue[str]):
return value.upper()

@strawberry.directive(locations=[DirectiveLocation.FIELD])
def replace(value: str, old: str, new: str):
def replace(value: DirectiveValue[str], old: str, new: str):
return value.replace(old, new)

schema = strawberry.Schema(
Expand All @@ -242,6 +253,7 @@ def replace(value: str, old: str, new: str):
result = schema.execute_sync(query, variable_values={"identified": False})

assert not result.errors
assert result.data
assert result.data["person"]["name"] == "JESS"
assert result.data["jess"]["name"] == "Jessica"
assert result.data["johnDoe"].get("name") is None
Expand All @@ -262,7 +274,7 @@ def person(self) -> Person:
@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
async def uppercase(value: str):
async def uppercase(value: DirectiveValue[str]):
return value.upper()

schema = strawberry.Schema(query=Query, directives=[uppercase])
Expand Down Expand Up @@ -293,7 +305,7 @@ def person(self) -> Person:
return Person()

@strawberry.directive(locations=[DirectiveLocation.FIELD])
def replace(value: str, old_list: List[str], new: str):
def replace(value: DirectiveValue[str], old_list: List[str], new: str):
for old in old_list:
value = value.replace(old, new)

Expand All @@ -310,6 +322,7 @@ def replace(value: str, old_list: List[str], new: str):
result = schema.execute_sync(query, variable_values={"identified": False})

assert not result.errors
assert result.data
assert result.data["person"]["name"] == "JESS"


Expand All @@ -327,7 +340,7 @@ def person(self) -> Person:
@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def uppercase(value: str):
def uppercase(value: DirectiveValue[str]):
return value.upper()

class ExampleExtension(SchemaExtension):
Expand Down Expand Up @@ -366,7 +379,7 @@ async def person(self) -> Person:
@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def uppercase(value: str):
def uppercase(value: DirectiveValue[str]):
return value.upper()

class ExampleExtension(SchemaExtension):
Expand Down Expand Up @@ -416,7 +429,7 @@ def greetingTemplate(self, locale: Locale = Locale.EN) -> str:
locations=[DirectiveLocation.FIELD],
description="Interpolate string on the server from context data",
)
def interpolate(value: str, info: strawberry.Info):
def interpolate(value: DirectiveValue[str], info: strawberry.Info):
try:
assert isinstance(info, strawberry.Info)
assert info._field is field
Expand Down Expand Up @@ -592,6 +605,7 @@ def append_names(value: DirectiveValue[str], names: List[str]):
)

assert result.errors is None
assert result.data
assert result.data["greeting"] == "Hi foo, bar"


Expand All @@ -602,12 +616,14 @@ class DirectiveInput:

@strawberry.type
class Query:
cake: str = "made_in_switzerland"
@strawberry.field
def cake(self) -> str:
return "made_in_switzerland"

@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def uppercase(value: str, input: DirectiveInput):
def uppercase(value: DirectiveValue[str], input: DirectiveInput):
return value.upper()

schema = strawberry.Schema(query=Query, directives=[uppercase])
Expand All @@ -627,18 +643,24 @@ def uppercase(value: str, input: DirectiveInput):

assert schema.as_str() == textwrap.dedent(expected_schema).strip()

result = schema.execute_sync('query { cake @uppercase(input: { example: "foo" }) }')
assert result.errors is None
assert result.data == {"cake": "MADE_IN_SWITZERLAND"}


def test_directives_with_scalar():
DirectiveInput = strawberry.scalar(str, name="DirectiveInput")

@strawberry.type
class Query:
cake: str = "made_in_switzerland"
@strawberry.field
def cake(self) -> str:
return "made_in_switzerland"

@strawberry.directive(
locations=[DirectiveLocation.FIELD], description="Make string uppercase"
)
def uppercase(value: str, input: DirectiveInput):
def uppercase(value: DirectiveValue[str], input: DirectiveInput):
return value.upper()

schema = strawberry.Schema(query=Query, directives=[uppercase])
Expand All @@ -656,6 +678,10 @@ def uppercase(value: str, input: DirectiveInput):

assert schema.as_str() == textwrap.dedent(expected_schema).strip()

result = schema.execute_sync('query { cake @uppercase(input: "foo") }')
assert result.errors is None
assert result.data == {"cake": "MADE_IN_SWITZERLAND"}


@pytest.mark.asyncio
async def test_directive_with_custom_info_class() -> NoReturn:
Expand Down Expand Up @@ -687,4 +713,5 @@ def append_names(value: DirectiveValue[str], names: List[str], info: CustomInfo)
)

assert result.errors is None
assert result.data
assert result.data["greeting"] == "Hi foo, bar"
3 changes: 2 additions & 1 deletion tests/schema/test_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
)

import strawberry
from strawberry.directive import DirectiveValue
from strawberry.scalars import JSON
from strawberry.schema.schema_converter import GraphQLCoreConverter
from strawberry.schema_directive import Location
Expand Down Expand Up @@ -52,7 +53,7 @@ class Query:

def test_directive():
@strawberry.directive(locations=[DirectiveLocation.FIELD])
def uppercase(value: str, foo: str):
def uppercase(value: DirectiveValue[str], foo: str):
return value.upper()

@strawberry.type()
Expand Down
4 changes: 2 additions & 2 deletions tests/schema/test_get_extensions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import strawberry
from strawberry.directive import DirectiveLocation
from strawberry.directive import DirectiveLocation, DirectiveValue
from strawberry.extensions import SchemaExtension
from strawberry.extensions.directives import (
DirectivesExtension,
Expand All @@ -13,7 +13,7 @@ class Query:


@strawberry.directive(locations=[DirectiveLocation.FIELD])
def uppercase(value: str) -> str:
def uppercase(value: DirectiveValue[str]) -> str:
return value.upper()


Expand Down
Loading

0 comments on commit 3ec03a7

Please sign in to comment.