replace hardcoded US/Pacific timezone references with timezone.utc (#4404)
Co-authored-by: kartik-mem0 <kartik.labhshetwar@mem0.ai>
This commit is contained in:
co-authored by
kartik-mem0
parent
46b4b2e9c8
commit
08de18f860
@@ -1,9 +1,10 @@
|
||||
import logging
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from mem0.memory.main import AsyncMemory, Memory
|
||||
from mem0.memory.main import AsyncMemory, Memory, _normalize_iso_timestamp_to_utc
|
||||
|
||||
|
||||
def _setup_mocks(mocker):
|
||||
@@ -127,3 +128,78 @@ class TestAsyncAddToVectorStoreErrors:
|
||||
assert result == []
|
||||
assert "Empty response from LLM, no memories to extract" in caplog.text
|
||||
assert mock_capture_event.call_count == 1
|
||||
|
||||
|
||||
def _build_memory_instance(mocker, memory_cls):
|
||||
_setup_mocks(mocker)
|
||||
mocker.patch("mem0.memory.main.SQLiteManager", mocker.MagicMock())
|
||||
mocker.patch("mem0.memory.main.MEM0_TELEMETRY", False)
|
||||
memory = memory_cls()
|
||||
memory.config = mocker.MagicMock()
|
||||
memory.config.custom_fact_extraction_prompt = None
|
||||
memory.config.custom_update_memory_prompt = None
|
||||
memory.api_version = "v1.1"
|
||||
memory.vector_store = mocker.MagicMock()
|
||||
memory.db = mocker.MagicMock()
|
||||
return memory
|
||||
|
||||
|
||||
def _assert_utc_timestamp(timestamp: str):
|
||||
parsed = datetime.fromisoformat(timestamp)
|
||||
assert parsed.tzinfo == timezone.utc
|
||||
assert parsed.utcoffset().total_seconds() == 0
|
||||
|
||||
|
||||
def test_create_memory_uses_utc_timestamps(mocker):
|
||||
memory = _build_memory_instance(mocker, Memory)
|
||||
memory._create_memory("new memory", {"new memory": [0.1, 0.2, 0.3]}, metadata={})
|
||||
payload = memory.vector_store.insert.call_args.kwargs["payloads"][0]
|
||||
_assert_utc_timestamp(payload["created_at"])
|
||||
|
||||
|
||||
def test_update_memory_uses_utc_timestamps(mocker):
|
||||
memory = _build_memory_instance(mocker, Memory)
|
||||
memory.vector_store.get.return_value = MagicMock(
|
||||
payload={"data": "old memory", "created_at": "2026-03-17T17:00:00-07:00"}
|
||||
)
|
||||
memory._update_memory("memory-id", "new memory", {"new memory": [0.1, 0.2, 0.3]}, metadata={})
|
||||
payload = memory.vector_store.update.call_args.kwargs["payload"]
|
||||
assert payload["created_at"] == "2026-03-18T00:00:00+00:00"
|
||||
_assert_utc_timestamp(payload["updated_at"])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_create_memory_uses_utc_timestamps(mocker):
|
||||
memory = _build_memory_instance(mocker, AsyncMemory)
|
||||
await memory._create_memory("new memory", {"new memory": [0.1, 0.2, 0.3]}, metadata={})
|
||||
payload = memory.vector_store.insert.call_args.kwargs["payloads"][0]
|
||||
_assert_utc_timestamp(payload["created_at"])
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_async_update_memory_uses_utc_timestamps(mocker):
|
||||
memory = _build_memory_instance(mocker, AsyncMemory)
|
||||
memory.vector_store.get.return_value = MagicMock(
|
||||
payload={"data": "old memory", "created_at": "2026-03-17T17:00:00-07:00"}
|
||||
)
|
||||
await memory._update_memory("memory-id", "new memory", {"new memory": [0.1, 0.2, 0.3]}, metadata={})
|
||||
payload = memory.vector_store.update.call_args.kwargs["payload"]
|
||||
assert payload["created_at"] == "2026-03-18T00:00:00+00:00"
|
||||
_assert_utc_timestamp(payload["updated_at"])
|
||||
|
||||
|
||||
def test_normalize_iso_timestamp_to_utc_preserves_naive_values():
|
||||
assert _normalize_iso_timestamp_to_utc("2026-03-18T00:00:00") == "2026-03-18T00:00:00"
|
||||
|
||||
|
||||
def test_normalize_iso_timestamp_to_utc_converts_pacific():
|
||||
result = _normalize_iso_timestamp_to_utc("2026-03-17T17:00:00-07:00")
|
||||
assert result == "2026-03-18T00:00:00+00:00"
|
||||
|
||||
|
||||
def test_normalize_iso_timestamp_to_utc_handles_none():
|
||||
assert _normalize_iso_timestamp_to_utc(None) is None
|
||||
|
||||
|
||||
def test_normalize_iso_timestamp_to_utc_handles_empty():
|
||||
assert _normalize_iso_timestamp_to_utc("") == ""
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import unittest
|
||||
from datetime import datetime, timezone
|
||||
from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from mem0.graphs.neptune.neptunedb import MemoryGraph
|
||||
|
||||
from mem0.graphs.neptune.base import NeptuneBase
|
||||
from mem0.graphs.neptune.neptunedb import MemoryGraph
|
||||
|
||||
|
||||
class TestNeptuneMemory(unittest.TestCase):
|
||||
@@ -289,6 +292,25 @@ class TestNeptuneMemory(unittest.TestCase):
|
||||
# Check the result
|
||||
self.assertEqual(result, mock_query_result)
|
||||
|
||||
def test_add_new_entities_payloads_use_utc_timestamps(self):
|
||||
"""Test that Neptune vector-store payloads use UTC timestamps."""
|
||||
self.memory_graph._add_new_entities_cypher(
|
||||
source="alice",
|
||||
source_embedding=[0.1, 0.2],
|
||||
source_type="person",
|
||||
destination="bob",
|
||||
dest_embedding=[0.3, 0.4],
|
||||
destination_type="person",
|
||||
relationship="KNOWS",
|
||||
user_id=self.user_id,
|
||||
)
|
||||
|
||||
_, kwargs = self.mock_vector_store.insert.call_args
|
||||
for payload in kwargs["payloads"]:
|
||||
parsed = datetime.fromisoformat(payload["created_at"])
|
||||
self.assertEqual(parsed.tzinfo, timezone.utc)
|
||||
self.assertEqual(parsed.utcoffset().total_seconds(), 0)
|
||||
|
||||
def test_search_graph_db(self):
|
||||
"""Test the _search_graph_db method."""
|
||||
# Mock node list
|
||||
|
||||
Reference in New Issue
Block a user