| Conditions | 4 |
| Total Lines | 30 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |