Coverage for src/kwai/modules/teams/repositories/_tables.py: 99%

68 statements  

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

1from dataclasses import dataclass 

2from datetime import date, datetime 

3from typing import Self 

4 

5from kwai.core.db.table_row import TableRow 

6from kwai.core.domain.value_objects.timestamp import Timestamp 

7from kwai.core.domain.value_objects.traceable_time import TraceableTime 

8from kwai.core.domain.value_objects.unique_id import UniqueId 

9from kwai.modules.club.domain.country import CountryEntity, CountryIdentifier 

10from kwai.modules.teams.domain.team import TeamEntity, TeamIdentifier 

11from kwai.modules.teams.domain.team_member import MemberEntity, TeamMember 

12 

13 

14@dataclass(kw_only=True, frozen=True, slots=True) 

15class TeamRow(TableRow): 

16 """Represents a row of the teams table.""" 

17 

18 __table_name__ = "teams" 

19 

20 id: int 

21 name: str 

22 season_id: int | None 

23 team_category_id: int | None 

24 active: int 

25 remark: str | None 

26 created_at: datetime 

27 updated_at: datetime | None 

28 

29 def create_entity(self, team_members: dict[UniqueId, TeamMember]) -> TeamEntity: 

30 return TeamEntity( 

31 id_=TeamIdentifier(self.id), 

32 name=self.name, 

33 active=self.active == 1, 

34 remark=self.remark or "", 

35 members=team_members, 

36 traceable_time=TraceableTime( 

37 created_at=Timestamp.create_utc(self.created_at), 

38 updated_at=Timestamp.create_utc(self.updated_at), 

39 ), 

40 ) 

41 

42 @classmethod 

43 def persist(cls, team: TeamEntity) -> Self: 

44 return cls( 

45 id=team.id.value, 

46 name=team.name, 

47 season_id=None, 

48 team_category_id=None, 

49 active=1 if team.is_active else 0, 

50 remark=team.remark, 

51 created_at=team.traceable_time.created_at.timestamp, # type: ignore[arg-type] 

52 updated_at=team.traceable_time.updated_at.timestamp, 

53 ) 

54 

55 

56@dataclass(kw_only=True, frozen=True, slots=True) 

57class MemberRow(TableRow): 

58 """Represents a row of the members table used for a team member.""" 

59 

60 __table_name__ = "judo_members" 

61 

62 id: int | None 

63 uuid: str | None 

64 license: str | None 

65 license_end_date: date | None 

66 person_id: int | None 

67 active: int | None 

68 

69 

70@dataclass(kw_only=True, frozen=True, slots=True) 

71class MemberPersonRow(TableRow): 

72 """Represents a row of the persons table used for a team member.""" 

73 

74 __table_name__ = "persons" 

75 

76 id: int | None 

77 firstname: str | None 

78 lastname: str | None 

79 gender: int | None 

80 birthdate: date | None 

81 nationality_id: int | None 

82 

83 

84@dataclass(kw_only=True, frozen=True, slots=True) 

85class TeamMemberRow(TableRow): 

86 """Represents a row of the team members table.""" 

87 

88 __table_name__ = "team_members" 

89 

90 team_id: int | None 

91 member_id: int | None 

92 active: int | None 

93 created_at: datetime | None 

94 updated_at: datetime | None 

95 

96 def create_team_member(self, member: MemberEntity): 

97 return TeamMember( 

98 active=True if self.active == 1 else False, 

99 member=member, 

100 traceable_time=TraceableTime( 

101 created_at=Timestamp.create_utc(self.created_at), 

102 updated_at=Timestamp.create_utc(self.updated_at), 

103 ), 

104 ) 

105 

106 @classmethod 

107 def persist(cls, team: TeamEntity, team_member: TeamMember) -> Self: 

108 """Persist a team member to the table row.""" 

109 return cls( 

110 team_id=team.id.value, 

111 member_id=team_member.member.id.value, 

112 active=1 if team_member.active else 0, 

113 created_at=team_member.traceable_time.created_at.timestamp, # type: ignore[arg-type] 

114 updated_at=team_member.traceable_time.updated_at.timestamp, 

115 ) 

116 

117 

118@dataclass(kw_only=True, frozen=True, slots=True) 

119class CountryRow(TableRow): 

120 """Represent a row of the countries table. 

121 

122 Attributes: 

123 id: The id of the country. 

124 iso_2: The ISO 2 code of the country. 

125 iso_3: The ISO 3 code of the country. 

126 """ 

127 

128 __table_name__ = "countries" 

129 

130 id: int | None = None 

131 iso_2: str | None 

132 iso_3: str | None 

133 name: str | None 

134 created_at: datetime | None 

135 updated_at: datetime | None 

136 

137 def create_country(self) -> CountryEntity: 

138 """Create a Country value object from the row. 

139 

140 Returns: 

141 A country value object. 

142 """ 

143 return CountryEntity( 

144 id_=CountryIdentifier(self.id), 

145 iso_2=self.iso_2, 

146 iso_3=self.iso_3, 

147 name=self.name, 

148 )