Improved singletonic models.

This commit is contained in:
sfigato 2019-07-01 22:37:56 +02:00
parent 706f109faf
commit f558492975
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
2 changed files with 51 additions and 15 deletions

View File

@ -244,6 +244,17 @@ def test_Hackers(client_fixture_multi):
assert repr(h) == f"<Hackers {h.kind}=Hackers,{BASE_DN}>"
def test_Hackers_singleton(client_fixture):
other_client = MockClient(BASE_DN)
h1 = Hackers(client_fixture)
h2 = Hackers(other_client)
h3 = Hackers(client_fixture)
assert client_fixture is not other_client
assert h1 is h3
assert h2 is not h1
@pytest.mark.asyncio
async def test_Entry_get_children(client_fixture_multi):
h = Hackers(client_fixture_multi.users)
@ -302,6 +313,17 @@ def test_Robots(client_fixture_multi):
assert repr(r) == f"<Services {r.kind}=Services,{BASE_DN}>"
def test_Robots_singleton(client_fixture):
other_client = MockClient(BASE_DN)
r1 = Robots(client_fixture)
r2 = Robots(other_client)
r3 = Robots(client_fixture)
assert client_fixture is not other_client
assert r1 is r3
assert r2 is not r1
@pytest.mark.asyncio
async def test_Robots_anext(client_fixture_multi):
r = Robots(client_fixture_multi.services)
@ -327,6 +349,17 @@ def test_Congregations(client_fixture_multi):
assert repr(g) == f"<Groups {g.kind}=Groups,{BASE_DN}>"
def test_Congregations_singleton(client_fixture):
other_client = MockClient(BASE_DN)
g1 = Congregations(client_fixture)
g2 = Congregations(other_client)
g3 = Congregations(client_fixture)
assert client_fixture is not other_client
assert g1 is g3
assert g2 is not g1
@pytest.mark.asyncio
async def test_Congregations_anext(client_fixture_multi):
c = Congregations(client_fixture_multi.groups)

View File

@ -123,12 +123,13 @@ class Hackers(Entry):
"""
kind = "ou"
_instance = None
_instances: T.Dict[str, Entry] = dict()
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
def __new__(cls, client, *args, **kwargs):
_name = f"{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
def __init__(self, client, *args, **kwargs):
super().__init__(client)
@ -160,12 +161,13 @@ class Robots(Entry):
kind = "ou"
_name = "Services"
_instance = None
_instances: T.Dict[str, Entry] = dict()
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
def __new__(cls, client, *args, **kwargs):
_name = f"{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
def __init__(self, client, *args, **kwargs):
super().__init__(client)
@ -186,12 +188,13 @@ class Congregations(Entry):
kind = "ou"
_name = "Groups"
_instance = None
_instances: T.Dict[str, Entry] = dict()
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = object.__new__(cls)
return cls._instance
def __new__(cls, client, *args, **kwargs):
_name = f"{id(client)}"
if _name not in cls._instances:
cls._instances[_name] = object.__new__(cls)
return cls._instances[_name]
def __init__(self, client, *args, **kwargs):
super().__init__(client)