Coverage for src/lexigram/auth/policies/in_memory_store.py: 46%

26 statements  

« prev     ^ index     » next       coverage.py v7.15.4, created at 2026-08-25 12:26 +0800

1"""In-memory policy store for development and testing.""" 

2 

3from __future__ import annotations 

4 

5from typing import TYPE_CHECKING 

6 

7if TYPE_CHECKING: 

8 from lexigram.auth.policies.types import Policy 

9 

10 

11class InMemoryPolicyStore: 

12 """In-memory policy storage for development and testing. 

13 

14 This store is NOT suitable for production as it does not persist data 

15 across restarts. 

16 """ 

17 

18 def __init__(self) -> None: 

19 self._policies: dict[str, Policy] = {} 

20 

21 async def get_all(self) -> list[Policy]: 

22 """Get all policies.""" 

23 return list(self._policies.values()) 

24 

25 async def get_by_id(self, policy_id: str) -> Policy | None: 

26 """Get a policy by ID.""" 

27 return self._policies.get(policy_id) 

28 

29 async def get_by_name(self, name: str) -> Policy | None: 

30 """Get a policy by name.""" 

31 for policy in self._policies.values(): 

32 if policy.name == name: 

33 return policy 

34 return None 

35 

36 async def save(self, policy: Policy) -> None: 

37 """Save a policy.""" 

38 self._policies[policy.policy_id] = policy 

39 

40 async def delete(self, policy_id: str) -> bool: 

41 """Delete a policy.""" 

42 if policy_id in self._policies: 

43 del self._policies[policy_id] 

44 return True 

45 return False 

46 

47 async def exists(self, policy_id: str) -> bool: 

48 """Check if a policy exists.""" 

49 return policy_id in self._policies 

50 

51 def clear(self) -> None: 

52 """Clear all policies.""" 

53 self._policies.clear() 

54 

55 

56__all__ = [ 

57 "InMemoryPolicyStore", 

58]