Coverage for src/tests/fixtures/training/trainings.py: 100%

49 statements  

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

1"""Module for defining factory fixtures for trainings.""" 

2 

3import pytest 

4 

5from kwai_bc_training.coaches.coach import CoachEntity 

6from kwai_bc_training.trainings.training import TrainingEntity 

7from kwai_bc_training.trainings.training_db_repository import TrainingDbRepository 

8from kwai_bc_training.trainings.value_objects import TrainingCoach 

9from kwai_core.db.database import Database 

10from kwai_core.db.uow import UnitOfWork 

11from kwai_core.domain.value_objects.owner import Owner 

12from kwai_core.domain.value_objects.period import Period 

13from kwai_core.domain.value_objects.text import DocumentFormat, Locale, LocaleText 

14 

15 

16@pytest.fixture 

17def make_text( 

18 *, 

19 locale: Locale | None = None, 

20 format_: DocumentFormat | None = None, 

21 title: str = "Training Test", 

22 content: str = "This is a test training", 

23 summary: str = "Test", 

24 owner: Owner, 

25): 

26 """A factory fixture for a text.""" 

27 

28 def _make_text() -> LocaleText: 

29 return LocaleText( 

30 title=title, 

31 content=content, 

32 summary=summary, 

33 locale=locale or Locale.NL, 

34 format=format_ or DocumentFormat.MARKDOWN, 

35 author=owner, 

36 ) 

37 

38 return _make_text 

39 

40 

41@pytest.fixture 

42def make_training_coach(make_coach, owner: Owner): 

43 """A factory fixture for a training coach.""" 

44 

45 def _make_training_coach(coach: CoachEntity | None = None) -> TrainingCoach: 

46 if coach is None: 

47 # make_coach returns a coach entity from the club module, so we need 

48 # to convert it to one for the training module. 

49 club_coach = make_coach() 

50 coach = CoachEntity( 

51 id=club_coach.id, name=club_coach.name, active=club_coach.active 

52 ) 

53 return TrainingCoach(coach=coach, owner=owner) 

54 

55 return _make_training_coach 

56 

57 

58@pytest.fixture 

59def make_training(make_text, make_training_coach): 

60 """A factory fixture for a training.""" 

61 

62 def _make_training( 

63 text: LocaleText | None = None, 

64 coach: TrainingCoach | None = None, 

65 period: Period | None = None, 

66 active: bool = True, 

67 ) -> TrainingEntity: 

68 coach = coach or make_training_coach() 

69 text = text or make_text() 

70 period = period or Period.create_from_delta(hours=2) 

71 return TrainingEntity( 

72 texts=(text,), 

73 coaches=frozenset((coach,)), 

74 season=None, 

75 period=period, 

76 active=active, 

77 ) 

78 

79 return _make_training 

80 

81 

82@pytest.fixture 

83async def make_training_in_db( 

84 request, database: Database, make_training, make_coach_in_db, owner 

85): 

86 """A factory fixture for a training in the database.""" 

87 created_entities = [] 

88 

89 async def _make_training_in_db( 

90 training: TrainingEntity | None = None, 

91 coach: TrainingCoach | None = None, 

92 ) -> TrainingEntity: 

93 if coach is None: 

94 club_coach = await make_coach_in_db() 

95 coach = TrainingCoach( 

96 coach=CoachEntity(id=club_coach.id, name=club_coach.name, active=True), 

97 owner=owner, 

98 ) 

99 training = training or make_training(coach=coach) 

100 

101 repo = TrainingDbRepository(database) 

102 async with UnitOfWork(database): 

103 training = await repo.create(training) 

104 created_entities.append(training) 

105 

106 return training 

107 

108 yield _make_training_in_db 

109 

110 repo = TrainingDbRepository(database) 

111 for entity in created_entities: 

112 async with UnitOfWork(database): 

113 await repo.delete(entity)