-
-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(chore) fastapi: test typing (#3782)
- Loading branch information
1 parent
4c4e07b
commit 03d8aca
Showing
1 changed file
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
from inline_snapshot import snapshot | ||
|
||
from .utils.marks import requires_mypy, requires_pyright, skip_on_windows | ||
from .utils.typecheck import Result, typecheck | ||
|
||
pytestmark = [skip_on_windows, requires_pyright, requires_mypy] | ||
|
||
CODE_ROUTER_WITH_CONTEXT = """ | ||
import strawberry | ||
from strawberry.fastapi import GraphQLRouter, BaseContext | ||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def hello(self) -> str: | ||
return "Hello World" | ||
class Context(BaseContext): | ||
pass | ||
def get_context() -> Context: | ||
return Context() | ||
router = GraphQLRouter( | ||
strawberry.Schema( | ||
query=Query, | ||
), | ||
context_getter=get_context, | ||
) | ||
reveal_type(router) | ||
""" | ||
|
||
|
||
def test_router_with_context(): | ||
results = typecheck(CODE_ROUTER_WITH_CONTEXT) | ||
|
||
assert results.pyright == snapshot( | ||
[ | ||
Result( | ||
type="information", | ||
message='Type of "router" is "GraphQLRouter[Context, None]"', | ||
line=29, | ||
column=13, | ||
), | ||
] | ||
) | ||
assert results.mypy == snapshot( | ||
[ | ||
Result( | ||
type="note", | ||
message='Revealed type is "strawberry.fastapi.router.GraphQLRouter[mypy_test.Context, None]"', | ||
line=29, | ||
column=13, | ||
), | ||
] | ||
) | ||
|
||
|
||
CODE_ROUTER_WITH_ASYNC_CONTEXT = """ | ||
import strawberry | ||
from strawberry.fastapi import GraphQLRouter, BaseContext | ||
@strawberry.type | ||
class Query: | ||
@strawberry.field | ||
def hello(self) -> str: | ||
return "Hello World" | ||
class Context(BaseContext): | ||
pass | ||
async def get_context() -> Context: | ||
return Context() | ||
router = GraphQLRouter[Context]( | ||
strawberry.Schema( | ||
query=Query, | ||
), | ||
context_getter=get_context, | ||
) | ||
reveal_type(router) | ||
""" | ||
|
||
|
||
def test_router_with_async_context(): | ||
results = typecheck(CODE_ROUTER_WITH_ASYNC_CONTEXT) | ||
|
||
assert results.pyright == snapshot( | ||
[ | ||
Result( | ||
type="information", | ||
message='Type of "router" is "GraphQLRouter[Context, None]"', | ||
line=29, | ||
column=13, | ||
), | ||
] | ||
) | ||
assert results.mypy == snapshot( | ||
[ | ||
Result( | ||
type="note", | ||
message='Revealed type is "strawberry.fastapi.router.GraphQLRouter[mypy_test.Context, None]"', | ||
line=29, | ||
column=13, | ||
), | ||
] | ||
) |