def freeze_tasks( repo: Repository, selector: TaskSelector, ) -> None: """Freeze a list of tasks based on a task filter.""" tasks = _tasks_from_selector(repo, selector) for task in tasks: if type(task) == Task: child_task = task if child_task.parent_id is None: raise ValueError( f"Task {child_task.id_}: {child_task.description} is not the child" " of any recurrent task, so it can't be frozen") parent_task = repo.get(child_task.parent_id, [RecurrentTask]) elif type(task) == RecurrentTask: parent_task = task try: child_task = repo.search( { "active": True, "parent_id": task.id_ }, [Task])[0] except EntityNotFoundError as error: raise EntityNotFoundError( f"The recurrent task {task.id_}: {task.description} has no active " "children") from error parent_task.freeze() repo.add(parent_task) repo.delete(child_task) log.info( f"Frozen recurrent task {parent_task.id_}: {parent_task.description} and " f"deleted it's last child {child_task.id_}") repo.commit()
def test_repository_delete_raise_error_if_entity_not_found( self, repo: Repository, entity: Entity, ) -> None: """ Given: an empty repository. When: trying to delete an inexistent entity. Then: An EntityNotFoundError error is raised. """ with pytest.raises(EntityNotFoundError) as error: repo.delete(entity) assert ( f"Unable to delete entity {entity} because it's not in the repository" in str(error.value))
def test_repository_can_delete_an_entity( self, repo: Repository, inserted_entities: List[Entity], ) -> None: """ Given: a full repository. When: an entity is deleted. Then: the entity is not longer in the repository. """ entity_to_delete = inserted_entities[1] repo.delete(entity_to_delete) repo.commit() # act remaining_entities = repo.all(type(entity_to_delete)) assert entity_to_delete not in remaining_entities
def test_repository_doesnt_delete_the_entity_if_we_dont_commit( self, database: Any, repo: Repository, repo_tester: RepositoryTester[Entity], inserted_entities: List[Entity], ) -> None: """ Given: a full repository. When: an entity is deleted but we don't commit the changes. Then: the entity is still in the repository. """ entity_to_delete = inserted_entities[1] repo.delete(entity_to_delete) # act remaining_entities = repo_tester.get_all(database, type(entity_to_delete)) assert entity_to_delete in remaining_entities