Coverage for src/kwai/core/db/rows.py: 98%
41 statements
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.6.10, created at 2024-01-01 00:00 +0000
1"""Module that defines common table classes."""
3from dataclasses import dataclass
4from datetime import datetime
6from kwai.core.db.table import Table
7from kwai.core.db.table_row import TableRow
8from kwai.core.domain.value_objects.identifier import IntIdentifier
9from kwai.core.domain.value_objects.name import Name
10from kwai.core.domain.value_objects.owner import Owner
11from kwai.core.domain.value_objects.text import DocumentFormat, Locale, LocaleText
12from kwai.core.domain.value_objects.timestamp import Timestamp
13from kwai.core.domain.value_objects.traceable_time import TraceableTime
14from kwai.core.domain.value_objects.unique_id import UniqueId
17@dataclass(kw_only=True, frozen=True, slots=True)
18class OwnerRow:
19 """Represent the owner data."""
21 id: int
22 uuid: str
23 first_name: str
24 last_name: str
26 def create_owner(self) -> Owner:
27 """Create an Author value object from row data."""
28 return Owner(
29 id=IntIdentifier(self.id),
30 uuid=UniqueId.create_from_string(self.uuid),
31 name=Name(first_name=self.first_name, last_name=self.last_name),
32 )
35OwnersTable = Table("users", OwnerRow)
38@dataclass(kw_only=True, frozen=True, slots=True)
39class OwnerTableRow(TableRow):
40 """Represent the owner data."""
42 __table_name__ = "users"
44 id: int
45 uuid: str
46 first_name: str
47 last_name: str
49 def create_owner(self) -> Owner:
50 """Create an Author value object from row data."""
51 return Owner(
52 id=IntIdentifier(self.id),
53 uuid=UniqueId.create_from_string(self.uuid),
54 name=Name(first_name=self.first_name, last_name=self.last_name),
55 )
58@dataclass(kw_only=True, frozen=True, slots=True)
59class TextRow:
60 """Represent a row for a content table.
62 Attributes:
63 locale: The code of the locale of the text
64 format: The format of the text (md = markdown, html, ...)
65 title: The title of the text
66 content: The long content of the text
67 summary: A summary of the text
68 user_id: The id of the author
69 created_at: the timestamp of creation
70 updated_at: the timestamp of the last modification
71 """
73 locale: str
74 format: str
75 title: str
76 content: str
77 summary: str
78 user_id: int
79 created_at: datetime
80 updated_at: datetime | None
82 def create_text(self, author: Owner) -> LocaleText:
83 """Create a LocaleText value object from a row.
85 Args:
86 author: The author of the text.
87 """
88 return LocaleText(
89 locale=Locale(self.locale),
90 format=DocumentFormat(self.format),
91 title=self.title,
92 content=self.content,
93 summary=self.summary,
94 author=author,
95 traceable_time=TraceableTime(
96 created_at=Timestamp.create_utc(self.created_at),
97 updated_at=Timestamp.create_utc(self.updated_at),
98 ),
99 )