"""Génération des créneaux d'un poste à partir des créneaux types.""" def _positions_url(edition): return f"/editions/{edition.id}/positions" def _create_position(client, edition, name="Bar"): return client.post( _positions_url(edition), json={"name": name, "category": "BAR"} ).json() def test_creating_a_position_generates_one_shift_per_day_and_type(client, edition): position = _create_position(client, edition) shifts = client.get( f"/editions/{edition.id}/shifts", params={"position_id": position["id"]} ).json() # 4 jours (2026-07-09..12) x 3 créneaux types. assert len(shifts) == 12 assert all(s["min_staff"] == 0 and s["ideal_staff"] == 0 for s in shifts) assert all(s["day_part_id"] is not None for s in shifts) def test_generate_is_idempotent(client, edition): position = _create_position(client, edition) resp = client.post( f"{_positions_url(edition)}/{position['id']}/generate-shifts" ) assert resp.status_code == 200 assert resp.json()["created"] == 0 def test_extending_the_edition_then_generating_adds_the_new_days(client, edition): position = _create_position(client, edition) client.patch(f"/editions/{edition.id}", json={"end_date": "2026-07-14"}) resp = client.post( f"{_positions_url(edition)}/{position['id']}/generate-shifts" ) # 2 jours de plus x 3 créneaux types. assert resp.json()["created"] == 6 shifts = client.get( f"/editions/{edition.id}/shifts", params={"position_id": position["id"]} ).json() assert len(shifts) == 18 def test_adding_a_day_part_then_generating_adds_it_everywhere(client, edition): position = _create_position(client, edition) client.post( f"/editions/{edition.id}/day-parts", json={"label": "Nuit", "start_time": "02:00", "end_time": "05:00"}, ) resp = client.post( f"{_positions_url(edition)}/{position['id']}/generate-shifts" ) assert resp.json()["created"] == 4 # 1 par jour def test_generation_does_not_touch_staffing_already_set(client, edition): position = _create_position(client, edition) shift = client.get( f"/editions/{edition.id}/shifts", params={"position_id": position["id"]} ).json()[0] client.patch( f"/editions/{edition.id}/shifts/{shift['id']}", json={"min_staff": 3, "ideal_staff": 5}, ) client.post(f"{_positions_url(edition)}/{position['id']}/generate-shifts") again = client.get(f"/editions/{edition.id}/shifts/{shift['id']}").json() assert again["min_staff"] == 3 and again["ideal_staff"] == 5