Coverage for src/kwai/core/domain/use_case.py: 90%
31 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 for defining common classes, functions, ... for use cases."""
3from abc import ABC, abstractmethod
4from dataclasses import dataclass
5from typing import AsyncIterator, NamedTuple
8class UseCaseResult(ABC):
9 """Base class for a use case result."""
11 @abstractmethod
12 def to_message(self) -> str:
13 """Return a message from the result."""
14 raise NotImplementedError
17@dataclass(kw_only=True, frozen=True, slots=True)
18class NotFoundResult[T](UseCaseResult):
19 """A result that indicates that an entity was not found."""
21 entity_name: str
22 key: T
24 def to_message(self) -> str:
25 return f"{self.entity_name} with key '{self.key}' not found."
28@dataclass(kw_only=True, frozen=True, slots=True)
29class EntitiesResult[T](UseCaseResult):
30 """A result that returns an iterator for entities and the number of entities."""
32 count: int
33 entities: AsyncIterator[T]
35 def to_message(self) -> str:
36 return f"{self.count} entities."
39@dataclass(kw_only=True, frozen=True, slots=True)
40class EntityResult[T](UseCaseResult):
41 """A result that returns an entity."""
43 entity: T
45 def to_message(self) -> str:
46 return str(self.entity)
49class UseCaseBrowseResult(NamedTuple):
50 """A named tuple for a use case that returns a result of browsing entities."""
52 count: int
53 iterator: AsyncIterator
56@dataclass(kw_only=True, frozen=True, slots=True)
57class TextCommand:
58 """Input for a text."""
60 locale: str
61 format: str
62 title: str
63 summary: str
64 content: str