Coverage for src/tests/fixtures/portal/authors.py: 100%
28 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 for defining factory fixtures for authors."""
3import pytest
5from kwai_bc_identity.users.user_account import UserAccountEntity
6from kwai_bc_portal.domain.author import AuthorEntity, AuthorIdentifier
7from kwai_bc_portal.repositories.author_db_repository import AuthorDbRepository
8from kwai_core.db.database import Database
9from kwai_core.db.uow import UnitOfWork
12@pytest.fixture
13def make_author(make_user_account):
14 """A factory fixture for creating an author."""
16 def _make_author(
17 user_account: UserAccountEntity | None = None,
18 name: str = "",
19 active: bool = True,
20 remark: str = "",
21 ) -> AuthorEntity:
22 the_user_account = user_account or make_user_account()
23 return AuthorEntity(
24 id=AuthorIdentifier(the_user_account.id),
25 uuid=the_user_account.user.uuid,
26 name=name,
27 active=active,
28 remark=remark,
29 )
31 return _make_author
34@pytest.fixture
35async def make_author_in_db(database: Database, make_author, make_user_account_in_db):
36 """A factory fixture for creating an author in the database."""
37 created_entities = []
39 async def _make_author_in_db(
40 user_account: UserAccountEntity | None = None,
41 author: AuthorEntity | None = None,
42 ) -> AuthorEntity:
43 user_account = user_account or await make_user_account_in_db()
44 author = author or make_author(user_account)
46 repo = AuthorDbRepository(database)
47 async with UnitOfWork(database):
48 author = await repo.create(author)
49 created_entities.append(author)
51 return author
53 yield _make_author_in_db
55 repo = AuthorDbRepository(database)
56 for entity in created_entities:
57 async with UnitOfWork(database):
58 await repo.delete(entity)