Coverage for kwai/core/db/rows.py: 100%

31 statements  

« prev     ^ index     » next       coverage.py v7.3.0, created at 2023-09-05 17:55 +0000

1"""Module that defines common table classes.""" 

2from dataclasses import dataclass 

3from datetime import datetime 

4 

5from kwai.core.db.table import Table 

6from kwai.core.domain.value_objects.identifier import IntIdentifier 

7from kwai.core.domain.value_objects.local_timestamp import LocalTimestamp 

8from kwai.core.domain.value_objects.name import Name 

9from kwai.core.domain.value_objects.owner import Owner 

10from kwai.core.domain.value_objects.text import LocaleText 

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

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

13 

14 

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

16class OwnerRow: 

17 """Represent the owner data.""" 

18 

19 id: int 

20 uuid: str 

21 first_name: str 

22 last_name: str 

23 

24 def create_owner(self) -> Owner: 

25 """Create an Author value object from row data.""" 

26 return Owner( 

27 id=IntIdentifier(self.id), 

28 uuid=UniqueId.create_from_string(self.uuid), 

29 name=Name(first_name=self.first_name, last_name=self.last_name), 

30 ) 

31 

32 

33OwnersTable = Table("users", OwnerRow) 

34 

35 

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

37class ContentRow: 

38 """Represent a row for a content table. 

39 

40 Attributes: 

41 locale: The code of the locale of the text 

42 format: The format of the text (md = markdown, html, ...) 

43 title: The title of the news story 

44 content: The long content of the news story 

45 summary: A summary of the content 

46 user_id: The id of the author 

47 created_at: the timestamp of creation 

48 updated_at: the timestamp of the last modification 

49 """ 

50 

51 locale: str 

52 format: str 

53 title: str 

54 content: str 

55 summary: str 

56 user_id: int 

57 created_at: datetime 

58 updated_at: datetime | None 

59 

60 def create_content(self, author: Owner) -> LocaleText: 

61 """Create a Content value object from a row. 

62 

63 Args: 

64 author: The author of the content. 

65 """ 

66 return LocaleText( 

67 locale=self.locale, 

68 format=self.format, 

69 title=self.title, 

70 content=self.content, 

71 summary=self.summary, 

72 author=author, 

73 traceable_time=TraceableTime( 

74 created_at=LocalTimestamp(timestamp=self.created_at), 

75 updated_at=LocalTimestamp(timestamp=self.updated_at), 

76 ), 

77 )