def test_delete_task_single_actual(): """ Test deleting task from a schedule with only one task in actual. """ # Construct session. Note that we have to manually set the base date of the session # in order to access the task, since it has a hard-coded date. session = Session("test") session.insert_task( day=4, planned=False, name="test", priority=1.0, start_time=time(hour=12), hours=1.5, ) session.base_date = session.current_schedules( )[1].tasks[0].start_time.date() session.base_date -= timedelta(days=session.base_date.weekday()) # Delete the task. session.delete_task(planned=False, day=4, task_index=0) # Test session values. assert session.history_pos == 2 assert len(session.edit_history) == 3 assert len(session.edit_history[0][0].tasks) == 0 assert len(session.edit_history[0][1].tasks) == 0 assert len(session.edit_history[1][0].tasks) == 0 assert len(session.edit_history[1][1].tasks) == 1 assert len(session.edit_history[2][0].tasks) == 0 assert len(session.edit_history[2][1].tasks) == 0
def test_insert_task_empty_planned(): """ Insert a task into the planned schedule of an empty session. """ # Construct session. session = Session("test") # Insert task. session.insert_task( day=0, planned=True, name="test", priority=1.0, start_time=time(hour=12), hours=1.5, ) # Test session values. assert session.history_pos == 1 assert len(session.edit_history) == 2 assert len(session.edit_history[0][0].tasks) == 0 assert len(session.edit_history[0][1].tasks) == 0 assert len(session.edit_history[1][0].tasks) == 1 assert len(session.edit_history[1][1].tasks) == 0 task = session.edit_history[1][0].tasks[0] assert task.name == "test" assert task.priority == 1.0 assert task.start_time == datetime.combine(session.base_date, time(hour=12)) assert task.end_time == datetime.combine( session.base_date, time(hour=13, minute=30) )
def test_undo_empty_planned(): """ Test undoing the addition of a task to an empty session. """ # Construct session. session = Session("test") # Insert task and undo insertion. session.insert_task( day=4, planned=True, name="test", priority=1.0, start_time=time(hour=12), hours=1.5, ) session.undo() # Test session values. assert session.history_pos == 0 assert len(session.edit_history) == 2 assert len(session.edit_history[0][0].tasks) == 0 assert len(session.edit_history[0][1].tasks) == 0 assert len(session.edit_history[1][0].tasks) == 1 assert len(session.edit_history[1][1].tasks) == 0 task = session.edit_history[1][0].tasks[0] assert task.name == "test" assert task.priority == 1.0 assert task.start_time.date() == session.base_date + timedelta(days=4) assert task.start_time.time() == time(hour=12) assert task.end_time.date() == session.base_date + timedelta(days=4) assert task.end_time.time() == time(hour=13, minute=30)
def test_insert_task_overlap(): """ Insert an invalid (overlapping) task into the planned schedule of a nonempty session, and check that an error gets raised. """ # Construct session. session = Session("example", load=True) # Store pre-insert session values. pre_history_pos = session.history_pos pre_edit_history = list(session.edit_history) # Insert task. error = False try: session.insert_task( day=5, planned=True, name="test", priority=1.0, start_time=time(hour=13), hours=1, ) except ValueError: error = True # Ensure error was thrown. assert error
def test_insert_task_nonempty_planned(): """ Insert a task into the planned schedule of a nonempty session. """ # Construct session. session = Session("example", load=True) # Store pre-insert session values. pre_history_pos = session.history_pos pre_edit_history = list(session.edit_history) # Insert task. task = Task( "test", priority=1.0, start_time=datetime(2020, 5, 1, hour=15), end_time=datetime(2020, 5, 1, hour=16), ) session.insert_task( day=4, planned=True, name="test", priority=1.0, start_time=time(hour=15), hours=1, ) # Test session values. assert session.history_pos == pre_history_pos + 1 assert len(session.edit_history) == len(pre_edit_history) + 1 assert session.edit_history[:-1] == pre_edit_history assert session.edit_history[-1][1] == pre_edit_history[-1][1] assert ( list_exclude(session.edit_history[-1][0].tasks, 2) == pre_edit_history[-1][0].tasks ) task = session.edit_history[-1][0].tasks[2] assert task.name == "test" assert task.priority == 1.0 assert task.start_time == datetime.combine( session.base_date, time(hour=15) ) + timedelta(days=4) assert task.end_time == datetime.combine( session.base_date, time(hour=16) ) + timedelta(days=4)
def test_move_tasks_single_planned(): """ Test moving a single task from a planned schedule with only a single task. """ # Construct session. Note that we have to manually set the base date of the session # in order to access the task, since it has a hard-coded date. session = Session("test") session.insert_task( day=4, planned=True, name="test", priority=1.0, start_time=time(hour=12), hours=1.5, ) session.base_date = session.current_schedules( )[0].tasks[0].start_time.date() session.base_date -= timedelta(days=session.base_date.weekday()) # Move the task. session.move_tasks( planned=True, day=4, start_task_index=0, end_task_index=1, time_delta=timedelta(hours=2), ) # Test session values. assert session.history_pos == 2 assert len(session.edit_history) == 3 assert len(session.edit_history[0][0].tasks) == 0 assert len(session.edit_history[0][1].tasks) == 0 assert len(session.edit_history[1][0].tasks) == 1 assert len(session.edit_history[1][1].tasks) == 0 assert len(session.edit_history[2][0].tasks) == 1 assert len(session.edit_history[2][1].tasks) == 0 task = session.edit_history[2][0].tasks[0] assert task.name == "test" assert task.priority == 1.0 assert task.start_time.date() == session.base_date + timedelta(days=4) assert task.start_time.time() == time(hour=14) assert task.end_time.date() == session.base_date + timedelta(days=4) assert task.end_time.time() == time(hour=15, minute=30)
def test_redo_empty_planned(): """ Test redoing the addition of a task to an empty session. """ # Construct session. session = Session("test") # Insert task. session.insert_task( day=4, planned=True, name="test", priority=1.0, start_time=time(hour=12), hours=1.5, ) # Store session values. old_planned, old_actual = session.current_schedules() # Undo, then redo insertion. session.undo() session.redo() # Test session values. new_planned, new_actual = session.current_schedules() assert session.history_pos == 1 assert len(session.edit_history) == 2 assert new_planned == old_planned assert new_actual == old_actual assert len(session.edit_history[0][0].tasks) == 0 assert len(session.edit_history[0][1].tasks) == 0 assert len(session.edit_history[1][0].tasks) == 1 assert len(session.edit_history[1][1].tasks) == 0 task = session.edit_history[1][0].tasks[0] assert task.name == "test" assert task.priority == 1.0 assert task.start_time.date() == session.base_date + timedelta(days=4) assert task.start_time.time() == time(hour=12) assert task.end_time.date() == session.base_date + timedelta(days=4) assert task.end_time.time() == time(hour=13, minute=30)
def test_insert_task_sorted(): """ Insert a task into the actual schedule of a nonempty session, and check that the new task gets sorted correctly. """ # Construct session. session = Session("example", load=True) # Store pre-insert session values. pre_history_pos = session.history_pos pre_edit_history = list(session.edit_history) # Insert task. session.insert_task( day=5, planned=False, name="test", priority=1.0, start_time=time(hour=12, minute=30), hours=0.5, ) # Test session values. assert session.history_pos == pre_history_pos + 1 assert len(session.edit_history) == len(pre_edit_history) + 1 assert session.edit_history[:-1] == pre_edit_history assert session.edit_history[-1][0] == pre_edit_history[-1][0] assert ( list_exclude(session.edit_history[-1][1].tasks, 3) == pre_edit_history[-1][1].tasks ) task = session.edit_history[-1][1].tasks[3] assert task.name == "test" assert task.priority == 1.0 assert task.start_time == datetime.combine( session.base_date, time(hour=12, minute=30) ) + timedelta(days=5) assert task.end_time == datetime.combine( session.base_date, time(hour=13) ) + timedelta(days=5)
def test_daily_points_score_current_week(): """ Daily points for a schedule with tasks only in current week. """ # Construct session. session = Session("test") b = session.base_date planned_tasks = [ { "day": 1, "planned": True, "name": "pt1", "priority": 1.0, "start_time": time(hour=12), "hours": 1.5, }, { "day": 1, "planned": True, "name": "pt1", "priority": 2.0, "start_time": time(hour=13, minute=30), "hours": 2.0, }, { "day": 3, "planned": True, "name": "pt3", "priority": 1.0, "start_time": time(hour=13, minute=30), "hours": 2.0, }, ] actual_tasks = [ { "day": 1, "planned": False, "name": "at2", "priority": 2.0, "start_time": time(hour=14, minute=30), "hours": 1.0, }, { "day": 3, "planned": False, "name": "at3", "priority": 1.0, "start_time": time(hour=19, minute=30), "hours": 2.0, }, ] for task_kwargs in planned_tasks: session.insert_task(**task_kwargs) for task_kwargs in actual_tasks: session.insert_task(**task_kwargs) # Construct expected point/score values. p_ind_pts = [0.0, 5.5, 0.0, 2.0, 0.0, 0.0, 0.0] p_cum_pts = [0.0, 5.5, 5.5, 7.5, 7.5, 7.5, 7.5] a_ind_pts = [0.0, 2.0, 0.0, 2.0, 0.0, 0.0, 0.0] a_cum_pts = [0.0, 2.0, 2.0, 4.0, 4.0, 4.0, 4.0] ind_score = [100.0, 100.0 * 2.0 / 5.5] + [100.0] * 5 cum_score = [100.0] + [100.0 * 2.0 / 5.5] * 2 + [100.0 * 4.0 / 7.5] * 4 # Test point values. for day in range(DAYS_IN_WEEK): assert (session.daily_points(day, planned=True, cumulative=False) == p_ind_pts[day]) assert (session.daily_points(day, planned=True, cumulative=True) == p_cum_pts[day]) assert (session.daily_points(day, planned=False, cumulative=False) == a_ind_pts[day]) assert (session.daily_points(day, planned=False, cumulative=True) == a_cum_pts[day]) assert session.daily_score(day, cumulative=False) == ind_score[day] assert session.daily_score(day, cumulative=True) == cum_score[day]