-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathtest_decorators.py
More file actions
36 lines (28 loc) · 886 Bytes
/
Copy pathtest_decorators.py
File metadata and controls
36 lines (28 loc) · 886 Bytes
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
from unittest.mock import MagicMock
import pytest
from python_utils.decorators import sample
@pytest.fixture
def random(monkeypatch):
mock = MagicMock()
monkeypatch.setattr("python_utils.decorators.random.random", mock, raising=True)
return mock
def test_sample_called(random):
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):
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