File: test_decorators.py

package info (click to toggle)
python-utils 3.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 396 kB
  • sloc: python: 2,135; makefile: 19; sh: 5
file content (71 lines) | stat: -rw-r--r-- 1,942 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
import typing
from unittest.mock import MagicMock

import pytest

from python_utils.decorators import sample, wraps_classmethod

T = typing.TypeVar('T')


@pytest.fixture
def random(monkeypatch: pytest.MonkeyPatch) -> MagicMock:
    mock = MagicMock()
    monkeypatch.setattr(
        'python_utils.decorators.random.random', mock, raising=True
    )
    return mock


def test_sample_called(random: MagicMock) -> None:
    demo_function = MagicMock()
    decorated = sample(0.5)(demo_function)
    random.return_value = 0.4
    decorated()
    random.return_value = 0.0
    decorated()
    args = [1, 2]
    kwargs = {'1': 1, '2': 2}
    decorated(*args, **kwargs)
    demo_function.assert_called_with(*args, **kwargs)
    assert demo_function.call_count == 3


def test_sample_not_called(random: MagicMock) -> None:
    demo_function = MagicMock()
    decorated = sample(0.5)(demo_function)
    random.return_value = 0.5
    decorated()
    random.return_value = 1.0
    decorated()
    assert demo_function.call_count == 0


class SomeClass:
    @classmethod
    def some_classmethod(cls, arg: T) -> T:
        return arg

    @classmethod
    def some_annotated_classmethod(cls, arg: int) -> int:
        return arg


def test_wraps_classmethod() -> None:
    some_class = SomeClass()
    some_class.some_classmethod = MagicMock()  # type: ignore[method-assign]
    wrapped_method = wraps_classmethod(SomeClass.some_classmethod)(
        some_class.some_classmethod
    )
    wrapped_method(123)
    some_class.some_classmethod.assert_called_with(123)


def test_wraps_annotated_classmethod() -> None:
    some_class = SomeClass()
    some_class.some_annotated_classmethod = MagicMock()  # type: ignore[method-assign]
    wrapped_method = wraps_classmethod(SomeClass.some_annotated_classmethod)(
        some_class.some_annotated_classmethod
    )
    wrapped_method(123)
    some_class.some_annotated_classmethod.assert_called_with(123)