22from unittest .mock import MagicMock , AsyncMock , patch
33from fastapi import APIRouter , Depends , HTTPException , status
44import app .schemas as schemas
5+
56# updateresult
67from pymongo .results import UpdateResult
78
89from app .routes .chat import (
9- get_new_chat ,
10- get_chats ,
11- change_chat_name ,
12- delete_chat ,
13- add_message_to_chat ,
14- get_chat_messages ,
15- rate_message
10+ get_new_chat ,
11+ get_chats ,
12+ change_chat_name ,
13+ delete_chat ,
14+ add_message_to_chat ,
15+ get_chat_messages ,
16+ rate_message ,
1617)
1718from bson import ObjectId
1819
20+
1921@pytest .fixture
2022def fake_chat_repo ():
2123 class FakeChatRepository :
@@ -26,26 +28,41 @@ async def initialize_chat(self, user_email):
2628 "user_email" : user_email ,
2729 "messages" : [],
2830 }
31+
2932 async def get_chat_by_user_email (self , user_email ):
3033 if user_email == "hi@hi.com" :
3134 return [
32- {"_id" : "chat123" , "name" : "Test Chat" , "user_email" : user_email , "messages" : []},
33- {"_id" : "chat456" , "name" : "Test Chat 456" , "user_email" : user_email , "messages" : []}
34- ]
35- async def get_chat_by_id (self , chat_id , user_email ):
36- print (chat_id , user_email )
37- if chat_id == "chat123" or chat_id == b'foo-bar-quux' :
35+ {
36+ "_id" : "chat123" ,
37+ "name" : "Test Chat" ,
38+ "user_email" : user_email ,
39+ "messages" : [],
40+ },
41+ {
42+ "_id" : "chat456" ,
43+ "name" : "Test Chat 456" ,
44+ "user_email" : user_email ,
45+ "messages" : [],
46+ },
47+ ]
48+
49+ async def get_chat_by_id (self , chat_id , user_email , limit = None ):
50+ print (chat_id , user_email , limit )
51+ if chat_id == "chat123" or chat_id == b"foo-bar-quux" :
3852 return {
39- "_id" : ' foo-bar-quux' ,
53+ "_id" : " foo-bar-quux" ,
4054 "name" : "Test Chat" ,
4155 "user_email" : user_email ,
4256 "messages" : [],
4357 }
4458 return None
59+
4560 async def update_chat (self , chat_id , chat_data ):
4661 return 1
62+
4763 async def delete_chat (self , chat_id , user_email ):
4864 return 1
65+
4966 async def add_message (self , chat_id , message : schemas .MessageCreate ):
5067 return {
5168 "_id" : "message123" ,
@@ -54,170 +71,202 @@ async def add_message(self, chat_id, message: schemas.MessageCreate):
5471 "timestamp" : "2023-10-01T00:00:00Z" ,
5572 "rating" : None ,
5673 }
57- async def update_message_rating (self , chat_id , message_id : ObjectId , rating_data ):
74+
75+ async def update_message_rating (
76+ self , chat_id , message_id : ObjectId , rating_data
77+ ):
5878
5979 MockUpdateResult = MagicMock ()
6080 if str (message_id ) == "614c1b2f8e4b0c6a1d2d5d2f" :
6181 MockUpdateResult .matched_count = 1
6282 MockUpdateResult .modified_count = 1
6383 return MockUpdateResult
64- if str (message_id ) == ' 614c1b2f8e4b0c6a1d2d5d21' :
84+ if str (message_id ) == " 614c1b2f8e4b0c6a1d2d5d21" :
6585 MockUpdateResult .matched_count = 1
6686 MockUpdateResult .modified_count = 0
6787 return MockUpdateResult
68- if str (message_id ) == ' 614c1b2f8e4b0c6a1d2d5d29' :
88+ if str (message_id ) == " 614c1b2f8e4b0c6a1d2d5d29" :
6989 MockUpdateResult .matched_count = 0
7090 MockUpdateResult .modified_count = 0
7191 return MockUpdateResult
72- if str (message_id ) == ' 614c1b2f8e4b0c6a1d2d5d25' :
92+ if str (message_id ) == " 614c1b2f8e4b0c6a1d2d5d25" :
7393 raise HTTPException (status_code = 500 , detail = "Internal Server Error" )
74- if str (message_id ) == ' 614c1b2f8e4b0c6a1d2d5d26' :
94+ if str (message_id ) == " 614c1b2f8e4b0c6a1d2d5d26" :
7595 raise Exception (status_code = 504 , detail = "Internal Server Error" )
96+
7697 return FakeChatRepository ()
77-
98+
99+
78100@pytest .mark .asyncio
79101async def test__unit_test__get_new_chat (fake_chat_repo ):
80- current_user = {"sub" :"hi@hi.com" }
102+ current_user = {"sub" : "hi@hi.com" }
81103 chat = await get_new_chat (current_user , fake_chat_repo )
82104 assert chat ["chat_id" ] == "chat123"
83105
106+
84107@pytest .mark .asyncio
85108async def test__unit_test__get_new_chat_no_email (fake_chat_repo ):
86- current_user = {"sub" :"" }
109+ current_user = {"sub" : "" }
87110 with pytest .raises (HTTPException ) as excinfo :
88111 await get_new_chat (current_user , fake_chat_repo )
89112 assert excinfo .value .status_code == 400
90113
114+
91115@pytest .mark .asyncio
92116async def test__unit_test__get_new_chat_no_chat (fake_chat_repo ):
93- current_user = {"sub" :"hi@hi.com" }
117+ current_user = {"sub" : "hi@hi.com" }
94118 chat = await get_chats (current_user , fake_chat_repo )
95119 assert chat [0 ]["id" ] == "chat123"
96120
121+
97122@pytest .mark .asyncio
98123async def test__unit_test__get_new_chat_no_chat_no_email (fake_chat_repo ):
99- current_user = {"sub" :"" }
124+ current_user = {"sub" : "" }
100125 with pytest .raises (HTTPException ) as excinfo :
101126 await get_chats (current_user , fake_chat_repo )
102127 assert excinfo .value .status_code == 400
103128
129+
104130@pytest .mark .asyncio
105131async def test__unit_test__change_chat_name (fake_chat_repo ):
106- current_user = {"sub" :"hi@hi.com" }
132+ current_user = {"sub" : "hi@hi.com" }
107133 chat_id = "chat123"
108134 await change_chat_name (chat_id , "New Chat Name" , current_user , fake_chat_repo )
109-
135+
136+
110137@pytest .mark .asyncio
111138async def test__unit_test__change_chat_name_not_found (fake_chat_repo ):
112- current_user = {"sub" :"hi@hi.com" }
139+ current_user = {"sub" : "hi@hi.com" }
113140 chat_id = "chat124"
114141 with pytest .raises (HTTPException ) as excinfo :
115142 await change_chat_name (chat_id , "New Chat Name" , current_user , fake_chat_repo )
116143 assert excinfo .value .status_code == 404
117-
144+
145+
118146@pytest .mark .asyncio
119147async def test__unit_test__delete_chat (fake_chat_repo ):
120- current_user = {"sub" :"hi@hi.com" }
148+ current_user = {"sub" : "hi@hi.com" }
121149 chat_id = "chat123"
122150 await delete_chat (chat_id , current_user , fake_chat_repo )
123151
152+
124153@pytest .mark .asyncio
125154async def test__unit_test__delete_chat_not_found (fake_chat_repo ):
126- current_user = {"sub" :"hi@hi.com" }
155+ current_user = {"sub" : "hi@hi.com" }
127156 chat_id = "chat124"
128157 with pytest .raises (HTTPException ) as excinfo :
129158 await delete_chat (chat_id , current_user , fake_chat_repo )
130159 assert excinfo .value .status_code == 404
131-
160+
161+
132162@pytest .mark .asyncio
133163async def test__unit_test__add_message_to_chat (fake_chat_repo ):
134- current_user = {"sub" :"hi@hi.com" }
164+ current_user = {"sub" : "hi@hi.com" }
135165 chat_id = "chat123"
136166 message = schemas .MessageCreate (content = "Hello!" , sender = "user" )
137- result = await add_message_to_chat (chat_id , message , current_user , fake_chat_repo )
167+ result = await add_message_to_chat (chat_id , message , current_user , fake_chat_repo )
138168 assert result ["_id" ] == "message123"
139169
170+
140171@pytest .mark .asyncio
141172async def test__unit_test__add_message_to_chat_not_found (fake_chat_repo ):
142- current_user = {"sub" :"hi@hi.com" }
173+ current_user = {"sub" : "hi@hi.com" }
143174 chat_id = "chat124"
144175 message = schemas .MessageCreate (content = "Hello!" , sender = "user" )
145176 with pytest .raises (HTTPException ) as excinfo :
146177 await add_message_to_chat (chat_id , message , current_user , fake_chat_repo )
147178 assert excinfo .value .status_code == 404
148179
180+
149181@pytest .mark .asyncio
150182async def test__unit_test__get_chat_messages (fake_chat_repo ):
151- current_user = {"sub" :"hi@hi.com" }
183+ current_user = {"sub" : "hi@hi.com" }
152184 chat_id = "chat123"
153- messages = await get_chat_messages (chat_id , current_user , fake_chat_repo )
185+ messages = await get_chat_messages (chat_id , None , current_user , fake_chat_repo )
154186 assert messages ["messages" ] == []
155187
188+
156189@pytest .mark .asyncio
157190async def test__unit_test__get_chat_messages_not_found (fake_chat_repo ):
158- current_user = {"sub" :"hi@hi.com" }
191+ current_user = {"sub" : "hi@hi.com" }
159192 chat_id = "chat125"
160193 with pytest .raises (HTTPException ) as excinfo :
161- await get_chat_messages (chat_id , current_user , fake_chat_repo )
194+ await get_chat_messages (chat_id , None , current_user , fake_chat_repo )
162195 assert excinfo .value .status_code == 404
163196
197+
164198@pytest .mark .asyncio
165199async def test__unit_test__rate_message (fake_chat_repo ):
166- current_user = {"sub" :"hi@hi.com" }
167- chat_id = b' foo-bar-quux'
200+ current_user = {"sub" : "hi@hi.com" }
201+ chat_id = b" foo-bar-quux"
168202 message_id = "614c1b2f8e4b0c6a1d2d5d2f"
169203 rating_data = schemas .MessageRatingUpdate (rating = True )
170204
171- result = await rate_message (chat_id , message_id , rating_data , current_user , fake_chat_repo )
205+ result = await rate_message (
206+ chat_id , message_id , rating_data , current_user , fake_chat_repo
207+ )
208+
172209
173210@pytest .mark .asyncio
174211async def test__unit_test__rate_message_no_chat (fake_chat_repo ):
175- current_user = {"sub" :"hi@hi.com" }
176- chat_id = b' foo-bar-quuu'
212+ current_user = {"sub" : "hi@hi.com" }
213+ chat_id = b" foo-bar-quuu"
177214 message_id = "614c1b2f8e4b0c6a1d2d5d2f"
178215 rating_data = schemas .MessageRatingUpdate (rating = True )
179216 with pytest .raises (HTTPException ) as excinfo :
180- await rate_message (chat_id , message_id , rating_data , current_user , fake_chat_repo )
217+ await rate_message (
218+ chat_id , message_id , rating_data , current_user , fake_chat_repo
219+ )
181220 assert excinfo .value .status_code == 404
182221
222+
183223@pytest .mark .asyncio
184224async def test__unit_test__rate_message_no_message (fake_chat_repo ):
185- current_user = {"sub" :"hi@hi.com" }
186- chat_id = b' foo-bar-quux'
225+ current_user = {"sub" : "hi@hi.com" }
226+ chat_id = b" foo-bar-quux"
187227 message_id = "614c1b2f8e4b0c6a1d2d5d29"
188228 rating_data = schemas .MessageRatingUpdate (rating = True )
189229 with pytest .raises (HTTPException ) as excinfo :
190- await rate_message (chat_id , message_id , rating_data , current_user , fake_chat_repo )
230+ await rate_message (
231+ chat_id , message_id , rating_data , current_user , fake_chat_repo
232+ )
191233 assert excinfo .value .status_code == 404
192234
235+
193236@pytest .mark .asyncio
194237async def test__unit_test__rate_message_no_edit (fake_chat_repo ):
195- current_user = {"sub" :"hi@hi.com" }
196- chat_id = b' foo-bar-quux'
238+ current_user = {"sub" : "hi@hi.com" }
239+ chat_id = b" foo-bar-quux"
197240 message_id = "614c1b2f8e4b0c6a1d2d5d21"
198241 rating_data = schemas .MessageRatingUpdate (rating = True )
199242 with pytest .raises (HTTPException ) as excinfo :
200- await rate_message (chat_id , message_id , rating_data , current_user , fake_chat_repo )
243+ await rate_message (
244+ chat_id , message_id , rating_data , current_user , fake_chat_repo
245+ )
201246 assert excinfo .value .status_code == 304
202247
248+
203249@pytest .mark .asyncio
204250async def test__unit_test__rate_message_internal_error (fake_chat_repo ):
205- current_user = {"sub" :"hi@hi.com" }
206- chat_id = b' foo-bar-quux'
251+ current_user = {"sub" : "hi@hi.com" }
252+ chat_id = b" foo-bar-quux"
207253 message_id = "614c1b2f8e4b0c6a1d2d5d25"
208254 rating_data = schemas .MessageRatingUpdate (rating = True )
209255 with pytest .raises (HTTPException ) as excinfo :
210- await rate_message (chat_id , message_id , rating_data , current_user , fake_chat_repo )
256+ await rate_message (
257+ chat_id , message_id , rating_data , current_user , fake_chat_repo
258+ )
211259 assert excinfo .value .status_code == 500
212260
261+
213262@pytest .mark .asyncio
214263async def test__unit_test__rate_message_internal_error_2 (fake_chat_repo ):
215- current_user = {"sub" :"hi@hi.com" }
216- chat_id = b' foo-bar-quux'
264+ current_user = {"sub" : "hi@hi.com" }
265+ chat_id = b" foo-bar-quux"
217266 message_id = "614c1b2f8e4b0c6a1d2d5d26"
218267 rating_data = schemas .MessageRatingUpdate (rating = True )
219268 with pytest .raises (HTTPException ) as excinfo :
220- await rate_message (chat_id , message_id , rating_data , current_user , fake_chat_repo )
269+ await rate_message (
270+ chat_id , message_id , rating_data , current_user , fake_chat_repo
271+ )
221272 assert excinfo .value .status_code == 500
222-
223-
0 commit comments