Coverage for kwai/modules/news/stories/story_repository.py: 100%

6 statements  

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

1"""Module that defines an interface for a story repository.""" 

2from abc import ABC, abstractmethod 

3from typing import AsyncIterator 

4 

5from kwai.modules.news.stories.story import StoryEntity, StoryIdentifier 

6from kwai.modules.news.stories.story_query import StoryQuery 

7 

8 

9class StoryNotFoundException(Exception): 

10 """Raised when the story can not be found.""" 

11 

12 

13class StoryRepository(ABC): 

14 """Interface for a story repository.""" 

15 

16 @abstractmethod 

17 async def create(self, story: StoryEntity) -> StoryEntity: 

18 """Create a new story entity. 

19 

20 Args: 

21 story: The story to create 

22 

23 Returns: 

24 The story entity with an identifier. 

25 """ 

26 raise NotImplementedError 

27 

28 @abstractmethod 

29 async def update(self, story: StoryEntity): 

30 """Update a story entity. 

31 

32 Args: 

33 story: The story to update. 

34 """ 

35 raise NotImplementedError 

36 

37 @abstractmethod 

38 async def delete(self, story: StoryEntity): 

39 """Delete a story. 

40 

41 Args: 

42 story: The story to delete. 

43 """ 

44 raise NotImplementedError 

45 

46 @abstractmethod 

47 def create_query(self) -> StoryQuery: 

48 """Create a query for querying stories.""" 

49 raise NotImplementedError 

50 

51 @abstractmethod 

52 async def get_by_id(self, id_: StoryIdentifier) -> StoryEntity: 

53 """Get the story with the given id. 

54 

55 Args: 

56 id_: The id of the story. 

57 

58 Returns: 

59 A story entity. 

60 """ 

61 raise NotImplementedError 

62 

63 @abstractmethod 

64 async def get_all( 

65 self, 

66 query: StoryQuery | None = None, 

67 limit: int | None = None, 

68 offset: int | None = None, 

69 ) -> AsyncIterator[StoryEntity]: 

70 """Return all stories of a given query. 

71 

72 Args: 

73 query: The query to use for selecting the rows. 

74 limit: The maximum number of entities to return. 

75 offset: Skip the offset rows before beginning to return entities. 

76 

77 Yields: 

78 A list of stories. 

79 """ 

80 raise NotImplementedError