def test_build_response(app: Flask) -> None:
    task = Task(1, 1, "title", "contents")
    output = Success(task)
    response, status_code = build_response(output, TaskSchema)

    assert status_code == 200
    assert response.json["data"] == task.asdict()
Esempio n. 2
0
 def to_entity(self) -> Task:
     return Task(
         id=self.task_id,
         user_id=self.user_id,
         title=self.title,
         contents=self.contents,
     )
Esempio n. 3
0
def test_get_all_tasks_with_pagination() -> None:
    mock_tasks = [
        Task(1, 1, "1", "contents"),
        Task(2, 2, "2", "contents"),
        Task(3, 3, "3", "contents"),
        Task(4, 4, "4", "contents"),
    ]

    dto = GetUserTaskDto(user_id=1)
    repo = mock.Mock()
    repo.get_tasks.return_value = mock_tasks

    output = GetTasksByUserUseCase(repo).execute(dto)
    assert True if output.is_success() else False

    assert output.get_data() == mock_tasks
    assert output.get_meta() == {"previous_id": mock_tasks[-1].id, "limit": 10}
Esempio n. 4
0
def test_update_task_not_found(repo: TaskRepository) -> None:
    entity = Task(id=1, user_id=1, title="title", contents="contents")

    new_task = repo.update_task(entity.id)

    if new_task:
        assert False

    assert True
Esempio n. 5
0
def test_update_task_empty_dto(repo: TaskRepository, old_task: Task) -> None:
    entity = Task(id=old_task.id,
                  user_id=1,
                  title="new title",
                  contents="new contents")
    new_task = repo.update_task(entity.id)

    if not new_task:
        assert False

    assert new_task.title == old_task.title
    assert new_task.contents == old_task.contents
Esempio n. 6
0
def test_create_task_repository_fail() -> None:
    request_task = Task(1, 1, "title", "contents")

    dto = CreateTaskDto(
        user_id=request_task.user_id,
        title=request_task.title,
        contents=request_task.contents,
    )

    repo = mock.Mock()
    repo.create_task.return_value = None

    output = CreateTaskUseCase(repo).execute(dto)

    assert False if output.is_success() else True
Esempio n. 7
0
def test_update_task(repo: TaskRepository, old_task: Task) -> None:
    task_id = old_task.id
    user_id = 1
    new_task_title = "new title"
    new_task_contents = "new contents"
    entity = Task(id=task_id,
                  user_id=user_id,
                  title=new_task_title,
                  contents=new_task_contents)

    repo.create_task(task_id, old_task.title, old_task.contents)
    new_task = repo.update_task(entity.id, entity.title, entity.contents)

    if not new_task:
        assert False

    assert new_task.title == new_task_title
    assert new_task.contents == new_task_contents
Esempio n. 8
0
def test_update_task() -> None:
    task_id = 1
    user_id = 1
    title = "title"
    contents = "contents"
    expected_task = Task(task_id, user_id, title, contents)
    dto = UpdateTaskDto(task_id, user_id, title, contents)

    repo = mock.Mock()
    repo.read_task.return_value = expected_task
    repo.update_task.return_value = expected_task

    output = UpdateTaskUseCase(repo).execute(dto)
    assert True if output.is_success() else False

    task = output.get_data()
    assert task.id == expected_task.id
    assert task.user_id == expected_task.user_id
    assert task.title == expected_task.title
    assert task.contents == expected_task.contents
Esempio n. 9
0
def test_create_task() -> None:
    expected_task = Task(1, 1, "title", "contents")

    dto = CreateTaskDto(
        user_id=expected_task.user_id,
        title=expected_task.title,
        contents=expected_task.contents,
    )

    repo = mock.Mock()
    repo.create_task.return_value = expected_task

    output = CreateTaskUseCase(repo).execute(dto)
    assert True if output.is_success() else False

    task = output.get_data()
    assert type(task) is Task
    assert task.user_id is expected_task.user_id
    assert task.title is expected_task.title
    assert task.contents is expected_task.contents