Coverage for src/tests/modules/identity/users/test_user_account_db_repository.py: 100%

41 statements  

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

1"""Module for testing the user account database repository.""" 

2 

3import pytest 

4 

5from kwai_bc_identity.users.user_account_db_repository import ( 

6 UserAccountDbRepository, 

7) 

8from kwai_bc_identity.users.user_account_repository import ( 

9 UserAccountNotFoundException, 

10 UserAccountRepository, 

11) 

12from kwai_core.db.database import Database 

13from kwai_core.db.uow import UnitOfWork 

14 

15 

16pytestmark = pytest.mark.db 

17 

18 

19@pytest.fixture(scope="module") 

20def repo(database: Database) -> UserAccountRepository: 

21 """Fixture for creating the repository.""" 

22 return UserAccountDbRepository(database) 

23 

24 

25async def test_create(make_user_account_in_db): 

26 """Test if the user account is created.""" 

27 user_account = await make_user_account_in_db() 

28 assert user_account.id, "There should be a user account entity" 

29 

30 

31async def test_get_all(repo: UserAccountRepository, make_user_account_in_db): 

32 """Test if user accounts are retrieved.""" 

33 await make_user_account_in_db() 

34 user_account = await anext(repo.get_all()) 

35 assert user_account is not None, "There should be a user account entity" 

36 

37 

38async def test_get_by_email(repo: UserAccountRepository, make_user_account_in_db): 

39 """Test if the user account can be fetched with email address.""" 

40 user_account = await make_user_account_in_db() 

41 result = await repo.get_user_by_email(user_account.user.email) 

42 assert result, "There should be a user account with the given email" 

43 

44 

45async def test_exists_with_email(repo: UserAccountRepository, make_user_account_in_db): 

46 """Test if the user account exists with the given email address.""" 

47 user_account = await make_user_account_in_db() 

48 exists = await repo.exists_with_email(user_account.user.email) 

49 assert exists, "There should be a user account with the given email" 

50 

51 

52async def test_get_by_uuid(repo: UserAccountRepository, make_user_account_in_db): 

53 """Test if the user account can be fetched with a unique id.""" 

54 user_account = await make_user_account_in_db() 

55 result = await repo.get_user_by_uuid(user_account.user.uuid) 

56 assert result, "There should be a user account with the given uuid" 

57 

58 

59async def test_update( 

60 database: Database, repo: UserAccountRepository, make_user_account_in_db 

61): 

62 """Test if the user account can be updated.""" 

63 user_account = await make_user_account_in_db() 

64 user_account = user_account.revoke() 

65 async with UnitOfWork(database): 

66 await repo.update(user_account) 

67 result = await repo.get_user_by_email(user_account.user.email) 

68 assert result.revoked is True, "The user should be revoked" 

69 

70 

71async def test_delete( 

72 database: Database, repo: UserAccountRepository, make_user_account_in_db 

73): 

74 """Test if the user account can be deleted.""" 

75 user_account = await make_user_account_in_db() 

76 async with UnitOfWork(database): 

77 await repo.delete(user_account) 

78 

79 with pytest.raises(UserAccountNotFoundException): 

80 await repo.get_user_by_email(user_account.user.email)