From 1f59002705253c4f9488ed08a3dd836a851c1456 Mon Sep 17 00:00:00 2001 From: anakin87 Date: Fri, 6 Feb 2026 16:09:46 +0100 Subject: [PATCH 1/2] test: mock Mem0 in Agent serde tests --- test/components/agents/test_agent.py | 6 ++++-- test/conftest.py | 10 +++++++++- test/memory_stores/test_mem0_memory_store.py | 8 -------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/test/components/agents/test_agent.py b/test/components/agents/test_agent.py index 18069d22..093f12d9 100644 --- a/test/components/agents/test_agent.py +++ b/test/components/agents/test_agent.py @@ -197,8 +197,9 @@ def tools() -> list[Tool]: class TestAgent: - def test_to_dict(self, tools, monkeypatch): + def test_to_dict(self, tools, monkeypatch, mock_memory_client): monkeypatch.setenv("OPENAI_API_KEY", "test") + monkeypatch.setenv("MEM0_API_KEY", "test") agent = Agent( chat_generator=OpenAIChatGenerator(model="gpt-4o-mini"), tools=tools, chat_message_store=InMemoryChatMessageStore(), memory_store=Mem0MemoryStore() @@ -267,8 +268,9 @@ def test_to_dict(self, tools, monkeypatch): }, } - def test_from_dict(self, tools, monkeypatch): + def test_from_dict(self, tools, monkeypatch, mock_memory_client): monkeypatch.setenv("OPENAI_API_KEY", "test") + monkeypatch.setenv("MEM0_API_KEY", "test") agent = Agent( chat_generator=OpenAIChatGenerator(), tools=tools, chat_message_store=InMemoryChatMessageStore(), memory_store=Mem0MemoryStore() diff --git a/test/conftest.py b/test/conftest.py index 7a91cd4a..c3dcc3ef 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -5,7 +5,7 @@ from pathlib import Path import pytest - +from unittest.mock import Mock, patch from haystack.testing.test_utils import set_all_seeds set_all_seeds(0) @@ -14,3 +14,11 @@ @pytest.fixture() def test_files_path(): return Path(__file__).parent / "test_files" + +@pytest.fixture +def mock_memory_client(): + """Mock the Mem0 MemoryClient.""" + with patch("haystack_experimental.memory_stores.mem0.memory_store.MemoryClient") as mock_client_class: + mock_client = Mock() + mock_client_class.return_value = mock_client + yield mock_client diff --git a/test/memory_stores/test_mem0_memory_store.py b/test/memory_stores/test_mem0_memory_store.py index db2b8386..c59a8ee9 100644 --- a/test/memory_stores/test_mem0_memory_store.py +++ b/test/memory_stores/test_mem0_memory_store.py @@ -35,14 +35,6 @@ def memory_store(): class TestMem0MemoryStore: - @pytest.fixture - def mock_memory_client(self): - """Mock the Mem0 MemoryClient.""" - with patch("haystack_experimental.memory_stores.mem0.memory_store.MemoryClient") as mock_client_class: - mock_client = Mock() - mock_client_class.return_value = mock_client - yield mock_client - @pytest.fixture def sample_messages(self): """Sample ChatMessage objects for testing.""" From fd9138af257c54be2fca1fbaf0688646b76274ff Mon Sep 17 00:00:00 2001 From: anakin87 Date: Fri, 6 Feb 2026 16:13:06 +0100 Subject: [PATCH 2/2] clearer name --- test/components/agents/test_agent.py | 4 ++-- test/conftest.py | 2 +- test/memory_stores/test_mem0_memory_store.py | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/components/agents/test_agent.py b/test/components/agents/test_agent.py index 093f12d9..580f3471 100644 --- a/test/components/agents/test_agent.py +++ b/test/components/agents/test_agent.py @@ -197,7 +197,7 @@ def tools() -> list[Tool]: class TestAgent: - def test_to_dict(self, tools, monkeypatch, mock_memory_client): + def test_to_dict(self, tools, monkeypatch, mock_mem0_memory_client): monkeypatch.setenv("OPENAI_API_KEY", "test") monkeypatch.setenv("MEM0_API_KEY", "test") agent = Agent( @@ -268,7 +268,7 @@ def test_to_dict(self, tools, monkeypatch, mock_memory_client): }, } - def test_from_dict(self, tools, monkeypatch, mock_memory_client): + def test_from_dict(self, tools, monkeypatch, mock_mem0_memory_client): monkeypatch.setenv("OPENAI_API_KEY", "test") monkeypatch.setenv("MEM0_API_KEY", "test") agent = Agent( diff --git a/test/conftest.py b/test/conftest.py index c3dcc3ef..b087eab9 100644 --- a/test/conftest.py +++ b/test/conftest.py @@ -16,7 +16,7 @@ def test_files_path(): return Path(__file__).parent / "test_files" @pytest.fixture -def mock_memory_client(): +def mock_mem0_memory_client(): """Mock the Mem0 MemoryClient.""" with patch("haystack_experimental.memory_stores.mem0.memory_store.MemoryClient") as mock_client_class: mock_client = Mock() diff --git a/test/memory_stores/test_mem0_memory_store.py b/test/memory_stores/test_mem0_memory_store.py index c59a8ee9..df2dd0cc 100644 --- a/test/memory_stores/test_mem0_memory_store.py +++ b/test/memory_stores/test_mem0_memory_store.py @@ -43,17 +43,17 @@ def sample_messages(self): ChatMessage.from_user("I like working with Haystack.", meta={"topic": "programming"}), ] - def test_init_with_user_id_and_api_key(self, mock_memory_client): + def test_init_with_user_id_and_api_key(self, mock_mem0_memory_client): """Test initialization with user_id and api_key.""" with patch.dict(os.environ, {}, clear=True): store = Mem0MemoryStore(api_key=Secret.from_token("test_api_key_12345")) - assert store.client == mock_memory_client + assert store.client == mock_mem0_memory_client - def test_init_with_params(self, mock_memory_client): + def test_init_with_params(self, mock_mem0_memory_client): store = Mem0MemoryStore( api_key=Secret.from_token("test_api_key_12345") ) - assert store.client == mock_memory_client + assert store.client == mock_mem0_memory_client def test_init_without_api_key_raises_error(self): """Test that initialization without API key raises ValueError.""" @@ -62,7 +62,7 @@ def test_init_without_api_key_raises_error(self): match="None of the following authentication environment variables are set"): Mem0MemoryStore() - def test_to_dict(self, monkeypatch, mock_memory_client): + def test_to_dict(self, monkeypatch, mock_mem0_memory_client): """Test serialization to dictionary.""" with patch.dict(os.environ, {}, clear=True): monkeypatch.setenv("ENV_VAR", "test_api_key_12345") @@ -72,7 +72,7 @@ def test_to_dict(self, monkeypatch, mock_memory_client): result = store.to_dict() assert result["init_parameters"]["api_key"] == {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"} - def test_from_dict(self, monkeypatch, mock_memory_client): + def test_from_dict(self, monkeypatch, mock_mem0_memory_client): with patch.dict(os.environ, {}, clear=True): monkeypatch.setenv("ENV_VAR", "test_api_key_12345") data = { @@ -80,7 +80,7 @@ def test_from_dict(self, monkeypatch, mock_memory_client): 'init_parameters': { "api_key": {"env_vars": ["ENV_VAR"], "strict": True, "type": "env_var"}, }} store = Mem0MemoryStore.from_dict(data) - assert store.client == mock_memory_client + assert store.client == mock_mem0_memory_client assert store.api_key == Secret.from_env_var("ENV_VAR") @pytest.mark.skipif(