| Conditions | 1 |
| Total Lines | 77 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | import pytest |
||
| 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 |