|
15 | 15 | class MockCrew:
|
16 | 16 | def __init__(self, memory_config):
|
17 | 17 | self.memory_config = memory_config
|
| 18 | + self.agents = [MagicMock(role="Test Agent")] |
18 | 19 |
|
19 | 20 |
|
20 | 21 | @pytest.fixture
|
@@ -107,11 +108,13 @@ def mem0_storage_with_memory_client_using_config_from_crew(mock_mem0_memory_clie
|
107 | 108 |
|
108 | 109 |
|
109 | 110 | @pytest.fixture
|
110 |
| -def mem0_storage_with_memory_client_using_explictly_config(mock_mem0_memory_client): |
| 111 | +def mem0_storage_with_memory_client_using_explictly_config(mock_mem0_memory_client, mock_mem0_memory): |
111 | 112 | """Fixture to create a Mem0Storage instance with mocked dependencies"""
|
112 | 113 |
|
113 |
| - # We need to patch the MemoryClient before it's instantiated |
114 |
| - with patch.object(MemoryClient, "__new__", return_value=mock_mem0_memory_client): |
| 114 | + # We need to patch both MemoryClient and Memory to prevent actual initialization |
| 115 | + with patch.object(MemoryClient, "__new__", return_value=mock_mem0_memory_client), \ |
| 116 | + patch.object(Memory, "__new__", return_value=mock_mem0_memory): |
| 117 | + |
115 | 118 | crew = MockCrew(
|
116 | 119 | memory_config={
|
117 | 120 | "provider": "mem0",
|
@@ -155,3 +158,82 @@ def test_mem0_storage_with_explict_config(
|
155 | 158 | mem0_storage_with_memory_client_using_explictly_config.memory_config
|
156 | 159 | == expected_config
|
157 | 160 | )
|
| 161 | + |
| 162 | + |
| 163 | +def test_save_method_with_memory_oss(mem0_storage_with_mocked_config): |
| 164 | + """Test save method for different memory types""" |
| 165 | + mem0_storage, _, _ = mem0_storage_with_mocked_config |
| 166 | + mem0_storage.memory.add = MagicMock() |
| 167 | + |
| 168 | + # Test short_term memory type (already set in fixture) |
| 169 | + test_value = "This is a test memory" |
| 170 | + test_metadata = {"key": "value"} |
| 171 | + |
| 172 | + mem0_storage.save(test_value, test_metadata) |
| 173 | + |
| 174 | + mem0_storage.memory.add.assert_called_once_with( |
| 175 | + test_value, |
| 176 | + agent_id="Test_Agent", |
| 177 | + infer=False, |
| 178 | + metadata={"type": "short_term", "key": "value"}, |
| 179 | + ) |
| 180 | + |
| 181 | + |
| 182 | +def test_save_method_with_memory_client(mem0_storage_with_memory_client_using_config_from_crew): |
| 183 | + """Test save method for different memory types""" |
| 184 | + mem0_storage = mem0_storage_with_memory_client_using_config_from_crew |
| 185 | + mem0_storage.memory.add = MagicMock() |
| 186 | + |
| 187 | + # Test short_term memory type (already set in fixture) |
| 188 | + test_value = "This is a test memory" |
| 189 | + test_metadata = {"key": "value"} |
| 190 | + |
| 191 | + mem0_storage.save(test_value, test_metadata) |
| 192 | + |
| 193 | + mem0_storage.memory.add.assert_called_once_with( |
| 194 | + test_value, |
| 195 | + agent_id="Test_Agent", |
| 196 | + infer=False, |
| 197 | + metadata={"type": "short_term", "key": "value"}, |
| 198 | + output_format="v1.1" |
| 199 | + ) |
| 200 | + |
| 201 | + |
| 202 | +def test_search_method_with_memory_oss(mem0_storage_with_mocked_config): |
| 203 | + """Test search method for different memory types""" |
| 204 | + mem0_storage, _, _ = mem0_storage_with_mocked_config |
| 205 | + mock_results = {"results": [{"score": 0.9, "content": "Result 1"}, {"score": 0.4, "content": "Result 2"}]} |
| 206 | + mem0_storage.memory.search = MagicMock(return_value=mock_results) |
| 207 | + |
| 208 | + results = mem0_storage.search("test query", limit=5, score_threshold=0.5) |
| 209 | + |
| 210 | + mem0_storage.memory.search.assert_called_once_with( |
| 211 | + query="test query", |
| 212 | + limit=5, |
| 213 | + agent_id="Test_Agent", |
| 214 | + user_id="test_user" |
| 215 | + ) |
| 216 | + |
| 217 | + assert len(results) == 1 |
| 218 | + assert results[0]["content"] == "Result 1" |
| 219 | + |
| 220 | + |
| 221 | +def test_search_method_with_memory_client(mem0_storage_with_memory_client_using_config_from_crew): |
| 222 | + """Test search method for different memory types""" |
| 223 | + mem0_storage = mem0_storage_with_memory_client_using_config_from_crew |
| 224 | + mock_results = {"results": [{"score": 0.9, "content": "Result 1"}, {"score": 0.4, "content": "Result 2"}]} |
| 225 | + mem0_storage.memory.search = MagicMock(return_value=mock_results) |
| 226 | + |
| 227 | + results = mem0_storage.search("test query", limit=5, score_threshold=0.5) |
| 228 | + |
| 229 | + mem0_storage.memory.search.assert_called_once_with( |
| 230 | + query="test query", |
| 231 | + limit=5, |
| 232 | + agent_id="Test_Agent", |
| 233 | + metadata={"type": "short_term"}, |
| 234 | + user_id="test_user", |
| 235 | + output_format='v1.1' |
| 236 | + ) |
| 237 | + |
| 238 | + assert len(results) == 1 |
| 239 | + assert results[0]["content"] == "Result 1" |
0 commit comments