Skip to content

Commit

Permalink
rewrite test imports
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaniyaps committed Jun 29, 2024
1 parent 3e79ca5 commit 44bfd61
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 36 deletions.
3 changes: 2 additions & 1 deletion server/tests/integration/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ class GraphQLResponse(TypedDict):
errors: NotRequired[Any]


# TODO: subclass strawberry.test.BaseGraphQLTestClient
class GraphQLClient:
"""Async GraphQL client."""

def __init__(self, client: AsyncClient, endpoint: str) -> None:
self._client = client
self._endpoint = endpoint
Expand Down
18 changes: 9 additions & 9 deletions server/tests/integration/todos/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from app.todos.types import TodoType
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from strawberry import relay
from strawberry.relay import to_base64

from tests.integration.client import GraphQLClient

Expand Down Expand Up @@ -56,15 +56,15 @@ async def test_create_todo(
"createTodo": {
"todoEdge": {
"node": {
"id": relay.to_base64(TodoType, todo.id),
"id": to_base64(TodoType, todo.id),
"completed": todo.completed,
"content": content,
"createdAt": todo.created_at.isoformat(),
"updatedAt": todo.updated_at.isoformat()
if todo.updated_at
else None,
},
"cursor": relay.to_base64(TodoType, todo.id),
"cursor": to_base64(TodoType, todo.id),
},
},
},
Expand Down Expand Up @@ -99,7 +99,7 @@ async def test_delete_todo(
response = await graphql_client(
DELETE_TODO_MUTATION,
variables={
"todoId": relay.to_base64(TodoType, todo.id),
"todoId": to_base64(TodoType, todo.id),
},
)

Expand All @@ -113,7 +113,7 @@ async def test_delete_todo(
"data": {
"deleteTodo": {
"__typename": "Todo",
"id": relay.to_base64(TodoType, todo.id),
"id": to_base64(TodoType, todo.id),
"completed": todo.completed,
"content": todo.content,
"createdAt": todo.created_at.isoformat(),
Expand All @@ -128,7 +128,7 @@ async def test_delete_todo_unknown_id(graphql_client: GraphQLClient) -> None:
response = await graphql_client(
DELETE_TODO_MUTATION,
variables={
"todoId": relay.to_base64(TodoType, 1432),
"todoId": to_base64(TodoType, 1432),
},
)

Expand Down Expand Up @@ -171,7 +171,7 @@ async def test_toggle_todo_completed(
response = await graphql_client(
TOGGLE_TODO_COMPLETED_MUTATION,
variables={
"todoId": relay.to_base64(TodoType, todo.id),
"todoId": to_base64(TodoType, todo.id),
},
)

Expand All @@ -182,7 +182,7 @@ async def test_toggle_todo_completed(
"data": {
"toggleTodoCompleted": {
"__typename": "Todo",
"id": relay.to_base64(TodoType, todo.id),
"id": to_base64(TodoType, todo.id),
"completed": (not initial_completed_value),
"content": todo.content,
"createdAt": todo.created_at.isoformat(),
Expand All @@ -197,7 +197,7 @@ async def test_toggle_todo_completed_unknown_id(graphql_client: GraphQLClient) -
response = await graphql_client(
TOGGLE_TODO_COMPLETED_MUTATION,
variables={
"todoId": relay.to_base64(TodoType, 1432),
"todoId": to_base64(TodoType, 1432),
},
)

Expand Down
42 changes: 16 additions & 26 deletions server/tests/integration/todos/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from app.todos.repositories import TodoRepo
from app.todos.types import TodoType
from sqlalchemy.ext.asyncio import AsyncSession
from strawberry import relay
from strawberry.relay import to_base64

from tests.integration.client import GraphQLClient

Expand Down Expand Up @@ -50,14 +50,14 @@ async def test_get_todo(graphql_client: GraphQLClient, todo: Todo) -> None:
response = await graphql_client(
GET_TODO_QUERY,
variables={
"todoId": relay.to_base64(TodoType, todo.id),
"todoId": to_base64(TodoType, todo.id),
},
)

assert response == {
"data": {
"node": {
"id": relay.to_base64(TodoType, todo.id),
"id": to_base64(TodoType, todo.id),
"completed": todo.completed,
"content": todo.content,
"createdAt": todo.created_at.isoformat(),
Expand All @@ -72,7 +72,7 @@ async def test_get_todo_unknown_id(graphql_client: GraphQLClient) -> None:
response = await graphql_client(
GET_TODO_QUERY,
variables={
"todoId": relay.to_base64(TodoType, 1432),
"todoId": to_base64(TodoType, 1432),
},
)

Expand Down Expand Up @@ -121,30 +121,26 @@ async def test_get_todos(graphql_client: GraphQLClient) -> None:
assert len(response["data"]["todos"]["edges"]) == pagination_limit
assert response["data"]["todos"]["pageInfo"]["hasNextPage"] is True
assert response["data"]["todos"]["pageInfo"]["hasPreviousPage"] is False
assert response["data"]["todos"]["pageInfo"]["startCursor"] == relay.to_base64(
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 50
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == relay.to_base64(
TodoType, 41
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 41)

# Test fetching the next 10 todos
response = await graphql_client(
GET_TODOS_QUERY,
variables={
"first": pagination_limit,
"after": relay.to_base64(TodoType, 49),
"after": to_base64(TodoType, 49),
},
)
assert len(response["data"]["todos"]["edges"]) == pagination_limit
assert response["data"]["todos"]["pageInfo"]["hasNextPage"] is True
assert response["data"]["todos"]["pageInfo"]["hasPreviousPage"] is True
assert response["data"]["todos"]["pageInfo"]["startCursor"] == relay.to_base64(
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 48
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == relay.to_base64(
TodoType, 39
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 39)

# Test fetching the last 10 todos
response = await graphql_client(
Expand All @@ -154,30 +150,26 @@ async def test_get_todos(graphql_client: GraphQLClient) -> None:
assert len(response["data"]["todos"]["edges"]) == pagination_limit
assert response["data"]["todos"]["pageInfo"]["hasNextPage"] is False
assert response["data"]["todos"]["pageInfo"]["hasPreviousPage"] is True
assert response["data"]["todos"]["pageInfo"]["startCursor"] == relay.to_base64(
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 10
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == relay.to_base64(
TodoType, 1
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 1)

# Test fetching 10 todos before the last one
response = await graphql_client(
GET_TODOS_QUERY,
variables={
"last": pagination_limit,
"before": relay.to_base64(TodoType, 1),
"before": to_base64(TodoType, 1),
},
)
assert len(response["data"]["todos"]["edges"]) == pagination_limit
assert response["data"]["todos"]["pageInfo"]["hasNextPage"] is True
assert response["data"]["todos"]["pageInfo"]["hasPreviousPage"] is True
assert response["data"]["todos"]["pageInfo"]["startCursor"] == relay.to_base64(
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 11
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == relay.to_base64(
TodoType, 2
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 2)

# Test fetching all todos with a limit higher than the total count
pagination_limit = 75
Expand All @@ -191,9 +183,7 @@ async def test_get_todos(graphql_client: GraphQLClient) -> None:
assert len(response["data"]["todos"]["edges"]) < pagination_limit
assert response["data"]["todos"]["pageInfo"]["hasNextPage"] is False
assert response["data"]["todos"]["pageInfo"]["hasPreviousPage"] is False
assert response["data"]["todos"]["pageInfo"]["startCursor"] == relay.to_base64(
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 50
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == relay.to_base64(
TodoType, 1
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 1)

0 comments on commit 44bfd61

Please sign in to comment.