Coverage for src/tests/modules/identity/users/test_user_db_repository.py: 100%
37 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module with tests for the user database repository."""
3from random import randint
5import pytest
7from kwai_bc_identity.users.user import UserEntity
8from kwai_bc_identity.users.user_account import UserAccountEntity
9from kwai_bc_identity.users.user_account_db_repository import (
10 UserAccountDbRepository,
11)
12from kwai_bc_identity.users.user_db_repository import UserDbRepository
13from kwai_bc_identity.users.user_repository import (
14 UserNotFoundException,
15 UserRepository,
16)
17from kwai_core.db.database import Database
18from kwai_core.domain.value_objects.email_address import EmailAddress
19from kwai_core.domain.value_objects.name import Name
20from kwai_core.domain.value_objects.password import Password
21from kwai_core.domain.value_objects.unique_id import UniqueId
24pytestmark = pytest.mark.db
27@pytest.fixture(scope="module")
28def repo(database: Database) -> UserRepository:
29 """Fixture for creating a repository."""
30 return UserDbRepository(database)
33@pytest.fixture(scope="module")
34async def user_account(database):
35 """Fixture for creating a user account.
37 The user repository does not provide a way to create a user. So, we need
38 to use the UserAccountRepository here.
39 """
40 number = randint(1, 99)
41 email = f"jigoro.kano{number:02d}@kwai.com"
42 user_account = UserAccountEntity(
43 password=Password.create_from_string("Test1234"),
44 user=UserEntity(
45 uuid=UniqueId.generate(),
46 email=EmailAddress(email),
47 name=Name(first_name="Jigoro", last_name="Kano"),
48 ),
49 )
50 return await UserAccountDbRepository(database).create(user_account)
53def test_create(user_account: UserAccountEntity):
54 """Test if the user account is created."""
55 assert user_account.id, "There should be a user account entity"
58async def test_get_by_id(repo: UserRepository, user_account: UserAccountEntity):
59 """Test if the user can be fetched with an id."""
60 result = await repo.get_user_by_id(user_account.id)
61 assert result, "There should be a user with the given id"
64async def test_get_by_uuid(repo: UserRepository, user_account: UserAccountEntity):
65 """Test if the user can be fetched with an uuid."""
66 result = await repo.get_user_by_uuid(user_account.user.uuid)
67 assert result, "There should be a user with the given uuid"
70async def test_get_by_email(repo: UserRepository, user_account: UserAccountEntity):
71 """Test if the user can be fetched with email address."""
72 result = await repo.get_user_by_email(user_account.user.email)
73 assert result, "There should be a user with the given email"
76async def test_delete(
77 repo: UserRepository, database: Database, user_account: UserAccountEntity
78):
79 """Test if the user can be deleted."""
80 await UserAccountDbRepository(database).delete(user_account)
82 with pytest.raises(UserNotFoundException):
83 await repo.get_user_by_email(user_account.user.email)