|
1
|
|
|
import pytest |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
@pytest.fixture |
|
5
|
|
|
def transformers(): |
|
6
|
|
|
from green_magic.utils import Transformer |
|
7
|
|
|
def gg(a, b=2): |
|
8
|
|
|
return a * b + 1 |
|
9
|
|
|
return {'lambda': Transformer(lambda x: x + 1), 'gg': Transformer(gg)} |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
@pytest.mark.parametrize('transformer, args_list, kwargs_dict, expected_result', [ |
|
13
|
|
|
('lambda', [1], {}, 2), |
|
14
|
|
|
('lambda', [0], {}, 1), |
|
15
|
|
|
pytest.param('lambda', [0, 2], {}, 1, marks=pytest.mark.xfail(raises=TypeError, reason="The lambda accepts a single argument and not two; transform() takes 2 positional arguments but 3 were given")), |
|
16
|
|
|
('gg', [10], {}, 21), |
|
17
|
|
|
('gg', [10], {'b': 3}, 31), |
|
18
|
|
|
pytest.param('gg', [2], {'b': 2, 'f':8}, 5, marks=pytest.mark.xfail(raises=TypeError, reason="The 'gg' function only allows the 'b' keyword argument and not the 'f'.")), |
|
19
|
|
|
pytest.param('gg', [10, 20], {'b': 2}, 31, marks=pytest.mark.xfail()), |
|
20
|
|
|
]) |
|
21
|
|
|
|
|
22
|
|
|
def test_transform_method(transformer, args_list, kwargs_dict, expected_result, transformers): |
|
23
|
|
|
assert expected_result == transformers[transformer].transform(*args_list, **kwargs_dict) |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
@pytest.fixture(params=[ |
|
27
|
|
|
['instance', ValueError, lambda x: f"Expected a callable as argument; instead got '{type(x)}'"], |
|
28
|
|
|
['function', ValueError, lambda x: f"Expected a callable that receives at least one positional argument; instead got a callable that receives '{x.__code__.co_argcount}'"], |
|
29
|
|
|
]) |
|
30
|
|
|
def false_arguments(request): |
|
31
|
|
|
a = object() |
|
32
|
|
|
def f(): |
|
33
|
|
|
pass |
|
34
|
|
|
callables = {'instance': a, 'function': f} |
|
35
|
|
|
first_argument = callables[request.param[0]] |
|
36
|
|
|
return {'args': [first_argument], |
|
37
|
|
|
'exception': request.param[1], |
|
38
|
|
|
'exception_text': request.param[2](first_argument), |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
def test_false_arguments(false_arguments): |
|
43
|
|
|
from green_magic.utils import Transformer |
|
44
|
|
|
with pytest.raises(false_arguments['exception']) as e: |
|
45
|
|
|
_ = Transformer(*false_arguments['args']) |
|
46
|
|
|
assert false_arguments['exception_text'] == str(e) |
|
47
|
|
|
|