Total Complexity | 2 |
Total Lines | 32 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import pytest |
||
2 | |||
3 | |||
4 | @pytest.fixture |
||
5 | def simple_memoize(): |
||
6 | from software_patterns import ObjectsPool |
||
7 | |||
8 | class TestClass: |
||
9 | def __init__(self, number, name): |
||
10 | self.number = number |
||
11 | self.name = name |
||
12 | |||
13 | @staticmethod |
||
14 | def factory_method(arg1, arg2, **kwargs): |
||
15 | return TestClass(arg1, arg2) |
||
16 | |||
17 | return ObjectsPool(TestClass.factory_method) |
||
18 | |||
19 | |||
20 | def test_simple_memoize(simple_memoize): |
||
21 | runtime_args = (7, 'gg') |
||
22 | runtime_kwargs = dict(kwarg1='something', kwarg2=[1, 2]) |
||
23 | instance1 = simple_memoize.get_object(*runtime_args, **runtime_kwargs) |
||
24 | instance2 = simple_memoize.get_object(*runtime_args, **runtime_kwargs) |
||
25 | assert instance1 == instance2 |
||
26 | assert id(instance1) == id(instance2) |
||
27 | hash1 = simple_memoize._build_hash(*runtime_args, **runtime_kwargs) |
||
28 | assert hash1 == hash( |
||
29 | '-'.join( |
||
30 | [str(_) for _ in runtime_args] |
||
31 | + [f'{key}={str(value)}' for key, value in runtime_kwargs.items()] |
||
32 | ) |
||
34 |