fix: make messages appear in the correct order again (#5309)

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-12-17 22:45:22 -03:00 • committed by GitHub
commit 5a8d73c5b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 189 additions and 26 deletions

View file

@ -1,4 +1,5 @@
import shutil
from datetime import datetime, timezone
from pathlib import Path
import pytest
@ -154,11 +155,16 @@ def test_message_without_sender():
def test_message_serialization():
"""Test message serialization to dict."""
# Create a timestamp with timezone
message = Message(text="Test message", sender=MESSAGE_SENDER_USER)
timestamp_str = message.timestamp
timestamp = datetime.strptime(timestamp_str, "%Y-%m-%d %H:%M:%S %Z").replace(tzinfo=timezone.utc)
serialized = message.model_dump()
assert serialized["text"] == "Test message"
assert serialized["sender"] == MESSAGE_SENDER_USER
assert serialized["timestamp"] == timestamp
assert serialized["timestamp"].tzinfo == timezone.utc
def test_message_to_lc_without_sender():
@ -170,6 +176,24 @@ def test_message_to_lc_without_sender():
assert lc_message.content == "Test message"
def test_timestamp_serialization():
"""Test timestamp serialization with different formats."""
# Test with timezone
msg1 = Message(text="Test message", sender=MESSAGE_SENDER_USER, timestamp="2023-12-25 15:30:45 UTC")
serialized1 = msg1.model_dump()
assert serialized1["timestamp"].tzinfo == timezone.utc
# Test without timezone
msg2 = Message(text="Test message", sender=MESSAGE_SENDER_USER, timestamp="2023-12-25 15:30:45")
serialized2 = msg2.model_dump()
assert serialized2["timestamp"].tzinfo == timezone.utc
# Test that both formats result in equivalent UTC times when appropriate
msg_with_tz = Message(text="Test message", sender=MESSAGE_SENDER_USER, timestamp="2023-12-25 15:30:45 UTC")
msg_without_tz = Message(text="Test message", sender=MESSAGE_SENDER_USER, timestamp="2023-12-25 15:30:45")
assert msg_with_tz.model_dump()["timestamp"] == msg_without_tz.model_dump()["timestamp"]
# Clean up the cache directory after all tests
@pytest.fixture(autouse=True)
def cleanup():

View file

@ -1,3 +1,4 @@
from datetime import datetime, timezone
from uuid import UUID
import pytest
@ -25,7 +26,7 @@ async def created_messages(session): # noqa: ARG001
messages = [
MessageCreate(text="Test message 1", sender="User", sender_name="User", session_id="session_id2"),
MessageCreate(text="Test message 2", sender="User", sender_name="User", session_id="session_id2"),
MessageCreate(text="Test message 3", sender="User", sender_name="User", session_id="session_id2"),
MessageCreate(text="Test message 3", sender="AI", sender_name="AI", session_id="session_id2"),
]
messagetables = [MessageTable.model_validate(message, from_attributes=True) for message in messages]
return await aadd_messagetables(messagetables, _session)
@ -99,8 +100,19 @@ async def test_successfully_update_session_id(client, logged_in_headers, created
)
assert response.status_code == 200
assert len(response.json()) == len(created_messages)
for message in response.json():
messages = response.json()
for message in messages:
assert message["session_id"] == new_session_id
response_timestamp = message["timestamp"]
timestamp = datetime.strptime(response_timestamp, "%Y-%m-%d %H:%M:%S %Z").replace(tzinfo=timezone.utc)
timestamp_str = timestamp.strftime("%Y-%m-%d %H:%M:%S %Z")
assert timestamp_str == response_timestamp
# Check if the messages ordered by timestamp are in the correct order
# User, User, AI
assert messages[0]["sender"] == "User"
assert messages[1]["sender"] == "User"
assert messages[2]["sender"] == "AI"
# No messages found with the given session ID