Skip to content

Commit

Permalink
don't use magic numbers in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryaniyaps committed Jun 29, 2024
1 parent 6823411 commit 5178406
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 22 deletions.
2 changes: 1 addition & 1 deletion server/tests/integration/todos/test_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from strawberry.relay import to_base64

from server.tests.integration.graphql_client import GraphQLClient
from tests.integration.graphql_client import GraphQLClient

pytestmark = pytest.mark.usefixtures("session")

Expand Down
47 changes: 26 additions & 21 deletions server/tests/integration/todos/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.ext.asyncio import AsyncSession
from strawberry.relay import to_base64

from server.tests.integration.graphql_client import GraphQLClient
from tests.integration.graphql_client import GraphQLClient

pytestmark = pytest.mark.usefixtures("session")

Expand Down Expand Up @@ -110,6 +110,9 @@ async def test_get_todos(graphql_client: GraphQLClient) -> None:
"""Ensure that we can get all todos."""
pagination_limit = 10

expected_start_cursor = to_base64(TodoType, 50)
expected_end_cursor = to_base64(TodoType, 41)

# Test fetching the first 10 todos
response = await graphql_client(
GET_TODOS_QUERY,
Expand All @@ -121,12 +124,13 @@ 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"] == to_base64(
TodoType, 50
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 41)
assert response["data"]["todos"]["pageInfo"]["startCursor"] == expected_start_cursor
assert response["data"]["todos"]["pageInfo"]["endCursor"] == expected_end_cursor

# Test fetching the next 10 todos
expected_start_cursor = to_base64(TodoType, 48)
expected_end_cursor = to_base64(TodoType, 39)

response = await graphql_client(
GET_TODOS_QUERY,
variables={
Expand All @@ -137,25 +141,27 @@ 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 True
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 48
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 39)
assert response["data"]["todos"]["pageInfo"]["startCursor"] == expected_start_cursor
assert response["data"]["todos"]["pageInfo"]["endCursor"] == expected_end_cursor

# Test fetching the last 10 todos
expected_start_cursor = to_base64(TodoType, 10)
expected_end_cursor = to_base64(TodoType, 1)

response = await graphql_client(
GET_TODOS_QUERY,
variables={"last": pagination_limit},
)
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"] == to_base64(
TodoType, 10
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 1)
assert response["data"]["todos"]["pageInfo"]["startCursor"] == expected_start_cursor
assert response["data"]["todos"]["pageInfo"]["endCursor"] == expected_end_cursor

# Test fetching 10 todos before the last one
expected_start_cursor = to_base64(TodoType, 11)
expected_end_cursor = to_base64(TodoType, 2)

response = await graphql_client(
GET_TODOS_QUERY,
variables={
Expand All @@ -166,14 +172,15 @@ 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 True
assert response["data"]["todos"]["pageInfo"]["startCursor"] == to_base64(
TodoType, 11
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 2)
assert response["data"]["todos"]["pageInfo"]["startCursor"] == expected_start_cursor
assert response["data"]["todos"]["pageInfo"]["endCursor"] == expected_end_cursor

# Test fetching all todos with a limit higher than the total count
pagination_limit = 75

expected_start_cursor = to_base64(TodoType, 50)
expected_end_cursor = to_base64(TodoType, 1)

response = await graphql_client(
GET_TODOS_QUERY,
variables={
Expand All @@ -183,7 +190,5 @@ 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"] == to_base64(
TodoType, 50
)
assert response["data"]["todos"]["pageInfo"]["endCursor"] == to_base64(TodoType, 1)
assert response["data"]["todos"]["pageInfo"]["startCursor"] == expected_start_cursor
assert response["data"]["todos"]["pageInfo"]["endCursor"] == expected_end_cursor

0 comments on commit 5178406

Please sign in to comment.