Coverage for src/tests/fixtures/training/trainings.py: 100%
48 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 trainings."""
3import pytest
5from kwai_bc_training.coaches.coach import CoachEntity
6from kwai_bc_training.trainings.training import (
7 TrainingCoachEntity,
8 TrainingEntity,
9)
10from kwai_bc_training.trainings.training_db_repository import TrainingDbRepository
11from kwai_core.db.database import Database
12from kwai_core.db.uow import UnitOfWork
13from kwai_core.domain.value_objects.owner import Owner
14from kwai_core.domain.value_objects.period import Period
15from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
18@pytest.fixture
19def make_text(
20 *,
21 locale: Locale | None = None,
22 format_: DocumentFormat | None = None,
23 title: str = "Training Test",
24 content: str = "This is a test training",
25 summary: str = "Test",
26 owner: Owner,
27):
28 """A factory fixture for a text."""
30 def _make_text() -> LocaleText:
31 return LocaleText(
32 title=title,
33 content=content,
34 summary=summary,
35 locale=locale or Locale.NL,
36 format=format_ or DocumentFormat.MARKDOWN,
37 author=owner,
38 )
40 return _make_text
43@pytest.fixture
44def make_training_coach(make_coach, owner: Owner):
45 """A factory fixture for a training coach."""
47 def _make_training_coach(coach: CoachEntity | None = None) -> TrainingCoachEntity:
48 if coach is None:
49 # make_coach returns a coach entity from the club module, so we need
50 # to convert it to one for the training module.
51 club_coach = make_coach()
52 coach = CoachEntity(
53 id=club_coach.id,
54 name=club_coach.name,
55 active=club_coach.active,
56 uuid=club_coach.uuid,
57 head=False,
58 )
59 return TrainingCoachEntity(coach=coach, owner=owner)
61 return _make_training_coach
64@pytest.fixture
65def make_training(make_text, make_training_coach):
66 """A factory fixture for a training."""
68 def _make_training(
69 text: LocaleText | None = None,
70 coach: TrainingCoachEntity | None = None,
71 period: Period | None = None,
72 active: bool = True,
73 ) -> TrainingEntity:
74 coach = coach or make_training_coach()
75 text = text or make_text()
76 period = period or Period.create_from_delta(hours=2)
77 return TrainingEntity(
78 texts=(text,),
79 coaches=frozenset((coach,)),
80 season=None,
81 period=period,
82 active=active,
83 )
85 return _make_training
88@pytest.fixture
89async def make_training_in_db(
90 request, database: Database, make_training, make_coach_in_db, owner
91):
92 """A factory fixture for a training in the database."""
93 created_entities = []
95 async def _make_training_in_db(
96 training: TrainingEntity | None = None,
97 coach: TrainingCoachEntity | None = None,
98 ) -> TrainingEntity:
99 if coach is None:
100 club_coach = await make_coach_in_db()
101 coach = TrainingCoachEntity(
102 coach=CoachEntity(
103 id=club_coach.id,
104 name=club_coach.name,
105 active=True,
106 uuid=club_coach.uuid,
107 head=False,
108 ),
109 owner=owner,
110 )
111 training = training or make_training(coach=coach)
113 repo = TrainingDbRepository(database)
114 async with UnitOfWork(database):
115 training = await repo.create(training)
116 created_entities.append(training)
118 return training
120 yield _make_training_in_db
122 repo = TrainingDbRepository(database)
123 for entity in created_entities:
124 async with UnitOfWork(database):
125 await repo.delete(entity)