Coverage for bc/kwai-bc-identity/src/kwai_bc_identity/users/user_account_repository.py: 100%

9 statements  

« prev     ^ index     » next       coverage.py v7.11.0, created at 2024-01-01 00:00 +0000

1"""Module that defines a repository for a user account.""" 

2 

3from abc import abstractmethod 

4from collections.abc import AsyncGenerator 

5 

6from kwai_core.domain.value_objects.email_address import EmailAddress 

7from kwai_core.domain.value_objects.unique_id import UniqueId 

8 

9from kwai_bc_identity.users.user_account import UserAccountEntity 

10from kwai_bc_identity.users.user_account_query import UserAccountQuery 

11 

12 

13class UserAccountRepository: 

14 """Interface for a user account repository.""" 

15 

16 @abstractmethod 

17 def get_all( 

18 self, 

19 query: UserAccountQuery | None = None, 

20 limit: int | None = None, 

21 offset: int | None = None, 

22 ) -> AsyncGenerator[UserAccountEntity, None]: 

23 """Return all user accounts. 

24 

25 Args: 

26 query: Query to filter user accounts. 

27 limit: The maximum number of entities to return. 

28 offset: Skip the offset rows before beginning to return entities. 

29 

30 Yields: 

31 A list of user account entities. 

32 """ 

33 raise NotImplementedError 

34 

35 @abstractmethod 

36 def create_query(self) -> UserAccountQuery: 

37 """Return a new user account query. 

38 

39 Returns: 

40 A query for user accounts. 

41 """ 

42 raise NotImplementedError 

43 

44 @abstractmethod 

45 async def get_user_by_email(self, email: EmailAddress) -> UserAccountEntity: 

46 """Get a user account with the given email address. 

47 

48 Raises: 

49 UserAccountNotFoundException: If no user account with the given email address exists. 

50 """ 

51 raise NotImplementedError 

52 

53 async def exists_with_email(self, email: EmailAddress) -> bool: 

54 """Check if a user account with the given email address already exists. 

55 

56 Args: 

57 email: The email address to check. 

58 

59 Returns: 

60 True when a user with the given email address exists. 

61 """ 

62 raise NotImplementedError 

63 

64 @abstractmethod 

65 async def get_user_by_uuid(self, uuid: UniqueId) -> UserAccountEntity: 

66 """Get a user account using the unique id. 

67 

68 Raises: 

69 UserAccountNotFoundException: If no user account with the given uuid exists. 

70 """ 

71 raise NotImplementedError 

72 

73 @abstractmethod 

74 async def create(self, user_account: UserAccountEntity) -> UserAccountEntity: 

75 """Save a new user account.""" 

76 raise NotImplementedError 

77 

78 @abstractmethod 

79 async def update(self, user_account: UserAccountEntity): 

80 """Save a user account.""" 

81 raise NotImplementedError 

82 

83 @abstractmethod 

84 async def delete(self, user_account): 

85 """Delete a user account.""" 

86 raise NotImplementedError 

87 

88 

89class UserAccountNotFoundException(Exception): 

90 """Raised when a user account cannot be found."""