Passed
Push — dev ( 996b14...d06756 )
by Konstantinos
03:56
created

test_proxy.proxy_module()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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