Passed
Push — dev ( 40dcc8...10ffc2 )
by Konstantinos
01:20
created

test_transformations.test_transform_method()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
cc 1
nop 5
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