File: test_api_completed_tasks.py

package info (click to toggle)
todoist-api-python 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 520 kB
  • sloc: python: 3,822; makefile: 3
file content (147 lines) | stat: -rw-r--r-- 4,362 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from __future__ import annotations

import sys
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Any

if sys.version_info >= (3, 11):
    from datetime import UTC
else:
    UTC = timezone.utc

import pytest
import responses

from tests.data.test_defaults import DEFAULT_API_URL, PaginatedItems
from tests.utils.test_utils import (
    auth_matcher,
    enumerate_async,
    param_matcher,
    request_id_matcher,
)
from todoist_api_python._core.utils import format_datetime

if TYPE_CHECKING:
    from todoist_api_python.api import TodoistAPI
    from todoist_api_python.api_async import TodoistAPIAsync
    from todoist_api_python.models import Task


@pytest.mark.asyncio
async def test_get_completed_tasks_by_due_date(
    todoist_api: TodoistAPI,
    todoist_api_async: TodoistAPIAsync,
    requests_mock: responses.RequestsMock,
    default_completed_tasks_response: list[PaginatedItems],
    default_completed_tasks_list: list[list[Task]],
) -> None:
    since = datetime(2024, 1, 1, 0, 0, 0, tzinfo=UTC)
    until = datetime(2024, 2, 1, 0, 0, 0, tzinfo=UTC)
    project_id = "6X7rM8997g3RQmvh"
    filter_query = "p1"

    params = {
        "since": format_datetime(since),
        "until": format_datetime(until),
        "project_id": project_id,
        "filter_query": filter_query,
    }

    endpoint = f"{DEFAULT_API_URL}/tasks/completed/by_due_date"

    cursor: str | None = None
    for page in default_completed_tasks_response:
        requests_mock.add(
            method=responses.GET,
            url=endpoint,
            json=page,
            status=200,
            match=[auth_matcher(), request_id_matcher(), param_matcher(params, cursor)],
        )
        cursor = page["next_cursor"]

    count = 0

    tasks_iter = todoist_api.get_completed_tasks_by_due_date(
        since=since,
        until=until,
        project_id=project_id,
        filter_query=filter_query,
    )

    for i, tasks in enumerate(tasks_iter):
        assert len(requests_mock.calls) == count + 1
        assert tasks == default_completed_tasks_list[i]
        count += 1

    tasks_async_iter = await todoist_api_async.get_completed_tasks_by_due_date(
        since=since,
        until=until,
        project_id=project_id,
        filter_query=filter_query,
    )

    async for i, tasks in enumerate_async(tasks_async_iter):
        assert len(requests_mock.calls) == count + 1
        assert tasks == default_completed_tasks_list[i]
        count += 1


@pytest.mark.asyncio
async def test_get_completed_tasks_by_completion_date(
    todoist_api: TodoistAPI,
    todoist_api_async: TodoistAPIAsync,
    requests_mock: responses.RequestsMock,
    default_completed_tasks_response: list[PaginatedItems],
    default_completed_tasks_list: list[list[Task]],
) -> None:
    since = datetime(2024, 3, 1, 0, 0, 0)  # noqa: DTZ001
    until = datetime(2024, 4, 1, 0, 0, 0)  # noqa: DTZ001
    workspace_id = "123"
    filter_query = "@label"

    params: dict[str, Any] = {
        "since": format_datetime(since),
        "until": format_datetime(until),
        "workspace_id": workspace_id,
        "filter_query": filter_query,
    }

    endpoint = f"{DEFAULT_API_URL}/tasks/completed/by_completion_date"

    cursor: str | None = None
    for page in default_completed_tasks_response:
        requests_mock.add(
            method=responses.GET,
            url=endpoint,
            json=page,
            status=200,
            match=[auth_matcher(), request_id_matcher(), param_matcher(params, cursor)],
        )
        cursor = page["next_cursor"]

    count = 0

    tasks_iter = todoist_api.get_completed_tasks_by_completion_date(
        since=since,
        until=until,
        workspace_id=workspace_id,
        filter_query=filter_query,
    )

    for i, tasks in enumerate(tasks_iter):
        assert len(requests_mock.calls) == count + 1
        assert tasks == default_completed_tasks_list[i]
        count += 1

    tasks_async_iter = await todoist_api_async.get_completed_tasks_by_completion_date(
        since=since,
        until=until,
        workspace_id=workspace_id,
        filter_query=filter_query,
    )

    async for i, tasks in enumerate_async(tasks_async_iter):
        assert len(requests_mock.calls) == count + 1
        assert tasks == default_completed_tasks_list[i]
        count += 1