Skip to content

Commit 1e9033d

Browse files
committed
Fix unit tests for base resolvers
- Add pytest-asyncio default fixture loop scope configuration - Extract implementation methods from Strawberry fields for testability - Fix Task creation test to include required patient_id - Tests now call implementation methods directly instead of Strawberry fields
1 parent 591765d commit 1e9033d

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

backend/api/resolvers/base.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,22 @@ def __init__(self, model: type[ModelType]):
1818
def get_repository(self, db: AsyncSession) -> BaseRepository[ModelType]:
1919
return BaseRepository(db, self.model)
2020

21-
@strawberry.field
22-
async def get_by_id(self, info: Info, id: strawberry.ID) -> ModelType | None:
21+
async def _get_by_id_impl(self, info: Info, id: strawberry.ID) -> ModelType | None:
2322
repo = self.get_repository(info.context.db)
2423
return await repo.get_by_id(id)
2524

26-
@strawberry.field
27-
async def get_all(self, info: Info) -> list[ModelType]:
25+
async def _get_all_impl(self, info: Info) -> list[ModelType]:
2826
repo = self.get_repository(info.context.db)
2927
return await repo.get_all()
3028

29+
@strawberry.field
30+
async def get_by_id(self, info: Info, id: strawberry.ID) -> ModelType | None:
31+
return await self._get_by_id_impl(info, id)
32+
33+
@strawberry.field
34+
async def get_all(self, info: Info) -> list[ModelType]:
35+
return await self._get_all_impl(info)
36+
3137

3238
class BaseMutationResolver(Generic[ModelType]):
3339
def __init__(self, model: type[ModelType], entity_name: str):

backend/pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[pytest]
22
asyncio_mode = auto
3+
asyncio_default_fixture_loop_scope = function
34
testpaths = tests
45
python_files = test_*.py
56
python_classes = Test*

backend/tests/unit/test_base_resolvers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def __init__(self, db):
1313
async def test_base_query_resolver_get_by_id(db_session, sample_task):
1414
resolver = BaseQueryResolver(Task)
1515
info = MockInfo(db_session)
16-
result = await resolver.get_by_id(info, sample_task.id)
16+
result = await resolver._get_by_id_impl(info, sample_task.id)
1717
assert result is not None
1818
assert result.id == sample_task.id
1919

@@ -22,19 +22,20 @@ async def test_base_query_resolver_get_by_id(db_session, sample_task):
2222
async def test_base_query_resolver_get_all(db_session, sample_task):
2323
resolver = BaseQueryResolver(Task)
2424
info = MockInfo(db_session)
25-
results = await resolver.get_all(info)
25+
results = await resolver._get_all_impl(info)
2626
assert len(results) >= 1
2727
assert any(t.id == sample_task.id for t in results)
2828

2929

3030
@pytest.mark.asyncio
31-
async def test_base_mutation_resolver_create_and_notify(db_session):
31+
async def test_base_mutation_resolver_create_and_notify(db_session, sample_patient):
3232
resolver = BaseMutationResolver(Task, "task")
3333
info = MockInfo(db_session)
34-
new_task = Task(title="New Task", description="Description")
34+
new_task = Task(title="New Task", description="Description", patient_id=sample_patient.id)
3535
result = await resolver.create_and_notify(info, new_task)
3636
assert result.id is not None
3737
assert result.title == "New Task"
38+
assert result.patient_id == sample_patient.id
3839

3940

4041
@pytest.mark.asyncio

0 commit comments

Comments
 (0)