Skip to content

Commit 6f44efb

Browse files
committed
fix tests
1 parent 6b5d91a commit 6f44efb

3 files changed

Lines changed: 19 additions & 13 deletions

File tree

rating_api/routes/comment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,12 @@ async def get_comments(
214214
)
215215

216216
comments = comments_query.limit(limit).offset(offset).all()
217-
218217
if not comments:
219218
raise ObjectNotFound(Comment, 'all')
220-
if "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
219+
if user and "rating.comment.review" in [scope['name'] for scope in user.get('session_scopes')]:
221220
result = CommentGetAllWithAllInfo(limit=limit, offset=offset, total=len(comments))
222221
comment_validator = CommentGetWithAllInfo
223-
elif user.get('id') == user_id:
222+
elif user and user.get('id') == user_id:
224223
result = CommentGetAllWithStatus(limit=limit, offset=offset, total=len(comments))
225224
comment_validator = CommentGetWithStatus
226225
else:

tests/conftest.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ def comment(dbsession, lecturer):
5353
mark_kindness=1,
5454
mark_clarity=1,
5555
mark_freebie=1,
56-
mark_general=1,
5756
lecturer_id=lecturer.id,
5857
review_status=ReviewStatus.APPROVED,
5958
)
@@ -169,8 +168,9 @@ def lecturers_with_comments(dbsession, lecturers):
169168
(lecturers[2].id, 9992, 'test_subject12', ReviewStatus.APPROVED, 0, 0, 0),
170169
]
171170

172-
comments = [
173-
Comment(
171+
comments = []
172+
for lecturer_id, user_id, subject, review_status, mark_kindness, mark_clarity, mark_freebie in comments_data:
173+
comment = Comment(
174174
subject=subject,
175175
text="test_comment",
176176
mark_kindness=mark_kindness,
@@ -180,8 +180,12 @@ def lecturers_with_comments(dbsession, lecturers):
180180
user_id=user_id,
181181
review_status=review_status,
182182
)
183-
for lecturer_id, user_id, subject, review_status, mark_kindness, mark_clarity, mark_freebie in comments_data
184-
]
183+
184+
# Set approved_by to -1 for approved or dismissed comments
185+
if review_status in [ReviewStatus.APPROVED, ReviewStatus.DISMISSED]:
186+
comment.approved_by = -1
187+
188+
comments.append(comment)
185189

186190
dbsession.add_all(comments)
187191
dbsession.commit()

tests/test_routes/test_comment.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def test_get_comment(client, comment):
206206

207207

208208
@pytest.mark.parametrize(
209-
'lecturer_n,response_status', [(0, status.HTTP_200_OK), (1, status.HTTP_200_OK), (3, status.HTTP_200_OK)]
209+
'lecturer_n,response_status',
210+
[(0, status.HTTP_200_OK), (1, status.HTTP_200_OK), (2, status.HTTP_200_OK), (3, status.HTTP_404_NOT_FOUND)],
210211
)
211212
def test_comments_by_lecturer_id(client, lecturers_with_comments, lecturer_n, response_status):
212213
lecturers, comments = lecturers_with_comments
@@ -217,8 +218,10 @@ def test_comments_by_lecturer_id(client, lecturers_with_comments, lecturer_n, re
217218
assert len(json_response["comments"]) == len(
218219
[
219220
comment
220-
for comment in lecturers[lecturer_n].comments
221-
if comment.review_status == ReviewStatus.APPROVED and not comment.is_deleted
221+
for comment in comments
222+
if comment.lecturer_id == lecturers[lecturer_n].id
223+
and comment.review_status == ReviewStatus.APPROVED
224+
and not comment.is_deleted
222225
]
223226
)
224227

@@ -228,15 +231,15 @@ def test_comments_by_lecturer_id(client, lecturers_with_comments, lecturer_n, re
228231
)
229232
def test_comments_by_user_id(client, lecturers_with_comments, user_id, response_status):
230233
_, comments = lecturers_with_comments
231-
response = response = client.get(f'{url}', params={"user_id": user_id})
234+
response = client.get(f'{url}', params={"user_id": 9990 + user_id})
232235
assert response.status_code == response_status
233236
if response.status_code == status.HTTP_200_OK:
234237
json_response = response.json()
235238
assert len(json_response["comments"]) == len(
236239
[
237240
comment
238241
for comment in comments
239-
if comment.user_id == user_id
242+
if comment.user_id == 9990 + user_id
240243
and comment.review_status == ReviewStatus.APPROVED
241244
and not comment.is_deleted
242245
]

0 commit comments

Comments
 (0)