def test_thawing_a_parent_with_completed_children_works( self, repo: FakeRepository, insert_parent_task: Tuple[RecurrentTask, Task]) -> None: """ Given: A parent with completed children When: Thawing the parent Then: The parent is thawed and the next children is breeded """ parent_task, child_task = insert_parent_task services._close_task(repo, child_task, TaskState.DONE) repo.commit() selector = TaskSelector(task_ids=[parent_task.id_], model=RecurrentTask) services.freeze_tasks(repo, selector) services.thaw_tasks(repo, selector) # act parent_task = repo.get(parent_task.id_, [RecurrentTask]) assert parent_task.state == TaskState.BACKLOG children_tasks = repo.search( { "parent_id": parent_task.id_, "active": True }, [Task]) assert len(children_tasks) == 1
def test_close_child_task_generates_next_children( self, action: Callable[[FakeRepository, TaskSelector], None], state: str, repo: FakeRepository, parent_and_child_tasks: Tuple[RecurrentTask, Task], caplog: LogCaptureFixture, ) -> None: """ Given: A repository with a parent and a child task. When: Using the do_tasks or rm_tasks on the child_id. Then: The child is closed, the new child is created, and the parent is left open. """ now = datetime.now() parent_task, child_task = parent_and_child_tasks selector = TaskSelector(task_ids=[child_task.id_]) action(repo, selector) # act parent_task = repo.get(parent_task.id_, [RecurrentTask]) new_child = repo.search({ "parent_id": parent_task.id_, "active": True }, [Task])[-1] assert new_child is not None assert new_child > child_task assert parent_task.state == "backlog" assert child_task.state == state assert child_task.closed is not None assert (child_task.closed - now).total_seconds() < 2 assert new_child.state == "backlog" assert (new_child.created - now).total_seconds() < 2 assert new_child.due is not None assert new_child.due >= parent_task.due assert ( "pydo.services", logging.INFO, f"Closing child task {child_task.id_}: {child_task.description} with state" f" {state}", ) in caplog.record_tuples assert ( "pydo.services", logging.INFO, f"Added child task {new_child.id_}: {new_child.description}", ) in caplog.record_tuples
def test_add_generates_recurrent_tasks( self, repo: FakeRepository, faker: Faker, caplog: LogCaptureFixture, recurrence_type: str, ) -> None: """ Given: Some desired task attributes for a recurrent or repeating task. When: using the add_task service. Then: Both parent and child tasks are added to the repository. """ task_attributes = { "description": faker.sentence(), "due": faker.date_time(), "recurrence": "1d", "recurrence_type": recurrence_type, "state": "todo", } result = services.add_task( repo, TaskChanges(task_attributes=task_attributes)) assert isinstance(result, RecurrentTask) parent_task = repo.get(result.id_, [RecurrentTask]) child_task = repo.search({"parent_id": parent_task.id_}, [Task])[0] # Assert the id of the child is sequential with the parent's assert child_task.parent_id == parent_task.id_ assert child_task.state == "todo" assert child_task.due >= task_attributes["due"] assert parent_task.recurrence == task_attributes["recurrence"] assert parent_task.recurrence_type == task_attributes[ "recurrence_type"] assert parent_task.due == task_attributes["due"] assert ( "pydo.services", logging.INFO, f"Added {parent_task.recurrence_type} task {parent_task.id_}:" f" {parent_task.description}", ) in caplog.record_tuples assert ( "pydo.services", logging.INFO, f"Added first child task with id {child_task.id_}", ) in caplog.record_tuples
def test_thawing_a_parent_without_children_works( self, repo: FakeRepository) -> None: """ Given: A parent without children When: Thawing the parent Then: The parent is thawed and the first children is breeded """ now = datetime.now() task = RecurrentTaskFactory(state="frozen") repo.add(task) repo.commit() selector = TaskSelector(task_ids=[task.id_]) services.thaw_tasks(repo, selector) # act parent_task = repo.get(task.id_, [RecurrentTask]) assert parent_task.state == TaskState.BACKLOG assert (parent_task.modified - now).total_seconds() < 2 children_tasks = repo.search({"parent_id": parent_task.id_}, [Task]) assert len(children_tasks) == 1