Passed
Push — dev ( 40dcc8...10ffc2 )
by Konstantinos
01:20
created

test_singleton.test_singleton()   A

Complexity

Conditions 4

Size

Total Lines 30
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 30
rs 9.256
c 0
b 0
f 0
cc 4
nop 0
1
2
def test_singleton():
3
    from green_magic.utils import Singleton
4
5
    class ObjectRegistry(Singleton):
6
        def __new__(cls, *args, **kwargs):
7
            x = super().__new__(cls)
8
            if not getattr(x, 'objects', None):
9
                x.objects = {}
10
            return x
11
12
    reg1 = ObjectRegistry()
13
    reg1.objects['a'] = 1
14
    reg2 = ObjectRegistry()
15
    assert reg2.objects == {'a': 1}
16
    reg2.objects['b'] = 2
17
    reg3 = ObjectRegistry()
18
19
    rs = [reg1, reg2, reg3]
20
21
    assert id(reg1) == id(reg2) == id(reg3)
22
    for i in rs:
23
        print(i.objects)
24
        assert i.objects == {'a': 1, 'b': 2}
25
    assert all(x.objects == {'a': 1,
26
                             'b': 2} for x in rs)
27
    del reg3.objects['a']
28
    assert all(x.objects == {'b': 2} for x in rs)
29
    for i in rs:
30
        print(i.objects)
31
        assert i.objects == {'b': 2}
32