Files
proc-rota/test_rota.py
T
2025-12-29 22:22:29 +00:00

158 lines
4.3 KiB
Python

#!/usr/bin/env python3
"""
Test script to generate a small rota for testing ICS export functionality.
"""
import datetime
import os
import sys
from pathlib import Path
# Add the project root to Python path
sys.path.insert(0, str(Path(__file__).parent))
from rota_generator.shifts import RotaBuilder, SingleShift, WorkerRequirement, days, NightConstraint, PreShiftConstraint
from rota_generator.workers import Worker, NotAvailableToWork, NonWorkingDays
def create_test_rota():
"""Create a small test rota with a few shifts and workers."""
# Define sites
sites = ("truro", "exeter", "plymouth")
# Create rota builder
rota_start_date = datetime.date(2025, 12, 29) # Start from today-ish
Rota = RotaBuilder(
rota_start_date,
weeks_to_rota=4, # Small rota for testing
balance_offset_modifier=2,
name="test_rota",
)
# Add some shifts
Rota.add_shifts(
SingleShift(
sites=("truro", "exeter", "plymouth"),
name="night_weekday",
length=12.25,
days=days[:5], # Mon-Fri
balance_offset=3.9,
workers_required=1,
force_as_block=True,
constraints=[NightConstraint(), PreShiftConstraint(days=1)],
),
SingleShift(
sites=("truro", "exeter", "plymouth"),
name="weekend_truro",
length=12.5,
days=days[5:], # Sat-Sun
balance_offset=3,
workers_required=1,
force_as_block=True,
constraints=[PreShiftConstraint(days=1)],
),
SingleShift(
sites=("plymouth",),
name="plymouth_twilight",
length=12.5,
days=days[:5],
balance_offset=4,
workers_required=1,
),
)
# Add some test workers
workers = [
Worker(
name="Alice",
site="truro",
fte=100, # 1.0 FTE
start_date=rota_start_date,
end_date=None,
grade=1,
initials="AL",
),
Worker(
name="Bob",
site="exeter",
fte=100, # 1.0 FTE
start_date=rota_start_date,
end_date=None,
grade=1,
initials="BO",
),
Worker(
name="Charlie",
site="plymouth",
fte=100, # 1.0 FTE
start_date=rota_start_date,
end_date=None,
grade=1,
initials="CH",
),
Worker(
name="Chester",
site="plymouth",
fte=100, # 1.0 FTE
start_date=rota_start_date,
end_date=None,
grade=1,
initials="CH",
),
Worker(
name="Dino",
site="truro",
fte=100, # 0.8 FTE
start_date=rota_start_date,
end_date=None,
grade=2,
initials="DI",
),
Worker(
name="Diana",
site="truro",
fte=80, # 0.8 FTE
start_date=rota_start_date,
end_date=None,
grade=2,
initials="DI",
),
]
Rota.add_workers(workers)
# Build and solve
Rota.build_and_solve(options={"ratio": 0.1, "seconds": 30, "threads": 10})
return Rota
def generate_html_output(rota, output_dir="output/test"):
"""Generate HTML output for the rota."""
os.makedirs(output_dir, exist_ok=True)
# Use the built-in export method
rota.export_rota_to_html(filename="index")
html_path = os.path.join("output", "test", "index.html")
print(f"Test rota HTML generated at: {html_path}")
print("Open this file in a web browser to test the ICS export functionality.")
return html_path
if __name__ == "__main__":
print("Generating test rota...")
try:
rota = create_test_rota()
html_path = generate_html_output(rota)
print("\nTest rota generation complete!")
print(f"To test ICS export, open: file://{os.path.abspath(html_path)}")
print("Then use the 'Set Shift Times' button to configure times and download ICS files.")
except Exception as e:
print(f"Error generating test rota: {e}")
import traceback
traceback.print_exc()