test_memoize   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 26
dl 0
loc 32
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A test_simple_memoize() 0 12 1
A simple_memoize() 0 14 1
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
        )
33
    )
34