File: decorators.py

package info (click to toggle)
python-beanie 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 14,427; makefile: 7; sh: 6
file content (81 lines) | stat: -rw-r--r-- 2,374 bytes parent folder | download | duplicates (2)
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
from typing import Any, Callable, Coroutine

from typing_extensions import Protocol, TypeAlias, assert_type

from beanie import Document
from beanie.odm.actions import EventTypes, wrap_with_actions
from beanie.odm.utils.self_validation import validate_self_before
from beanie.odm.utils.state import (
    previous_saved_state_needed,
    save_state_after,
    saved_state_needed,
)


def sync_func(doc_self: Document, arg1: str, arg2: int, /) -> Document:
    """
    Models `Document` sync method that expects self
    """
    raise NotImplementedError


SyncFunc: TypeAlias = Callable[[Document, str, int], Document]


async def async_func(doc_self: Document, arg1: str, arg2: int, /) -> Document:
    """
    Models `Document` async method that expects self
    """
    raise NotImplementedError


AsyncFunc: TypeAlias = Callable[
    [Document, str, int], Coroutine[Any, Any, Document]
]


def test_wrap_with_actions_preserves_signature() -> None:
    assert_type(async_func, AsyncFunc)
    assert_type(wrap_with_actions(EventTypes.SAVE)(async_func), AsyncFunc)


def test_save_state_after_preserves_signature() -> None:
    assert_type(async_func, AsyncFunc)
    assert_type(save_state_after(async_func), AsyncFunc)


def test_validate_self_before_preserves_signature() -> None:
    assert_type(async_func, AsyncFunc)
    assert_type(validate_self_before(async_func), AsyncFunc)


def test_saved_state_needed_preserves_signature() -> None:
    assert_type(async_func, AsyncFunc)
    assert_type(saved_state_needed(async_func), AsyncFunc)

    assert_type(sync_func, SyncFunc)
    assert_type(saved_state_needed(sync_func), SyncFunc)


def test_previous_saved_state_needed_preserves_signature() -> None:
    assert_type(async_func, AsyncFunc)
    assert_type(previous_saved_state_needed(async_func), AsyncFunc)

    assert_type(sync_func, SyncFunc)
    assert_type(previous_saved_state_needed(sync_func), SyncFunc)


class ExpectsDocumentSelf(Protocol):
    def __call__(self, doc_self: Document, /) -> Any: ...


def test_document_insert_expects_self() -> None:
    test_insert: ExpectsDocumentSelf = Document.insert  # noqa: F841


def test_document_save_expects_self() -> None:
    test_insert: ExpectsDocumentSelf = Document.save  # noqa: F841


def test_document_replace_expects_self() -> None:
    test_insert: ExpectsDocumentSelf = Document.replace  # noqa: F841