Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: delete thread throwing error #159

Merged
merged 1 commit into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion forum/backends/mongodb/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ def delete_read_state_by_thread_id(self, thread_id: str) -> None:
for user in list(users):
updated_read_states = []
for read_state in user.get("read_states", []):
del read_state["last_read_times"][thread_id]
if read_state["last_read_times"].get(thread_id):
del read_state["last_read_times"][thread_id]
updated_read_states.append(read_state)
self._collection.update_one(
{"_id": user["_id"]}, {"$set": {"read_states": updated_read_states}}
Expand Down
24 changes: 24 additions & 0 deletions tests/test_views/test_threads.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Test threads api endpoints."""

import time
from datetime import datetime
from typing import Any, Optional
import pytest

Expand Down Expand Up @@ -1114,3 +1115,26 @@ def test_read_states_deletion_on_thread_deletion_with_multiple_read_states(
assert patched_mongo_backend.get_thread(thread_id_1) is None
assert is_thread_id_exists_in_user_read_state(user_id_1, thread_id_1) is False
assert is_thread_id_exists_in_user_read_state(user_id_2, thread_id_2) is True


def test_read_states_deletion_checks_thread_id_existence(
api_client: APIClient, patched_mongo_backend: MongoBackend
) -> None:
"""Test that read state deletion only occurs when thread_id exists in last_read_times."""
user_id, thread_id = setup_models(backend=patched_mongo_backend)

other_thread_id = "other_thread_id"
read_states = [
{
"course_id": "course1",
"last_read_times": {other_thread_id: datetime.now()},
}
]
patched_mongo_backend.update_user(user_id, {"read_states": read_states})

assert is_thread_id_exists_in_user_read_state(user_id, other_thread_id) is True
assert is_thread_id_exists_in_user_read_state(user_id, thread_id) is False

response = api_client.delete_json(f"/api/v2/threads/{thread_id}")
assert response.status_code == 200
assert is_thread_id_exists_in_user_read_state(user_id, other_thread_id) is True