Skip to content

Commit

Permalink
Add tests for users endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalp committed Oct 5, 2024
1 parent 4ec3ea6 commit 35d4f1a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 44 deletions.
47 changes: 3 additions & 44 deletions example/schema.py
Original file line number Diff line number Diff line change
@@ -1,47 +1,6 @@
from ariadne_graphql_modules import GraphQLObject, make_executable_schema
from graphql import GraphQLResolveInfo
from ariadne_graphql_modules import make_executable_schema

from .database import db
from .enums.groupfilter import GroupFilter
from .types.group import GroupType
from .types.user import UserType
from .queries import queries


class Query(GraphQLObject):
hello: str

@GraphQLObject.resolver("hello")
@staticmethod
def resolve_hello(obj, info: GraphQLResolveInfo) -> str:
return "Hello world!"

@GraphQLObject.field(args={"filter_": GraphQLObject.argument("filter")})
@staticmethod
async def groups(
obj, info: GraphQLResolveInfo, filter_: GroupFilter = GroupFilter.ALL
) -> list[GroupType]:
if filter_ == GroupFilter.ADMIN:
return await db.get_all("groups", is_admin=True)

if filter_ == GroupFilter.MEMBER:
return await db.get_all("groups", is_admin=False)

return await db.get_all("groups")

@GraphQLObject.field()
@staticmethod
async def group(obj, info: GraphQLResolveInfo, id: str) -> GroupType | None:
try:
id_int = int(id)
except (TypeError, ValueError):
return None

return await db.get_row("groups", id=id_int)

@GraphQLObject.field()
@staticmethod
async def users(obj, info: GraphQLResolveInfo) -> list[UserType]:
return await db.get_all("users")


schema = make_executable_schema(Query, convert_names_case=True)
schema = make_executable_schema(queries, convert_names_case=True)
37 changes: 37 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,40 @@ async def test_query_groups_field_member_arg(exec_query):
},
],
}


@pytest.mark.asyncio
async def test_query_users(exec_query):
result = await exec_query("{ users { id username group { name } } }")
assert result.data == {
"users": [
{
"id": "1",
"username": "JohnDoe",
"group": {
"name": "Admins",
},
},
{
"id": "2",
"username": "Alice",
"group": {
"name": "Admins",
},
},
{
"id": "3",
"username": "Bob",
"group": {
"name": "Members",
},
},
{
"id": "4",
"username": "Mia",
"group": {
"name": "Members",
},
},
],
}

0 comments on commit 35d4f1a

Please sign in to comment.