Completed
Push — master ( 178087...13f71d )
by Konstantinos
25s queued 12s
created

test_proxy.test_proxy_behaviour()   B

Complexity

Conditions 1

Size

Total Lines 62
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 62
rs 8.968
c 0
b 0
f 0
cc 1
nop 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import pytest
2
3
@pytest.fixture
4
def proxy_module():
5
    from software_patterns import proxy
6
    return proxy
7
8
9
@pytest.fixture
10
def dummy_handle():
11
    def handle(self, *args, **kwargs):
12
        return f'{type(self).__name__} handle request with args [{", ".join((str(_) for _ in args))}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]'
13
    return handle
14
15
16
def test_proxy_behaviour(proxy_module, dummy_handle, capsys):
17
    prm = proxy_module
18
19
    # replicate client code that wants to use the proxy pattern
20
21
    # Derive from class RealSubject or use 'python overloading' (use a class 
22
    # that has a 'request' method with the same signature as ReadSubject.request)
23
    class ClientSubject(prm.ProxySubject):
24
        def request(self, *args, **kwargs):
25
            # delegate handling to the real real subject from client code
26
            # so frankly the request method here is also an adapter
27
            print(dummy_handle(self, *args, **kwargs))
28
            super().request(*args, **kwargs)
29
            return type(self).__name__
30
31
    # Derive from Proxy
32
    class ClientProxy(prm.Proxy):
33
34
        def request(self, *args, **kwargs):
35
36
            # run proxy code before sending request to the "proxied" handler
37
            before_args = list(['before'] + list(args))
38
            print(dummy_handle(self, *before_args, **kwargs))
39
40
            # handle request with the proxied logic
41
            _ = super().request(*args, **kwargs)
42
            assert _ == 'ClientSubject'
43
44
            # run proxy code after request to the "proxied" handler
45
            after_args = list(['after'] + list(args))
46
            print(dummy_handle(self, *after_args, **kwargs))
47
            return _
48
    def _dummy_callback(*args, **kwargs):
49
        return None
50
    real_subject = ClientSubject(_dummy_callback)
51
    proxy = ClientProxy(real_subject)
52
53
    # use proxy in a scenario
54
55
    # First test what happens without using proxy
56
    args = [1, 2]
57
    kwargs = {'k1': 'v1'}
58
    result = real_subject.request(*args, **kwargs)
59
60
    captured = capsys.readouterr()
61
    assert captured.out == dummy_handle(real_subject, 1, 2, k1='v1') + '\n'
62
    assert captured.out == f'ClientSubject handle request with args [{", ".join(str(_) for _ in args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n'
63
    assert result == type(real_subject).__name__
64
    assert result == 'ClientSubject'
65
66
    # Now test what happens using proxy
67
    result = proxy.request(*args, **kwargs)
68
69
    captured = capsys.readouterr()
70
    assert captured.out == dummy_handle(*list([proxy, 'before'] + args), **kwargs) +'\n' + \
71
        dummy_handle(*list([real_subject] + args), **kwargs) + '\n' + \
72
        dummy_handle(*list([proxy, 'after'] + args), **kwargs) +'\n'
73
    assert captured.out == f'ClientProxy handle request with args [{", ".join(str(_) for _ in ["before"] + args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n' + \
74
        f'ClientSubject handle request with args [{", ".join(str(_) for _ in args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n' + \
75
        f'ClientProxy handle request with args [{", ".join(str(_) for _ in ["after"] + args)}] and kwargs [{", ".join(f"{k}={v}" for k, v in kwargs.items())}]\n'
76
    assert result == type(real_subject).__name__
77
    assert result == 'ClientSubject'
78