Give every year of a repeating period its own row
A repeating period was a single row that the planner expanded into one window per year at read time, so no year had anywhere to differ from the others. Editing one year rewrote them all, and removing a year in the middle had to split the wish in two. Neither survives contact with the thing this app is for: Easter moves, and the cheapest week is not the same week every year. Each year is now its own row, linked by a series id. Editing one year touches one row; editing the series re-projects the rest from it. Removing a year is one delete. The weekday snap is worked out once, when the rows are made, and stored, so a row now says what it means rather than being re-derived on every read. Recurrence stops being a stored field and becomes what it always was in practice — an instruction at create and update time; whether a period repeats is whether it belongs to a series. Migration 004 rebuilds the table and expands the existing rows, keeping each original row as its first year so the days already planned against it still point somewhere. A Feb 29 anchor recurring into a common year used to raise out of the allocator; it clamps now. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ei7gCZzdd751uZtPzb1Pep
This commit is contained in:
@@ -433,3 +433,124 @@ class TestLeaveWishWantsDubbeldagRoundTrip:
|
||||
|
||||
assert update_response.status_code == 200
|
||||
assert update_response.json()["wants_dubbeldag"] is True
|
||||
|
||||
|
||||
class TestLeaveWishScopeQueryParam:
|
||||
"""`?scope=series` widens PUT/DELETE from one year to the whole series.
|
||||
|
||||
The calendar's period popover offers "this year" vs "every year"; the
|
||||
scope has to reach the service or the choice is decorative.
|
||||
"""
|
||||
|
||||
async def _workspace(self, connection: aiosqlite.Connection) -> Workspace:
|
||||
now = datetime.now()
|
||||
ws = await SQLiteWorkspaceRepository(connection).create(
|
||||
Workspace(id=uuid4(), name="Familj", created_at=now)
|
||||
)
|
||||
await SQLitePersonalDataRepository(connection).create(
|
||||
PersonalData(
|
||||
uuid=_CURRENT_USER_PD_UUID, workspace_id=ws.id, name="Alex", created_at=now
|
||||
)
|
||||
)
|
||||
return ws
|
||||
|
||||
async def _create_series(self, client: AsyncClient, ws: Workspace) -> list[dict[str, object]]:
|
||||
first_year = date.today().year + 1
|
||||
response = await client.post(
|
||||
"/api/leave-wishes",
|
||||
json={
|
||||
"workspace_id": str(ws.id),
|
||||
"name": "Höstlov",
|
||||
"window_start_month": 10,
|
||||
"window_start_day": 5,
|
||||
"window_end_month": 10,
|
||||
"window_end_day": 11,
|
||||
"target_weeks": 1,
|
||||
"recurrence": "yearly",
|
||||
"first_year": first_year,
|
||||
"last_year": first_year + 2,
|
||||
},
|
||||
)
|
||||
assert response.status_code == 201, response.text
|
||||
assert response.json()["series_id"] is not None
|
||||
listed = await client.get(f"/api/leave-wishes/{ws.id}")
|
||||
assert listed.status_code == 200, listed.text
|
||||
return sorted(listed.json(), key=lambda row: row["first_year"])
|
||||
|
||||
async def test_create_yearly_returns_a_series_and_lists_one_row_per_year(
|
||||
self, client: AsyncClient, connection: aiosqlite.Connection
|
||||
) -> None:
|
||||
ws = await self._workspace(connection)
|
||||
rows = await self._create_series(client, ws)
|
||||
|
||||
first_year = date.today().year + 1
|
||||
assert [r["first_year"] for r in rows] == [first_year, first_year + 1, first_year + 2]
|
||||
assert len({r["series_id"] for r in rows}) == 1
|
||||
# `recurrence` is an instruction, not a response field any more.
|
||||
assert "recurrence" not in rows[0]
|
||||
|
||||
async def test_update_defaults_to_this_year_only(
|
||||
self, client: AsyncClient, connection: aiosqlite.Connection
|
||||
) -> None:
|
||||
ws = await self._workspace(connection)
|
||||
rows = await self._create_series(client, ws)
|
||||
|
||||
response = await client.put(
|
||||
f"/api/leave-wishes/{rows[1]['id']}", json={"name": "Bara detta år"}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
after = sorted(
|
||||
(await client.get(f"/api/leave-wishes/{ws.id}")).json(), key=lambda r: r["first_year"]
|
||||
)
|
||||
assert [r["name"] for r in after] == ["Höstlov", "Bara detta år", "Höstlov"]
|
||||
|
||||
async def test_update_with_scope_series_applies_to_every_year(
|
||||
self, client: AsyncClient, connection: aiosqlite.Connection
|
||||
) -> None:
|
||||
ws = await self._workspace(connection)
|
||||
rows = await self._create_series(client, ws)
|
||||
|
||||
response = await client.put(
|
||||
f"/api/leave-wishes/{rows[1]['id']}?scope=series", json={"name": "Alla år"}
|
||||
)
|
||||
assert response.status_code == 200, response.text
|
||||
|
||||
after = (await client.get(f"/api/leave-wishes/{ws.id}")).json()
|
||||
assert {r["name"] for r in after} == {"Alla år"}
|
||||
assert len(after) == 3
|
||||
|
||||
async def test_delete_defaults_to_this_year_only(
|
||||
self, client: AsyncClient, connection: aiosqlite.Connection
|
||||
) -> None:
|
||||
ws = await self._workspace(connection)
|
||||
rows = await self._create_series(client, ws)
|
||||
|
||||
response = await client.delete(f"/api/leave-wishes/{rows[1]['id']}")
|
||||
assert response.status_code == 204, response.text
|
||||
|
||||
after = (await client.get(f"/api/leave-wishes/{ws.id}")).json()
|
||||
assert [r["id"] for r in sorted(after, key=lambda r: r["first_year"])] == [
|
||||
rows[0]["id"],
|
||||
rows[2]["id"],
|
||||
]
|
||||
|
||||
async def test_delete_with_scope_series_removes_every_year(
|
||||
self, client: AsyncClient, connection: aiosqlite.Connection
|
||||
) -> None:
|
||||
ws = await self._workspace(connection)
|
||||
rows = await self._create_series(client, ws)
|
||||
|
||||
response = await client.delete(f"/api/leave-wishes/{rows[1]['id']}?scope=series")
|
||||
assert response.status_code == 204, response.text
|
||||
|
||||
assert (await client.get(f"/api/leave-wishes/{ws.id}")).json() == []
|
||||
|
||||
async def test_an_unknown_scope_is_a_422(
|
||||
self, client: AsyncClient, connection: aiosqlite.Connection
|
||||
) -> None:
|
||||
ws = await self._workspace(connection)
|
||||
rows = await self._create_series(client, ws)
|
||||
|
||||
response = await client.delete(f"/api/leave-wishes/{rows[0]['id']}?scope=everything")
|
||||
assert response.status_code == 422, response.text
|
||||
|
||||
Reference in New Issue
Block a user