Make repository list() reject unrecognised filter names
Repository.list(**filters) let each implementation pick out the filter names it understood and silently ignore the rest, so a typo or an unsupported filter returned every row in the table instead of an error. This was hit for real: LeaveWishRepository.list(parent_personal_data_uuid =...) returns every wish in the database, because that implementation only recognises workspace_id and child_id. A guard written on top of it would have passed while checking nothing. All ten implementations now validate against their supported set and raise ValueError naming the offending key. ValueError rather than a domain error because passing a filter that does not exist is a programming bug to fix, not a runtime condition to handle -- it must not become an HTTP 4xx. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01RXMhc9nT6sNz1SL4j8dznL
This commit is contained in:
@@ -287,6 +287,47 @@ class TestSQLiteLeaveWishRepository:
|
||||
assert tied.id in ids
|
||||
assert family_wide.id not in ids
|
||||
|
||||
async def test_list_rejects_unsupported_filter(
|
||||
self,
|
||||
leave_wish_repo: SQLiteLeaveWishRepository,
|
||||
seeded_ids: SeededIds,
|
||||
) -> None:
|
||||
"""Passing an unsupported filter (like a typo) must raise ValueError, not return all rows."""
|
||||
with pytest.raises(ValueError, match="parent_personal_data_uuid"):
|
||||
await leave_wish_repo.list(parent_personal_data_uuid=seeded_ids.personal_data_uuid)
|
||||
|
||||
# Verify the error message lists what filters ARE supported
|
||||
with pytest.raises(ValueError, match="child_id.*workspace_id"):
|
||||
await leave_wish_repo.list(parent_personal_data_uuid=seeded_ids.personal_data_uuid)
|
||||
|
||||
async def test_list_with_supported_filter_still_works(
|
||||
self,
|
||||
leave_wish_repo: SQLiteLeaveWishRepository,
|
||||
seeded_ids: SeededIds,
|
||||
) -> None:
|
||||
"""Verify filter validation doesn't break existing filtering behavior."""
|
||||
ws = seeded_ids.workspace_id
|
||||
created_1 = await leave_wish_repo.create(_make(workspace_id=ws, name="W1"))
|
||||
created_2 = await leave_wish_repo.create(_make(workspace_id=ws, name="W2"))
|
||||
|
||||
rows = await leave_wish_repo.list(workspace_id=ws)
|
||||
assert len(rows) >= 2
|
||||
ids = {r.id for r in rows}
|
||||
assert created_1.id in ids
|
||||
assert created_2.id in ids
|
||||
|
||||
async def test_list_rejects_multiple_unsupported_filters(
|
||||
self,
|
||||
leave_wish_repo: SQLiteLeaveWishRepository,
|
||||
seeded_ids: SeededIds,
|
||||
) -> None:
|
||||
"""Error message should list all unsupported filters."""
|
||||
with pytest.raises(ValueError, match="parent_personal_data_uuid.*typo_filter"):
|
||||
await leave_wish_repo.list(
|
||||
parent_personal_data_uuid=seeded_ids.personal_data_uuid,
|
||||
typo_filter="value",
|
||||
)
|
||||
|
||||
|
||||
class TestLeaveWishService:
|
||||
"""Test LeaveWishService business logic (validation)."""
|
||||
|
||||
@@ -9,6 +9,8 @@ this file targets the new column's round trip specifically.
|
||||
from datetime import datetime
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
||||
from app.entities.personal_data import PersonalData
|
||||
from app.entities.workspace import Workspace
|
||||
from app.repositories_impl.sqlite.personal_data_repository import SQLitePersonalDataRepository
|
||||
@@ -75,3 +77,43 @@ class TestPersonalDataMinVacationDays:
|
||||
fetched = await personal_data_repo.get_by_id(created.uuid)
|
||||
assert fetched is not None
|
||||
assert fetched.min_vacation_days_per_year == 15
|
||||
|
||||
async def test_list_rejects_unsupported_filter(
|
||||
self,
|
||||
personal_data_repo: SQLitePersonalDataRepository,
|
||||
) -> None:
|
||||
"""Passing an unsupported filter must raise ValueError."""
|
||||
with pytest.raises(ValueError, match="invalid_column"):
|
||||
await personal_data_repo.list(invalid_column="value")
|
||||
|
||||
async def test_list_accepts_include_deleted_filter(
|
||||
self,
|
||||
personal_data_repo: SQLitePersonalDataRepository,
|
||||
workspace_repo: SQLiteWorkspaceRepository,
|
||||
) -> None:
|
||||
"""The SoftDeleteRepository contract requires list() to accept include_deleted=True."""
|
||||
ws = await workspace_repo.create(
|
||||
Workspace(id=uuid4(), name="WS", created_at=datetime.now())
|
||||
)
|
||||
pd = await personal_data_repo.create(
|
||||
PersonalData(uuid=uuid4(), workspace_id=ws.id, name="Parent", created_at=datetime.now())
|
||||
)
|
||||
|
||||
# By default, deleted items should not be listed
|
||||
await personal_data_repo.soft_delete(pd.uuid)
|
||||
rows = await personal_data_repo.list(workspace_id=ws.id)
|
||||
ids = {r.uuid for r in rows}
|
||||
assert pd.uuid not in ids
|
||||
|
||||
# With include_deleted=True, deleted items should be included
|
||||
rows_with_deleted = await personal_data_repo.list(workspace_id=ws.id, include_deleted=True)
|
||||
ids_with_deleted = {r.uuid for r in rows_with_deleted}
|
||||
assert pd.uuid in ids_with_deleted
|
||||
|
||||
async def test_list_error_message_lists_supported_filters(
|
||||
self,
|
||||
personal_data_repo: SQLitePersonalDataRepository,
|
||||
) -> None:
|
||||
"""Error message should list supported filters."""
|
||||
with pytest.raises(ValueError, match="include_deleted.*workspace_id"):
|
||||
await personal_data_repo.list(bad_filter="value")
|
||||
|
||||
@@ -134,6 +134,22 @@ class TestSQLiteVacationDaysRepository:
|
||||
assert len(rows) == 2
|
||||
assert [r.year for r in rows] == [2026, 2025] # ordered year DESC
|
||||
|
||||
async def test_list_rejects_unsupported_filter(
|
||||
self,
|
||||
vacation_repo: SQLiteVacationDaysRepository,
|
||||
) -> None:
|
||||
"""Passing an unsupported filter must raise ValueError."""
|
||||
with pytest.raises(ValueError, match="invalid_filter"):
|
||||
await vacation_repo.list(invalid_filter="value")
|
||||
|
||||
async def test_list_error_message_lists_supported_filters(
|
||||
self,
|
||||
vacation_repo: SQLiteVacationDaysRepository,
|
||||
) -> None:
|
||||
"""Error message should list all supported filters (sorted)."""
|
||||
with pytest.raises(ValueError, match="personal_data_uuid.*workspace_id.*year"):
|
||||
await vacation_repo.list(bad_filter="value")
|
||||
|
||||
|
||||
class TestVacationDaysService:
|
||||
async def test_upsert_creates_then_updates(
|
||||
|
||||
Reference in New Issue
Block a user