Coverage for functions \ flipdare \ search \ doc \ friend_document.py: 86%

57 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-05-08 12:22 +1000

1#!/usr/bin/env python 

2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved. 

3# 

4# This file is part of Flipdare's proprietary software and contains 

5# confidential and copyrighted material. Unauthorised copying, 

6# modification, distribution, or use of this file is strictly 

7# prohibited without prior written permission from Flipdare Pty Ltd. 

8# 

9# This software includes third-party components licensed under MIT, 

10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details. 

11# 

12 

13from __future__ import annotations 

14 

15from typing import Any, override 

16 

17from flipdare.app_types import SearchDict 

18from flipdare.generated.schema.search.friend_document_schema import FriendDocumentSchema 

19from flipdare.generated.shared.search.friend_document_type import FriendDocumentType 

20from flipdare.generated.shared.search.search_obj_type import SearchObjType 

21from flipdare.search.doc._search_document import TS_C, SearchDocument 

22from flipdare.util.time_util import TypesenseTime 

23 

24__all__ = [ 

25 "FriendDocument", 

26] 

27 

28 

29class FriendDocument(SearchDocument[FriendDocumentSchema]): 

30 

31 SCHEMA_CLASS = FriendDocumentSchema 

32 

33 def __init__(self, data: FriendDocumentSchema, doc_id: str | None = None) -> None: 

34 super().__init__(data=data, doc_id=doc_id) 

35 

36 @classmethod 

37 def create( 

38 cls, 

39 uid: str, 

40 friend_type: FriendDocumentType, 

41 keywords: list[str], 

42 friend_uid: str, 

43 friend_keywords: list[str], 

44 updated_at: TS_C | None = None, 

45 created_at: TS_C | None = None, 

46 ) -> FriendDocument: 

47 

48 if updated_at is None: 

49 updated_at = TypesenseTime.server_timestamp() 

50 if created_at is None: 

51 created_at = TypesenseTime.server_timestamp() 

52 

53 data: FriendDocumentSchema = { 

54 "uid": uid, 

55 "friend_type": friend_type, 

56 "keywords": keywords, 

57 "friend_uid": friend_uid, 

58 "friend_keywords": friend_keywords, 

59 "created_at": TypesenseTime.from_any(created_at), 

60 "updated_at": TypesenseTime.from_any(updated_at), 

61 } 

62 

63 return cls(data=data) 

64 

65 @classmethod 

66 @override 

67 def from_payload( 

68 cls, 

69 doc_id: str, 

70 payload: SearchDict, 

71 original: SearchDocument[Any] | None = None, 

72 ) -> FriendDocument: 

73 schema_data = cls.parse(cls.SCHEMA_CLASS, payload, original) 

74 return cls(data=schema_data, doc_id=doc_id) 

75 

76 @property 

77 @override 

78 def created_at(self) -> int: 

79 return self._data["created_at"] 

80 

81 @property 

82 @override 

83 def updated_at(self) -> int: 

84 return self._data["updated_at"] 

85 

86 @property 

87 @override 

88 def obj_type(self) -> SearchObjType: 

89 return SearchObjType.FRIEND 

90 

91 @property 

92 @override 

93 def uid(self) -> str: 

94 return self._data["uid"] 

95 

96 @property 

97 def keywords(self) -> list[str]: 

98 return self._data["keywords"] 

99 

100 @property 

101 def friend_uid(self) -> str: 

102 return self._data["friend_uid"] 

103 

104 @property 

105 def friend_keywords(self) -> list[str]: 

106 return self._data["friend_keywords"] 

107 

108 @override 

109 def __str__(self) -> str: 

110 return ( 

111 f"FriendDocument(doc_id={self.doc_id}, uid={self.uid}, friend_uid={self.friend_uid})" 

112 ) 

113 

114 @override 

115 def __repr__(self) -> str: 

116 return self.__str__()