fix: Improve error handling in aupdate_messages function to prevent empty message error (#5655)
* refactor: Improve error handling in aupdate_messages function * Update src/backend/base/langflow/memory.py Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org> * fix(tests): updating a non-existing message adds a new message now - Updated the test name from `test_aupdate_nonexistent_message` to `test_aupdate_nonexistent_message_generates_a_new_message` for better clarity. - Modified the assertion to expect that a new message is generated when attempting to update a nonexistent message, changing the expected length of the updated messages from 0 to 1. * [autofix.ci] apply automated fixes * fix(tests): update message handling in tests for non-existent messages - Refactored tests to ensure that attempting to update a non-existent message raises a ValueError instead of creating a new message. - Updated test names and assertions for clarity and accuracy, ensuring they reflect the expected behavior when handling messages that do not exist in the database. --------- Co-authored-by: Gabriel Luiz Freitas Almeida <gabriel@langflow.org> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
00deb49c67
commit
0cf4f03400
2 changed files with 21 additions and 9 deletions
|
|
@ -144,7 +144,9 @@ async def aupdate_messages(messages: Message | list[Message]) -> list[Message]:
|
|||
await session.refresh(msg)
|
||||
updated_messages.append(msg)
|
||||
else:
|
||||
logger.warning(f"Message with id {message.id} not found")
|
||||
error_message = f"Message with id {message.id} not found"
|
||||
logger.warning(error_message)
|
||||
raise ValueError(error_message)
|
||||
return [MessageRead.model_validate(message, from_attributes=True) for message in updated_messages]
|
||||
|
||||
|
||||
|
|
@ -255,7 +257,12 @@ async def astore_message(
|
|||
msg = "All of session_id, sender, and sender_name must be provided."
|
||||
raise ValueError(msg)
|
||||
if hasattr(message, "id") and message.id:
|
||||
return await aupdate_messages([message])
|
||||
# if message has an id and exist in the database, update it
|
||||
# if not raise an error and add the message to the database
|
||||
try:
|
||||
return await aupdate_messages([message])
|
||||
except ValueError as e:
|
||||
logger.error(e)
|
||||
if flow_id and not isinstance(flow_id, UUID):
|
||||
flow_id = UUID(flow_id)
|
||||
return await aadd_messages([message], flow_id=flow_id)
|
||||
|
|
|
|||
|
|
@ -195,26 +195,27 @@ async def test_aupdate_multiple_messages(created_messages):
|
|||
|
||||
|
||||
@pytest.mark.usefixtures("client")
|
||||
async def test_aupdate_nonexistent_message():
|
||||
async def test_aupdate_nonexistent_message_generates_a_new_message():
|
||||
# Create a message with a non-existent UUID
|
||||
nonexistent_uuid = uuid4()
|
||||
message = MessageRead(
|
||||
id=uuid4(), # Generate a random UUID that won't exist in the database
|
||||
id=nonexistent_uuid, # Generate a random UUID that won't exist in the database
|
||||
text="Test message",
|
||||
sender="User",
|
||||
sender_name="User",
|
||||
session_id="session_id",
|
||||
flow_id=uuid4(),
|
||||
)
|
||||
|
||||
updated = await aupdate_messages(message)
|
||||
assert len(updated) == 0
|
||||
with pytest.raises(ValueError, match=f"Message with id {nonexistent_uuid} not found"):
|
||||
await aupdate_messages(message)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("client")
|
||||
async def test_aupdate_mixed_messages(created_messages):
|
||||
# Create a mix of existing and non-existing messages
|
||||
nonexistent_uuid = uuid4()
|
||||
nonexistent_message = MessageRead(
|
||||
id=uuid4(), # Generate a random UUID that won't exist in the database
|
||||
id=nonexistent_uuid, # Generate a random UUID that won't exist in the database
|
||||
text="Test message",
|
||||
sender="User",
|
||||
sender_name="User",
|
||||
|
|
@ -225,7 +226,11 @@ async def test_aupdate_mixed_messages(created_messages):
|
|||
messages_to_update = created_messages[:1] + [nonexistent_message]
|
||||
created_messages[0].text = "Updated existing message"
|
||||
|
||||
updated = await aupdate_messages(messages_to_update)
|
||||
with pytest.raises(ValueError, match=f"Message with id {nonexistent_uuid} not found"):
|
||||
await aupdate_messages(messages_to_update)
|
||||
|
||||
# Update just the existing message
|
||||
updated = await aupdate_messages(created_messages[:1])
|
||||
|
||||
assert len(updated) == 1
|
||||
assert updated[0].text == "Updated existing message"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue