diff --git a/async_tests/test_async_ldap_model.py b/async_tests/test_async_ldap_model.py index 193abb8..cca7d5b 100644 --- a/async_tests/test_async_ldap_model.py +++ b/async_tests/test_async_ldap_model.py @@ -244,6 +244,17 @@ def test_Hackers(client_fixture_multi): assert repr(h) == f"" +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"" +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"" +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) diff --git a/src/phi/async_ldap/model.py b/src/phi/async_ldap/model.py index 9c6be8e..473c192 100644 --- a/src/phi/async_ldap/model.py +++ b/src/phi/async_ldap/model.py @@ -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)