feat: add endpoint and tests for updating session ID in messages (#3946)

* Add endpoint to update session ID for messages in monitor API

* Add tests for updating session ID in messages endpoint

- Added test to successfully update session ID for all messages with the old session ID.
- Added test to handle case where no messages are found with the given session ID.

---------

Co-authored-by: anovazzi1 <otavio2204@gmail.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-10-01 16:35:46 -03:00 committed by GitHub
commit df792cb19d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 70 additions and 0 deletions

View file

@ -107,6 +107,39 @@ async def update_message(
raise HTTPException(status_code=500, detail=str(e))
@router.patch("/messages/session/{old_session_id}", response_model=list[MessageResponse])
async def update_session_id(
old_session_id: str,
new_session_id: str = Query(..., description="The new session ID to update to"),
session: Session = Depends(get_session),
current_user: User = Depends(get_current_active_user),
):
try:
# Get all messages with the old session ID
stmt = select(MessageTable).where(MessageTable.session_id == old_session_id)
messages = session.exec(stmt).all()
if not messages:
raise HTTPException(status_code=404, detail="No messages found with the given session ID")
# Update all messages with the new session ID
for message in messages:
message.session_id = new_session_id
session.add_all(messages)
session.commit()
message_responses = []
for message in messages:
session.refresh(message)
message_responses.append(MessageResponse.model_validate(message, from_attributes=True))
return message_responses
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.delete("/messages/session/{session_id}", status_code=204)
async def delete_messages_session(
session_id: str,

View file

@ -74,3 +74,40 @@ def test_delete_messages_session(client: TestClient, created_messages, logged_in
response = client.get("api/v1/monitor/messages", headers=logged_in_headers)
assert response.status_code == 200
assert len(response.json()) == 0
# Successfully update session ID for all messages with the old session ID
def test_successfully_update_session_id(client, session, logged_in_headers, created_messages):
old_session_id = "session_id2"
new_session_id = "new_session_id"
response = client.patch(
f"api/v1/monitor/messages/session/{old_session_id}",
params={"new_session_id": new_session_id},
headers=logged_in_headers,
)
assert response.status_code == 200, response.text
updated_messages = response.json()
assert len(updated_messages) == len(created_messages)
for message in updated_messages:
assert message["session_id"] == new_session_id
response = client.get("api/v1/monitor/messages", headers=logged_in_headers, params={"session_id": new_session_id})
assert response.status_code == 200
assert len(response.json()) == len(created_messages)
for message in response.json():
assert message["session_id"] == new_session_id
# No messages found with the given session ID
def test_no_messages_found_with_given_session_id(client, session, logged_in_headers):
old_session_id = "non_existent_session_id"
new_session_id = "new_session_id"
response = client.patch(
f"/messages/session/{old_session_id}", params={"new_session_id": new_session_id}, headers=logged_in_headers
)
assert response.status_code == 404, response.text
assert response.json()["detail"] == "Not Found"