File: test_utils.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 (54 lines) | stat: -rw-r--r-- 1,435 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
from __future__ import annotations

import re
from typing import TYPE_CHECKING, Any, TypeVar

from responses import matchers

from tests.data.test_defaults import (
    DEFAULT_TOKEN,
)
from todoist_api_python.api import TodoistAPI

if TYPE_CHECKING:
    from collections.abc import AsyncIterable, AsyncIterator, Callable


RE_UUID = re.compile(r"^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$", re.IGNORECASE)


def auth_matcher() -> Callable[..., Any]:
    return matchers.header_matcher({"Authorization": f"Bearer {DEFAULT_TOKEN}"})


def request_id_matcher(request_id: str | None = None) -> Callable[..., Any]:
    return matchers.header_matcher({"X-Request-Id": request_id or RE_UUID})


def param_matcher(
    params: dict[str, str], cursor: str | None = None
) -> Callable[..., Any]:
    return matchers.query_param_matcher(params | ({"cursor": cursor} if cursor else {}))


def data_matcher(data: dict[str, Any]) -> Callable[..., Any]:
    return matchers.json_params_matcher(data)


def get_todoist_api_patch(method: Callable[..., Any] | None) -> str:
    module = TodoistAPI.__module__
    name = TodoistAPI.__qualname__

    return f"{module}.{name}.{method.__name__}" if method else f"{module}.{name}"


T = TypeVar("T")


async def enumerate_async(
    iterable: AsyncIterable[T], start: int = 0
) -> AsyncIterator[tuple[int, T]]:
    index = start
    async for value in iterable:
        yield index, value
        index += 1