|
5 | 5 | import pytest |
6 | 6 | from starlette import status |
7 | 7 |
|
8 | | -from rating_api.models import Comment, LecturerUserComment, ReviewStatus |
| 8 | +from rating_api.models import Comment, CommentReaction, LecturerUserComment, Reaction, ReviewStatus |
9 | 9 | from rating_api.settings import get_settings |
10 | 10 |
|
11 | 11 |
|
@@ -205,6 +205,116 @@ def test_get_comment(client, comment): |
205 | 205 | assert response.status_code == status.HTTP_404_NOT_FOUND |
206 | 206 |
|
207 | 207 |
|
| 208 | +@pytest.fixture |
| 209 | +def comments_with_likes(client, dbsession, lecturers): |
| 210 | + """ |
| 211 | + Создает несколько комментариев с разным количеством лайков/дизлайков |
| 212 | + """ |
| 213 | + comments = [] |
| 214 | + |
| 215 | + user_id = 9999 |
| 216 | + |
| 217 | + comment_data = [ |
| 218 | + { |
| 219 | + "user_id": user_id, |
| 220 | + "lecturer_id": lecturers[0].id, |
| 221 | + "subject": "test_subject", |
| 222 | + "text": "Comment with many likes", |
| 223 | + "mark_kindness": 1, |
| 224 | + "mark_freebie": 0, |
| 225 | + "mark_clarity": 0, |
| 226 | + "review_status": ReviewStatus.APPROVED, |
| 227 | + }, |
| 228 | + { |
| 229 | + "user_id": user_id, |
| 230 | + "lecturer_id": lecturers[0].id, |
| 231 | + "subject": "test_subject", |
| 232 | + "text": "Comment with many dislikes", |
| 233 | + "mark_kindness": 1, |
| 234 | + "mark_freebie": 0, |
| 235 | + "mark_clarity": 0, |
| 236 | + "review_status": ReviewStatus.APPROVED, |
| 237 | + }, |
| 238 | + { |
| 239 | + "user_id": user_id, |
| 240 | + "lecturer_id": lecturers[0].id, |
| 241 | + "subject": "test_subject", |
| 242 | + "text": "Comment with balanced reactions", |
| 243 | + "mark_kindness": 1, |
| 244 | + "mark_freebie": 0, |
| 245 | + "mark_clarity": 0, |
| 246 | + "review_status": ReviewStatus.APPROVED, |
| 247 | + }, |
| 248 | + ] |
| 249 | + |
| 250 | + for data in comment_data: |
| 251 | + comment = Comment(**data) |
| 252 | + dbsession.add(comment) |
| 253 | + comments.append(comment) |
| 254 | + |
| 255 | + dbsession.commit() |
| 256 | + |
| 257 | + for _ in range(10): |
| 258 | + reaction = CommentReaction(comment_uuid=comments[0].uuid, user_id=user_id, reaction=Reaction.LIKE) |
| 259 | + dbsession.add(reaction) |
| 260 | + for _ in range(2): |
| 261 | + reaction = CommentReaction(comment_uuid=comments[0].uuid, user_id=user_id, reaction=Reaction.DISLIKE) |
| 262 | + dbsession.add(reaction) |
| 263 | + |
| 264 | + for _ in range(3): |
| 265 | + reaction = CommentReaction(comment_uuid=comments[1].uuid, user_id=user_id, reaction=Reaction.LIKE) |
| 266 | + dbsession.add(reaction) |
| 267 | + for _ in range(8): |
| 268 | + reaction = CommentReaction(comment_uuid=comments[1].uuid, user_id=user_id, reaction=Reaction.DISLIKE) |
| 269 | + dbsession.add(reaction) |
| 270 | + |
| 271 | + for _ in range(5): |
| 272 | + reaction = CommentReaction(comment_uuid=comments[2].uuid, user_id=user_id, reaction=Reaction.LIKE) |
| 273 | + dbsession.add(reaction) |
| 274 | + for _ in range(5): |
| 275 | + reaction = CommentReaction(comment_uuid=comments[2].uuid, user_id=user_id, reaction=Reaction.DISLIKE) |
| 276 | + dbsession.add(reaction) |
| 277 | + |
| 278 | + dbsession.commit() |
| 279 | + |
| 280 | + for comment in comments: |
| 281 | + dbsession.refresh(comment) |
| 282 | + |
| 283 | + return comments |
| 284 | + |
| 285 | + |
| 286 | +@pytest.mark.parametrize( |
| 287 | + 'order_by, asc_order', |
| 288 | + [ |
| 289 | + ('like_diff', False), |
| 290 | + ('like_diff', True), |
| 291 | + ], |
| 292 | +) |
| 293 | +def test_comments_sort_by_like_diff(client, comments_with_likes, order_by, asc_order): |
| 294 | + """ |
| 295 | + Тестирует сортировку комментариев по разнице лайков (like_diff) |
| 296 | + """ |
| 297 | + params = {"order_by": order_by, "asc_order": asc_order, "limit": 10} |
| 298 | + |
| 299 | + response = client.get('/comment', params=params) |
| 300 | + assert response.status_code == status.HTTP_200_OK |
| 301 | + |
| 302 | + json_response = response.json() |
| 303 | + returned_comments = json_response["comments"] |
| 304 | + |
| 305 | + if order_by == 'like_diff': |
| 306 | + if asc_order: |
| 307 | + for i in range(len(returned_comments) - 1): |
| 308 | + current_like_diff = returned_comments[i]["like_count"] - returned_comments[i]["dislike_count"] |
| 309 | + next_like_diff = returned_comments[i + 1]["like_count"] - returned_comments[i + 1]["dislike_count"] |
| 310 | + assert current_like_diff <= next_like_diff |
| 311 | + else: |
| 312 | + for i in range(len(returned_comments) - 1): |
| 313 | + current_like_diff = returned_comments[i]["like_count"] - returned_comments[i]["dislike_count"] |
| 314 | + next_like_diff = returned_comments[i + 1]["like_count"] - returned_comments[i + 1]["dislike_count"] |
| 315 | + assert current_like_diff >= next_like_diff |
| 316 | + |
| 317 | + |
208 | 318 | @pytest.mark.parametrize( |
209 | 319 | 'lecturer_n,response_status', |
210 | 320 | [(0, status.HTTP_200_OK), (1, status.HTTP_200_OK), (2, status.HTTP_200_OK), (3, status.HTTP_404_NOT_FOUND)], |
|
0 commit comments