Skip to content

Commit

Permalink
refactor(core): Refactor tests to read from csv
Browse files Browse the repository at this point in the history
  • Loading branch information
j3soon committed Jul 27, 2024
1 parent d1aa356 commit b8aab21
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 45 deletions.
19 changes: 9 additions & 10 deletions core/nurse_scheduling/exporter.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
import pandas as pd

from .context import Context

def get_people_versus_date_dataframe(
dates, people, requirements,
shifts, solver,
):

def get_people_versus_date_dataframe(ctx: Context, solver):
# Initialize dataframe with size including leading rows and columns
n_leading_rows, n_leading_cols = 2, 1
df = pd.DataFrame("", index=range(n_leading_rows + len(people)), columns=range(n_leading_cols + len(dates)))
df = pd.DataFrame("", index=range(n_leading_rows + len(ctx.people)), columns=range(n_leading_cols + len(ctx.dates)))

# Fill day numbers and weekdays
# - row 0 contains day number
# - row 1 contains weekday
for d, date in enumerate(dates):
for d, date in enumerate(ctx.dates):
df.iloc[0, n_leading_cols + d] = date.day
df.iloc[1, n_leading_cols + d] = date.strftime('%a')

# Fill person descriptions
# - column 0 contains person description
for p, person in enumerate(people):
for p, person in enumerate(ctx.people):
df.iloc[n_leading_rows+p, 0] = person.description

# Set cell values based on solver results
for (d, r, p) in shifts.keys():
if solver.Value(shifts[(d, r, p)]) == 1:
for (d, r, p) in ctx.shifts.keys():
if solver.Value(ctx.shifts[(d, r, p)]) == 1:
assert df.iloc[n_leading_rows+p, n_leading_cols+d] == ""
df.iloc[n_leading_rows+p, n_leading_cols+d] = requirements[r].id
df.iloc[n_leading_rows+p, n_leading_cols+d] = ctx.requirements[r].id

return df
5 changes: 1 addition & 4 deletions core/nurse_scheduling/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,5 @@ def on_solution_callback(self):
if not found:
return None

df = exporter.get_people_versus_date_dataframe(
ctx.dates, ctx.people, ctx.requirements,
ctx.shifts, solver,
)
df = exporter.get_people_versus_date_dataframe(ctx, solver)
return df
17 changes: 7 additions & 10 deletions core/tests/test_example_1.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import nurse_scheduling
import pandas as pd


def test_example_1():
filepath = "tests/testcases/example_1.yaml"
df = nurse_scheduling.schedule(filepath, validate=False, deterministic=True)
filepath = "tests/testcases/example_1"
df = nurse_scheduling.schedule(f"{filepath}.yaml", validate=False, deterministic=True)
print(df)
assert df.values.tolist() == [
['', 18, 19, 20],
['', 'Fri', 'Sat', 'Sun'],
['Nurse 0', 'E', 'E', 'E'],
['Nurse 1', 'D', 'D', 'D'],
['Nurse 2', '', '', ''],
['Nurse 3', 'N', 'N', 'N']
]
with open(f"{filepath}.csv", 'r') as f:
expected_csv = f.read()
assert df.to_csv(index=False, header=False) == expected_csv
15 changes: 5 additions & 10 deletions core/tests/test_or_tools_example_1.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import nurse_scheduling

def test_or_tools_example_1():
filepath = "tests/testcases/or_tools_example_1.yaml"
df = nurse_scheduling.schedule(filepath, validate=False, deterministic=True)
filepath = "tests/testcases/or_tools_example_1"
df = nurse_scheduling.schedule(f"{filepath}.yaml", validate=False, deterministic=True)
print(df)
assert df.values.tolist() == [
['', 18, 19, 20],
['', 'Fri', 'Sat', 'Sun'],
['Nurse 0', '', 'N', 'D'],
['Nurse 1', 'D', 'E', 'E'],
['Nurse 2', 'E', 'D', ''],
['Nurse 3', 'N', '', 'N']
]
with open(f"{filepath}.csv", 'r') as f:
expected_csv = f.read()
assert df.to_csv(index=False, header=False) == expected_csv
16 changes: 5 additions & 11 deletions core/tests/test_or_tools_example_2.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
import nurse_scheduling

def test_or_tools_example_2():
filepath = "tests/testcases/or_tools_example_2.yaml"
df = nurse_scheduling.schedule(filepath, validate=False, deterministic=True)
filepath = "tests/testcases/or_tools_example_2"
df = nurse_scheduling.schedule(f"{filepath}.yaml", validate=False, deterministic=True)
print(df)
assert df.values.tolist() == [
['', 1, 2, 3, 4, 5, 6, 7],
['', 'Fri', 'Sat', 'Sun', 'Mon', 'Tue', 'Wed', 'Thu'],
['Nurse 0', 'N', '', 'N', '', 'N', 'E', ''],
['Nurse 1', '', '', 'E', 'N', 'D', '', 'N'],
['Nurse 2', 'E', 'E', '', 'D', '', 'N', 'D'],
['Nurse 3', '', 'D', 'D', 'E', '', 'D', ''],
['Nurse 4', 'D', 'N', '', '', 'E', '', 'E'],
]
with open(f"{filepath}.csv", 'r') as f:
expected_csv = f.read()
assert df.to_csv(index=False, header=False) == expected_csv
6 changes: 6 additions & 0 deletions core/tests/testcases/example_1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
,18,19,20
,Fri,Sat,Sun
Nurse 0,E,E,E
Nurse 1,D,D,D
Nurse 2,,,
Nurse 3,N,N,N
6 changes: 6 additions & 0 deletions core/tests/testcases/or_tools_example_1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
,18,19,20
,Fri,Sat,Sun
Nurse 0,,N,D
Nurse 1,D,E,E
Nurse 2,E,D,
Nurse 3,N,,N
7 changes: 7 additions & 0 deletions core/tests/testcases/or_tools_example_2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
,1,2,3,4,5,6,7
,Fri,Sat,Sun,Mon,Tue,Wed,Thu
Nurse 0,N,,N,,N,E,
Nurse 1,,,E,N,D,,N
Nurse 2,E,E,,D,,N,D
Nurse 3,,D,D,E,,D,
Nurse 4,D,N,,,E,,E

0 comments on commit b8aab21

Please sign in to comment.