|
29 | 29 | "hosts": json.loads(os.getenv("NEBULAR_HOSTS", "localhost")), |
30 | 30 | "user_name": os.getenv("NEBULAR_USER", "root"), |
31 | 31 | "password": os.getenv("NEBULAR_PASSWORD", "xxxxxx"), |
32 | | - "space": "test_memory_count", |
| 32 | + "space": "memory_graph", |
33 | 33 | "auto_create": True, |
34 | 34 | "embedding_dimension": 3072, |
35 | 35 | "use_multi_db": False, |
@@ -224,3 +224,187 @@ def test_get_edges(): |
224 | 224 | assert edges[0]["from"] == source.id |
225 | 225 | assert edges[0]["to"] == target.id |
226 | 226 | assert edges[0]["type"] == "PARENT" |
| 227 | + |
| 228 | + |
| 229 | +def test_get_all_memory_items(): |
| 230 | + graph = GraphStoreFactory.from_config( |
| 231 | + GraphDBConfigFactory( |
| 232 | + backend="nebular", |
| 233 | + config=nebular_config, |
| 234 | + ) |
| 235 | + ) |
| 236 | + graph.clear() |
| 237 | + |
| 238 | + # Insert 2 WorkingMemory items |
| 239 | + for i in range(2): |
| 240 | + mem = TextualMemoryItem( |
| 241 | + memory=f"Memory {i}", |
| 242 | + metadata=TreeNodeTextualMemoryMetadata( |
| 243 | + memory_type="WorkingMemory", |
| 244 | + key="Research Topic", |
| 245 | + hierarchy_level="topic", |
| 246 | + type="fact", |
| 247 | + memory_time="2024-01-01", |
| 248 | + status="activated", |
| 249 | + visibility="public", |
| 250 | + updated_at=now, |
| 251 | + embedding=embed_memory_item(f"Memory {i}"), |
| 252 | + ), |
| 253 | + ) |
| 254 | + graph.add_node(mem.id, mem.memory, mem.metadata.model_dump(exclude_none=True)) |
| 255 | + |
| 256 | + # Retrieve all memory items of type WorkingMemory |
| 257 | + items = graph.get_all_memory_items("WorkingMemory") |
| 258 | + assert len(items) == 2 |
| 259 | + assert all(item["properties"]["memory_type"] == "WorkingMemory" for item in items) |
| 260 | + |
| 261 | + |
| 262 | +def test_get_structure_optimization_candidates(): |
| 263 | + graph = GraphStoreFactory.from_config( |
| 264 | + GraphDBConfigFactory( |
| 265 | + backend="nebular", |
| 266 | + config=nebular_config, |
| 267 | + ) |
| 268 | + ) |
| 269 | + graph.clear() |
| 270 | + |
| 271 | + # Insert one isolated node (no parent or child) |
| 272 | + mem = TextualMemoryItem( |
| 273 | + memory="Isolated memory", |
| 274 | + metadata=TreeNodeTextualMemoryMetadata( |
| 275 | + memory_type="LongTermMemory", |
| 276 | + key="Research Topic", |
| 277 | + hierarchy_level="topic", |
| 278 | + type="fact", |
| 279 | + memory_time="2024-01-01", |
| 280 | + status="activated", |
| 281 | + visibility="public", |
| 282 | + updated_at=now, |
| 283 | + embedding=embed_memory_item("Isolated memory"), |
| 284 | + ), |
| 285 | + ) |
| 286 | + graph.add_node(mem.id, mem.memory, mem.metadata.model_dump(exclude_none=True)) |
| 287 | + |
| 288 | + # Insert one node with empty background (and no edges) |
| 289 | + mem2 = TextualMemoryItem( |
| 290 | + memory="Empty background memory", |
| 291 | + metadata=TreeNodeTextualMemoryMetadata( |
| 292 | + memory_type="LongTermMemory", |
| 293 | + key="Research Topic", |
| 294 | + hierarchy_level="topic", |
| 295 | + type="fact", |
| 296 | + memory_time="2024-01-01", |
| 297 | + status="activated", |
| 298 | + visibility="public", |
| 299 | + updated_at=now, |
| 300 | + embedding=embed_memory_item("Empty background memory"), |
| 301 | + ), |
| 302 | + ) |
| 303 | + graph.add_node(mem2.id, mem2.memory, mem2.metadata.model_dump(exclude_none=True)) |
| 304 | + |
| 305 | + # Find optimization candidates |
| 306 | + candidates = graph.get_structure_optimization_candidates("LongTermMemory") |
| 307 | + print("Optimization candidates:", candidates) |
| 308 | + assert any("Isolated memory" in c["memory"] for c in candidates) |
| 309 | + assert any("Empty background memory" in c["memory"] for c in candidates) |
| 310 | + |
| 311 | + |
| 312 | +def test_drop_database(): |
| 313 | + config = GraphDBConfigFactory( |
| 314 | + backend="nebular", |
| 315 | + config=nebular_config, |
| 316 | + ) |
| 317 | + graph = GraphStoreFactory.from_config(config) |
| 318 | + |
| 319 | + # Create a dummy node |
| 320 | + mem = TextualMemoryItem( |
| 321 | + memory="Temp for drop DB", |
| 322 | + metadata=TreeNodeTextualMemoryMetadata( |
| 323 | + memory_type="LongTermMemory", |
| 324 | + key="Research Topic", |
| 325 | + hierarchy_level="topic", |
| 326 | + type="fact", |
| 327 | + memory_time="2024-01-01", |
| 328 | + status="activated", |
| 329 | + visibility="public", |
| 330 | + updated_at=now, |
| 331 | + embedding=embed_memory_item("Temp for drop DB"), |
| 332 | + ), |
| 333 | + ) |
| 334 | + graph.add_node(mem.id, mem.memory, mem.metadata.model_dump(exclude_none=True)) |
| 335 | + |
| 336 | + # Drop the database |
| 337 | + graph.drop_database() |
| 338 | + |
| 339 | + # Attempting any operation afterward should raise an error or fail (optional) |
| 340 | + try: |
| 341 | + _ = graph.get_all_memory_items("WorkingMemory") |
| 342 | + except Exception as e: |
| 343 | + print("Expected exception after DB drop:", str(e)) |
| 344 | + assert "Current working graph not found" in str(e) |
| 345 | + |
| 346 | + |
| 347 | +def test_get_by_metadata(): |
| 348 | + config = GraphDBConfigFactory( |
| 349 | + backend="nebular", |
| 350 | + config=nebular_config, |
| 351 | + ) |
| 352 | + graph = GraphStoreFactory.from_config(config) |
| 353 | + graph.clear() |
| 354 | + |
| 355 | + mem1 = TextualMemoryItem( |
| 356 | + memory="AI for science", |
| 357 | + metadata=TreeNodeTextualMemoryMetadata( |
| 358 | + memory_type="LongTermMemory", |
| 359 | + key="AI Science", |
| 360 | + confidence=92.5, |
| 361 | + tags=["AI", "science"], |
| 362 | + hierarchy_level="topic", |
| 363 | + type="fact", |
| 364 | + memory_time="2024-01-01", |
| 365 | + status="activated", |
| 366 | + visibility="public", |
| 367 | + updated_at=now, |
| 368 | + embedding=embed_memory_item("AI for science"), |
| 369 | + ), |
| 370 | + ) |
| 371 | + mem2 = TextualMemoryItem( |
| 372 | + memory="Neurosymbolic reasoning", |
| 373 | + metadata=TreeNodeTextualMemoryMetadata( |
| 374 | + memory_type="LongTermMemory", |
| 375 | + key="Neurosymbolic", |
| 376 | + tags=["symbolic", "reasoning"], |
| 377 | + confidence=88.0, |
| 378 | + hierarchy_level="topic", |
| 379 | + type="fact", |
| 380 | + memory_time="2024-01-01", |
| 381 | + status="activated", |
| 382 | + visibility="public", |
| 383 | + updated_at=now, |
| 384 | + embedding=embed_memory_item("Neurosymbolic reasoning"), |
| 385 | + ), |
| 386 | + ) |
| 387 | + graph.add_node(mem1.id, mem1.memory, mem1.metadata.model_dump(exclude_none=True)) |
| 388 | + graph.add_node(mem2.id, mem2.memory, mem2.metadata.model_dump(exclude_none=True)) |
| 389 | + |
| 390 | + # Exact match filter |
| 391 | + result_ids = graph.get_by_metadata([{"field": "key", "op": "=", "value": '"AI Science"'}]) |
| 392 | + assert mem1.id in result_ids |
| 393 | + assert mem2.id not in result_ids |
| 394 | + |
| 395 | + # Confidence filter |
| 396 | + result_ids = graph.get_by_metadata([{"field": "confidence", "op": ">=", "value": 90.0}]) |
| 397 | + assert mem1.id in result_ids |
| 398 | + assert mem2.id not in result_ids |
| 399 | + |
| 400 | + # Tag contains filter TODO |
| 401 | + result_ids = graph.get_by_metadata([{"field": "tags", "op": "contains", "value": '["AI"]'}]) |
| 402 | + assert mem1.id in result_ids |
| 403 | + assert mem2.id not in result_ids |
| 404 | + |
| 405 | + # In set filter |
| 406 | + result_ids = graph.get_by_metadata( |
| 407 | + [{"field": "key", "op": "in", "value": '["AI Science", "Neurosymbolic"]'}] |
| 408 | + ) |
| 409 | + assert mem1.id in result_ids |
| 410 | + assert mem2.id in result_ids |
0 commit comments