| Total Complexity | 3 |
| Total Lines | 27 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | def test_singleton(): |
||
| 2 | from so_magic.utils import Singleton |
||
| 3 | |||
| 4 | class ObjectRegistry(metaclass=Singleton): |
||
| 5 | def __init__(self): |
||
| 6 | super().__init__() |
||
| 7 | self.objects = {} |
||
| 8 | |||
| 9 | reg1 = ObjectRegistry() |
||
| 10 | reg1.objects['a'] = 1 |
||
| 11 | reg2 = ObjectRegistry() |
||
| 12 | assert reg2.objects == {'a': 1} |
||
| 13 | reg2.objects['b'] = 2 |
||
| 14 | reg3 = ObjectRegistry() |
||
| 15 | |||
| 16 | rs = [reg1, reg2, reg3] |
||
| 17 | |||
| 18 | assert id(reg1) == id(reg2) == id(reg3) |
||
| 19 | for i in rs: |
||
| 20 | assert i.objects == {'a': 1, 'b': 2} |
||
| 21 | assert all(x.objects == {'a': 1, |
||
| 22 | 'b': 2} for x in rs) |
||
| 23 | del reg3.objects['a'] |
||
| 24 | assert all(x.objects == {'b': 2} for x in rs) |
||
| 25 | for i in rs: |
||
| 26 | assert i.objects == {'b': 2} |
||
| 27 |