decorate_three_times()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 6
rs 10
1
from sacred.stflow.internal import ContextMethodDecorator
2
3
4
def test_context_method_decorator():
5
    " Ensure that ContextMethodDecorator can intercept method calls "
6
    class FooClass():
7
        def __init__(self, x):
8
            self.x = x
9
10
        def do_foo(self, y, z):
11
            print("foo")
12
            print(y)
13
            print(z)
14
            return y * self.x + z
15
16
    def decorate_three_times(instance, original_method, original_args,
17
                             original_kwargs):
18
        print("three_times")
19
        print(original_args)
20
        print(original_kwargs)
21
        return original_method(instance, *original_args, **original_kwargs) * 3
22
23
    with ContextMethodDecorator(FooClass, "do_foo", decorate_three_times):
24
        foo = FooClass(10)
25
        assert foo.do_foo(5, 6) == (5 * 10 + 6) * 3
26
        assert foo.do_foo(5, z=6) == (5 * 10 + 6) * 3
27
        assert foo.do_foo(y=5, z=6) == (5 * 10 + 6) * 3
28
    assert foo.do_foo(5, 6) == (5 * 10 + 6)
29
    assert foo.do_foo(5, z=6) == (5 * 10 + 6)
30
    assert foo.do_foo(y=5, z=6) == (5 * 10 + 6)
31
32
    def decorate_three_times_with_exception(instance, original_method,
33
                                            original_args, original_kwargs):
34
        raise RuntimeError("This should be caught")
35
36
    exception = False
37
    try:
38
        with ContextMethodDecorator(FooClass, "do_foo",
39
                                    decorate_three_times_with_exception):
40
            foo = FooClass(10)
41
            this_should_raise_exception = foo.do_foo(5, 6)
42
    except RuntimeError:
43
        exception = True
44
    assert foo.do_foo(5, 6) == (5 * 10 + 6)
45
    assert exception is True
46