This commit is contained in:
Ross
2022-07-13 19:30:14 +01:00
parent dc975b991c
commit 8b7af25238
5 changed files with 757 additions and 165 deletions
+160
View File
@@ -0,0 +1,160 @@
import datetime
import pytest
from rota.shifts import InvalidShift, NoWorkers, RotaBuilder, SingleShift, days
from rota.workers import Worker
def generate_basic_rota(weeks_to_rota=10):
start_date = datetime.date(2022, 3, 7)
Rota = RotaBuilder(
start_date,
weeks_to_rota=weeks_to_rota,
)
# Add a few workers
Rota.add_workers(
[
Worker(name="worker1", site="group1", grade=1),
Worker(name="worker2", site="group1", grade=1),
]
)
return Rota
def date_generator(from_date, days):
n = 0
while True:
yield from_date
n = n + 1
if n >= days:
break
from_date = from_date + datetime.timedelta(days=1)
class TestNightShifts:
def test_nights(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="n", length= 12.5, days=days[5:],
force_as_block=True,
constraint=[{"name": "night"}, {"name": "pre", "options": 2}],
workers_required=2,
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[:3],
force_as_block=False,
#constraint=[{"name": "night"}],
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("night")
assert Rota.results.solver.status == "ok"
assert Rota.results.solver.termination_condition == "optimal"
def test_nights_pre3(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="n", length= 12.5, days=days[5:],
force_as_block=True,
constraint=[{"name": "night"}, {"name": "pre", "options": 3}],
workers_required=2,
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[:2],
force_as_block=False,
#constraint=[{"name": "night"}],
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("night_pre3")
assert Rota.results.solver.status == "ok"
def test_nights_pre3_2(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="n", length= 12.5, days=days[5:],
force_as_block=True,
constraint=[{"name": "night"}, {"name": "pre", "options": 3}],
workers_required=1,
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[:3],
force_as_block=False,
#constraint=[{"name": "night"}],
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("night_pre3")
assert Rota.results.solver.status == "ok"
def test_nights_fail(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="n", length= 12.5, days=days[5:],
force_as_block=True,
constraint=[{"name": "night"}, {"name": "pre", "options": 3}],
workers_required=2,
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[:3],
force_as_block=False,
#constraint=[{"name": "night"}],
),
)
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "warning"
def test_nights2(self):
Rota = generate_basic_rota()
Rota.constraint_options["max_night_frequency"] = 0
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="n", length= 12.5, days=days[5:],
force_as_block=True,
constraint=[{"name": "night"}],
workers_required=2,
),
SingleShift(
sites=("group1", "group2"), name="m", length= 12.5, days=days[:5],
force_as_block=True,
#constraint=[{"name": "night"}, {"name": "pre", "options": 3}],
#constraint=[{"name": "night"}],
workers_required=2,
),
#SingleShift(
# sites=("group1", "group2"), name="b", length= 12.5, days=days[:3],
# force_as_block=False,
# #constraint=[{"name": "night"}],
#),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("night")
assert Rota.results.solver.status == "ok"
+93 -11
View File
@@ -203,6 +203,7 @@ class TestDemoRotaNights:
Rota.constraint_options["balance_nights"] = False
Rota.constraint_options["minimise_shift_diffs"] = False
Rota.constraint_options["balance_blocks"] = False
Rota.constraint_options["max_night_frequency"] = 0
# Rota.constraint_options["balance_weekends"] = False
# Add a few workers
@@ -227,7 +228,7 @@ class TestDemoRotaNights:
length=12.5,
days=days[:4],
workers_required=1,
constraint=[{"name": "preclear2"}, {"name": "postclear2"}],
constraint=[{"name": "pre", "options": 1}, {"name": "post", "options": 2}],
# constraint=["night"],
force_as_block=True
# We use a different balance weighting for each shift as otherwise
@@ -240,7 +241,7 @@ class TestDemoRotaNights:
length=12.5,
days=days[4:],
# balance_weighting=0.5
constraint=[{"name": "preclear2"}, {"name": "postclear2"}],
constraint=[{"name": "pre", "options": 1}, {"name": "post", "options": 2}],
# constraint=["night"],
force_as_block=True,
),
@@ -274,6 +275,8 @@ class TestDemoRotaNights:
assert self.Rota.start_date == self.start_date
def test_night_assignment(self):
assert self.Rota.results.solver.status == "ok"
shift_summary = self.Rota.get_shift_summary_dict()
for worker_name in shift_summary:
@@ -316,7 +319,7 @@ class TestDemoRotaClear:
(worker1, worker2, worker3, worker4, worker5, worker6, worker7, worker8)
)
def test_preclear(self):
def test_pre(self):
self.Rota.add_shifts(
SingleShift(
sites=("group1", "group2"),
@@ -325,7 +328,7 @@ class TestDemoRotaClear:
days=days[:5],
balance_offset=40,
workers_required=2,
constraint=[{"name": "preclear"}],
constraint=[{"name": "pre", "options": 1}],
),
SingleShift(
sites=("group1", "group2"),
@@ -342,7 +345,7 @@ class TestDemoRotaClear:
days=days[3],
balance_offset=40,
workers_required=1,
constraint=[{"name": "preclear2"}, {"name": "postclear2"}],
constraint=[{"name": "pre", "options": 1}, {"name": "post", "options": 1}],
),
)
@@ -369,12 +372,72 @@ class TestDemoRotaClear:
assert "ca" not in shifts_string
assert "ac" not in shifts_string
assert "ca" not in shifts_string
assert "a-c" not in shifts_string
assert "c-a" not in shifts_string
#assert "a-c" not in shifts_string
#assert "c-a" not in shifts_string
assert "bc" not in shifts_string
assert "cb" not in shifts_string
assert "b-c" not in shifts_string
assert "c-b" not in shifts_string
#assert "b-c" not in shifts_string
#assert "c-b" not in shifts_string
#def test_preclear(self):
# self.Rota.add_shifts(
# SingleShift(
# sites=("group1", "group2"),
# name="a",
# length=12.5,
# days=days[:5],
# balance_offset=40,
# workers_required=2,
# constraint=[{"name": "preclear"}],
# ),
# SingleShift(
# sites=("group1", "group2"),
# name="b",
# length=12.5,
# days=days[:5],
# balance_offset=40,
# workers_required=2,
# ),
# SingleShift(
# sites=("group1", "group2"),
# name="c",
# length=12.5,
# days=days[3],
# balance_offset=40,
# workers_required=1,
# constraint=[{"name": "pre", "options": 2}, {"name": "post", "options": 2}],
# ),
# )
# self.Rota.build_shifts()
# self.Rota.build_workers()
# self.Rota.build_model()
# solver_options = {"ratio": 0.1, "seconds": 1000, "threads": 10}
# self.Rota.solve_model(options=solver_options)
# self.Rota.export_rota_to_html("test4")
# assert self.Rota.results.solver.status == "ok"
# assert self.Rota.results.solver.termination_condition == "optimal"
# for worker in self.Rota.workers:
# shifts = self.Rota.get_worker_shift_list(worker)
# # Convert shift to a string representation
# shifts_string = "".join([i if i != "" else "-" for i in shifts])
# # check for patterns that should not occur
# assert "ba" not in shifts_string
# assert "ca" not in shifts_string
# assert "ac" not in shifts_string
# assert "ca" not in shifts_string
# assert "a-c" not in shifts_string
# assert "c-a" not in shifts_string
# assert "bc" not in shifts_string
# assert "cb" not in shifts_string
# assert "b-c" not in shifts_string
# assert "c-b" not in shifts_string
class TestDemoRotaShiftConstraints:
@@ -1045,7 +1108,7 @@ class TestNightUnavailable:
Rota.add_workers((worker1, worker2))
def test_basic_assignment(self, capsys):
def test_basic_assignment(self):
self.Rota.shifts = []
# Add a weekday and weekend shift
@@ -1074,7 +1137,6 @@ class TestNightUnavailable:
def test_assign_night_prior_to_unavailablity(self):
self.Rota.shifts = []
# Add a weekday and weekend shift
self.Rota.add_shifts(
SingleShift(
sites =("group1",),
@@ -1087,10 +1149,30 @@ class TestNightUnavailable:
)
self.Rota.build_and_solve(options={"ratio": 0.00})
self.Rota.export_rota_to_html("testnight_unavail")
assert self.Rota.results.solver.status == "warning"
assert self.Rota.results.solver.termination_condition == "infeasible"
def test_assign_non_night_prior_to_unavailablity(self):
self.Rota.shifts = []
self.Rota.add_shifts(
SingleShift(
sites =("group1",),
name = "night_weekend",
length = 12.5,
days = days[5:],
workers_required=2,
#constraint=[{"name": "night"}],
),
)
self.Rota.build_and_solve(options={"ratio": 0.00})
self.Rota.export_rota_to_html("testnight_unavail")
assert self.Rota.results.solver.status == "ok"
def test_assign_prior_to_unavailablity_non_night(self):
self.Rota.shifts = []
+139
View File
@@ -172,4 +172,143 @@ class TestShiftConstraints:
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("max_shifts_per_week_block")
assert Rota.results.solver.status == "warning"
def test_pre_shift_constraint(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="a", length= 12.5, days=days[0],
constraint=[{"name": "pre", "options": 1}],
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[1],
constraint=[{"name": "pre", "options": 1}],
),
SingleShift(
sites=("group1", "group2"), name="c", length= 12.5, days=days[2],
constraint=[{"name": "pre", "options": 1}],
),
SingleShift(
sites=("group1", "group2"), name="d", length= 12.5, days=days[3],
constraint=[{"name": "pre", "options": 1}],
),
SingleShift(
sites=("group1", "group2"), name="e", length= 12.5, days=days[4],
constraint=[{"name": "pre", "options": 1}],
),
SingleShift(
sites=("group1", "group2"), name="f", length= 12.5, days=days[5],
constraint=[{"name": "pre", "options": 1}],
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("pre")
assert Rota.results.solver.status == "ok"
for worker in Rota.get_workers():
shift_string = Rota.get_worker_shift_list_string(worker)
print(shift_string)
for s in ["ab", "bc", "cd", "de", "ef", "fg"]:
assert s not in shift_string
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="g", length= 12.5, days=days[6],
constraint=[{"name": "pre", "options": 2}],
),
)
Rota.build_and_solve(options={"ratio": 0.000})
assert Rota.results.solver.status == "warning"
def test_pre_shift_constraint2(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="a", length= 12.5, days=days[0],
constraint=[{"name": "pre", "options": 2}],
workers_required=2
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[4],
constraint=[{"name": "pre", "options": 3}],
workers_required=2
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("pre")
assert Rota.results.solver.status == "ok"
def test_pre_shift_constraint3(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="a", length= 12.5, days=days[0],
constraint=[{"name": "pre", "options": 3}],
workers_required=2
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[4],
constraint=[{"name": "pre", "options": 3}],
workers_required=2
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("pre")
assert Rota.results.solver.status == "warning"
def test_post_shift_constraint(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="a", length= 12.5, days=days[0],
constraint=[{"name": "post", "options": 3}],
workers_required=2
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[4],
constraint=[{"name": "post", "options": 2}],
workers_required=2
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("post")
assert Rota.results.solver.status == "ok"
def test_post_shift_constraint2(self):
Rota = generate_basic_rota()
Rota.add_shifts(
SingleShift(
sites=("group1", "group2"), name="a", length= 12.5, days=days[0],
constraint=[{"name": "post", "options": 3}],
workers_required=2
),
SingleShift(
sites=("group1", "group2"), name="b", length= 12.5, days=days[4],
constraint=[{"name": "post", "options": 3}],
workers_required=2
),
)
Rota.build_and_solve(options={"ratio": 0.000})
Rota.export_rota_to_html("post")
assert Rota.results.solver.status == "warning"