Coverage for app/backend/src/tests/fixtures/api/threads.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.17.5, created at 2026-11-24 16:36 +0000

1from contextlib import contextmanager 

2from typing import cast 

3 

4import grpc 

5 

6from couchers.proto import threads_pb2, threads_pb2_grpc 

7from couchers.servicers.threads import Threads 

8from tests.fixtures.misc import Moderator 

9from tests.fixtures.sessions import FakeChannel 

10 

11 

12@contextmanager 

13def _threads_session(token: str): 

14 channel = FakeChannel(token) 

15 threads_pb2_grpc.add_ThreadsServicer_to_server(Threads(), cast(grpc.Server, channel)) 

16 yield threads_pb2_grpc.ThreadsStub(cast(grpc.Channel, channel)) 

17 

18 

19def get_thread( 

20 *, 

21 token: str, 

22 thread_id: int, 

23 page_size: int | None = None, 

24 page_token: str | None = None, 

25 sort_order: threads_pb2.ThreadSortOrder.ValueType | None = None, 

26) -> threads_pb2.GetThreadRes: 

27 with _threads_session(token) as session: 

28 res: threads_pb2.GetThreadRes = session.GetThread( 

29 threads_pb2.GetThreadReq( 

30 thread_id=thread_id, page_size=page_size, page_token=page_token, sort_order=sort_order 

31 ) 

32 ) 

33 return res 

34 

35 

36def post_reply( 

37 *, 

38 token: str, 

39 thread_id: int, 

40 content: str = "Test reply", 

41 approve_by: Moderator | None = None, 

42) -> int: 

43 """Returns the thread id of the newly posted reply.""" 

44 with _threads_session(token) as session: 

45 res: threads_pb2.PostReplyRes = session.PostReply( 

46 threads_pb2.PostReplyReq(thread_id=thread_id, content=content) 

47 ) 

48 

49 if approve_by is not None: 

50 approve_by.approve_thread_post(res.thread_id) 

51 

52 return res.thread_id 

53 

54 

55def update_reply(*, token: str, thread_id: int, content: str) -> threads_pb2.Reply: 

56 with _threads_session(token) as session: 

57 reply: threads_pb2.Reply = session.UpdateReply(threads_pb2.UpdateReplyReq(thread_id=thread_id, content=content)) 

58 return reply 

59 

60 

61def delete_reply(*, token: str, thread_id: int) -> None: 

62 with _threads_session(token) as session: 

63 session.DeleteReply(threads_pb2.DeleteReplyReq(thread_id=thread_id))