cognee 0.5.1__py3-none-any.whl → 0.5.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- cognee/__init__.py +2 -0
- cognee/alembic/README +1 -0
- cognee/alembic/env.py +107 -0
- cognee/alembic/script.py.mako +26 -0
- cognee/alembic/versions/1a58b986e6e1_enable_delete_for_old_tutorial_notebooks.py +52 -0
- cognee/alembic/versions/1d0bb7fede17_add_pipeline_run_status.py +33 -0
- cognee/alembic/versions/1daae0df1866_incremental_loading.py +48 -0
- cognee/alembic/versions/211ab850ef3d_add_sync_operations_table.py +118 -0
- cognee/alembic/versions/45957f0a9849_add_notebook_table.py +46 -0
- cognee/alembic/versions/46a6ce2bd2b2_expand_dataset_database_with_json_.py +333 -0
- cognee/alembic/versions/482cd6517ce4_add_default_user.py +30 -0
- cognee/alembic/versions/76625596c5c3_expand_dataset_database_for_multi_user.py +98 -0
- cognee/alembic/versions/8057ae7329c2_initial_migration.py +25 -0
- cognee/alembic/versions/9e7a3cb85175_loader_separation.py +104 -0
- cognee/alembic/versions/a1b2c3d4e5f6_add_label_column_to_data.py +38 -0
- cognee/alembic/versions/ab7e313804ae_permission_system_rework.py +236 -0
- cognee/alembic/versions/b9274c27a25a_kuzu_11_migration.py +75 -0
- cognee/alembic/versions/c946955da633_multi_tenant_support.py +137 -0
- cognee/alembic/versions/e1ec1dcb50b6_add_last_accessed_to_data.py +51 -0
- cognee/alembic/versions/e4ebee1091e7_expand_data_model_info.py +140 -0
- cognee/alembic.ini +117 -0
- cognee/api/v1/add/add.py +2 -1
- cognee/api/v1/add/routers/get_add_router.py +2 -0
- cognee/api/v1/cognify/cognify.py +11 -6
- cognee/api/v1/cognify/routers/get_cognify_router.py +8 -0
- cognee/api/v1/config/config.py +60 -0
- cognee/api/v1/datasets/routers/get_datasets_router.py +46 -3
- cognee/api/v1/memify/routers/get_memify_router.py +3 -0
- cognee/api/v1/search/routers/get_search_router.py +21 -6
- cognee/api/v1/search/search.py +21 -5
- cognee/api/v1/sync/routers/get_sync_router.py +3 -3
- cognee/cli/commands/add_command.py +1 -1
- cognee/cli/commands/cognify_command.py +6 -0
- cognee/cli/commands/config_command.py +1 -1
- cognee/context_global_variables.py +5 -1
- cognee/eval_framework/answer_generation/answer_generation_executor.py +7 -8
- cognee/infrastructure/databases/cache/cache_db_interface.py +38 -1
- cognee/infrastructure/databases/cache/config.py +6 -0
- cognee/infrastructure/databases/cache/fscache/FsCacheAdapter.py +21 -0
- cognee/infrastructure/databases/cache/get_cache_engine.py +9 -3
- cognee/infrastructure/databases/cache/redis/RedisAdapter.py +60 -1
- cognee/infrastructure/databases/dataset_database_handler/supported_dataset_database_handlers.py +7 -0
- cognee/infrastructure/databases/graph/get_graph_engine.py +29 -1
- cognee/infrastructure/databases/graph/neo4j_driver/Neo4jAuraDevDatasetDatabaseHandler.py +62 -27
- cognee/infrastructure/databases/hybrid/neptune_analytics/NeptuneAnalyticsAdapter.py +17 -4
- cognee/infrastructure/databases/relational/config.py +16 -1
- cognee/infrastructure/databases/relational/create_relational_engine.py +13 -3
- cognee/infrastructure/databases/relational/sqlalchemy/SqlAlchemyAdapter.py +26 -3
- cognee/infrastructure/databases/vector/chromadb/ChromaDBAdapter.py +2 -0
- cognee/infrastructure/databases/vector/config.py +6 -0
- cognee/infrastructure/databases/vector/create_vector_engine.py +70 -16
- cognee/infrastructure/databases/vector/embeddings/LiteLLMEmbeddingEngine.py +64 -9
- cognee/infrastructure/databases/vector/embeddings/OllamaEmbeddingEngine.py +13 -2
- cognee/infrastructure/databases/vector/lancedb/LanceDBAdapter.py +16 -3
- cognee/infrastructure/databases/vector/models/ScoredResult.py +3 -3
- cognee/infrastructure/databases/vector/pgvector/PGVectorAdapter.py +16 -3
- cognee/infrastructure/databases/vector/pgvector/PGVectorDatasetDatabaseHandler.py +86 -0
- cognee/infrastructure/databases/vector/pgvector/create_db_and_tables.py +81 -2
- cognee/infrastructure/databases/vector/vector_db_interface.py +8 -0
- cognee/infrastructure/files/utils/get_data_file_path.py +33 -27
- cognee/infrastructure/llm/LLMGateway.py +0 -13
- cognee/infrastructure/llm/prompts/extract_query_time.txt +1 -1
- cognee/infrastructure/llm/prompts/generate_event_entity_prompt.txt +1 -1
- cognee/infrastructure/llm/prompts/generate_event_graph_prompt.txt +1 -1
- cognee/infrastructure/llm/prompts/generate_graph_prompt.txt +2 -2
- cognee/infrastructure/llm/prompts/generate_graph_prompt_guided.txt +1 -1
- cognee/infrastructure/llm/prompts/generate_graph_prompt_oneshot.txt +2 -2
- cognee/infrastructure/llm/prompts/generate_graph_prompt_simple.txt +1 -1
- cognee/infrastructure/llm/prompts/generate_graph_prompt_strict.txt +1 -1
- cognee/infrastructure/llm/prompts/search_type_selector_prompt.txt +6 -6
- cognee/infrastructure/llm/prompts/test.txt +1 -1
- cognee/infrastructure/llm/prompts/translate_content.txt +19 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/anthropic/adapter.py +17 -12
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/gemini/adapter.py +31 -25
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/generic_llm_api/adapter.py +132 -7
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/get_llm_client.py +29 -5
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llama_cpp/adapter.py +191 -0
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/llm_interface.py +2 -6
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/mistral/adapter.py +58 -13
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/ollama/adapter.py +0 -1
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/openai/adapter.py +25 -131
- cognee/infrastructure/llm/structured_output_framework/litellm_instructor/llm/types.py +10 -0
- cognee/modules/chunking/models/DocumentChunk.py +0 -1
- cognee/modules/cognify/config.py +2 -0
- cognee/modules/data/models/Data.py +3 -1
- cognee/modules/engine/models/Entity.py +0 -1
- cognee/modules/engine/operations/setup.py +6 -0
- cognee/modules/graph/cognee_graph/CogneeGraph.py +150 -37
- cognee/modules/graph/cognee_graph/CogneeGraphElements.py +48 -2
- cognee/modules/graph/utils/__init__.py +1 -0
- cognee/modules/graph/utils/get_entity_nodes_from_triplets.py +12 -0
- cognee/modules/notebooks/methods/__init__.py +1 -0
- cognee/modules/notebooks/methods/create_notebook.py +0 -34
- cognee/modules/notebooks/methods/create_tutorial_notebooks.py +191 -0
- cognee/modules/notebooks/methods/get_notebooks.py +12 -8
- cognee/modules/notebooks/tutorials/cognee-basics/cell-1.md +3 -0
- cognee/modules/notebooks/tutorials/cognee-basics/cell-2.md +10 -0
- cognee/modules/notebooks/tutorials/cognee-basics/cell-3.md +7 -0
- cognee/modules/notebooks/tutorials/cognee-basics/cell-4.py +28 -0
- cognee/modules/notebooks/tutorials/cognee-basics/cell-5.py +3 -0
- cognee/modules/notebooks/tutorials/cognee-basics/cell-6.py +9 -0
- cognee/modules/notebooks/tutorials/cognee-basics/cell-7.py +17 -0
- cognee/modules/notebooks/tutorials/cognee-basics/config.json +4 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-1.md +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-10.md +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-11.md +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-12.py +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-13.md +7 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-14.py +6 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-15.md +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-16.py +7 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-2.md +9 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-3.md +7 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-4.md +9 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-5.md +5 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-6.py +13 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-7.md +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-8.md +3 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/cell-9.py +31 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/config.json +4 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/data/copilot_conversations.json +107 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/data/guido_contributions.json +976 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/data/my_developer_rules.md +79 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/data/pep_style_guide.md +74 -0
- cognee/modules/notebooks/tutorials/python-development-with-cognee/data/zen_principles.md +74 -0
- cognee/modules/retrieval/EntityCompletionRetriever.py +51 -38
- cognee/modules/retrieval/__init__.py +0 -1
- cognee/modules/retrieval/base_retriever.py +66 -10
- cognee/modules/retrieval/chunks_retriever.py +57 -49
- cognee/modules/retrieval/coding_rules_retriever.py +12 -5
- cognee/modules/retrieval/completion_retriever.py +29 -28
- cognee/modules/retrieval/cypher_search_retriever.py +25 -20
- cognee/modules/retrieval/graph_completion_context_extension_retriever.py +42 -46
- cognee/modules/retrieval/graph_completion_cot_retriever.py +68 -51
- cognee/modules/retrieval/graph_completion_retriever.py +78 -63
- cognee/modules/retrieval/graph_summary_completion_retriever.py +2 -0
- cognee/modules/retrieval/lexical_retriever.py +34 -12
- cognee/modules/retrieval/natural_language_retriever.py +18 -15
- cognee/modules/retrieval/summaries_retriever.py +51 -34
- cognee/modules/retrieval/temporal_retriever.py +59 -49
- cognee/modules/retrieval/triplet_retriever.py +32 -33
- cognee/modules/retrieval/utils/access_tracking.py +88 -0
- cognee/modules/retrieval/utils/brute_force_triplet_search.py +99 -103
- cognee/modules/retrieval/utils/node_edge_vector_search.py +174 -0
- cognee/modules/search/methods/__init__.py +1 -0
- cognee/modules/search/methods/get_retriever_output.py +53 -0
- cognee/modules/search/methods/get_search_type_retriever_instance.py +252 -0
- cognee/modules/search/methods/search.py +90 -222
- cognee/modules/search/models/SearchResultPayload.py +67 -0
- cognee/modules/search/types/SearchResult.py +1 -8
- cognee/modules/search/types/SearchType.py +1 -2
- cognee/modules/search/types/__init__.py +1 -1
- cognee/modules/search/utils/__init__.py +1 -2
- cognee/modules/search/utils/transform_insights_to_graph.py +2 -2
- cognee/modules/search/utils/{transform_context_to_graph.py → transform_triplets_to_graph.py} +2 -2
- cognee/modules/users/authentication/default/default_transport.py +11 -1
- cognee/modules/users/authentication/get_api_auth_backend.py +2 -1
- cognee/modules/users/authentication/get_client_auth_backend.py +2 -1
- cognee/modules/users/methods/create_user.py +0 -9
- cognee/modules/users/permissions/methods/has_user_management_permission.py +29 -0
- cognee/modules/visualization/cognee_network_visualization.py +1 -1
- cognee/run_migrations.py +48 -0
- cognee/shared/exceptions/__init__.py +1 -3
- cognee/shared/exceptions/exceptions.py +11 -1
- cognee/shared/usage_logger.py +332 -0
- cognee/shared/utils.py +12 -5
- cognee/tasks/chunks/__init__.py +9 -0
- cognee/tasks/cleanup/cleanup_unused_data.py +172 -0
- cognee/tasks/graph/__init__.py +7 -0
- cognee/tasks/ingestion/data_item.py +8 -0
- cognee/tasks/ingestion/ingest_data.py +12 -1
- cognee/tasks/ingestion/save_data_item_to_storage.py +5 -0
- cognee/tasks/memify/__init__.py +8 -0
- cognee/tasks/memify/extract_usage_frequency.py +613 -0
- cognee/tasks/summarization/models.py +0 -2
- cognee/tasks/temporal_graph/__init__.py +0 -1
- cognee/tasks/translation/__init__.py +96 -0
- cognee/tasks/translation/config.py +110 -0
- cognee/tasks/translation/detect_language.py +190 -0
- cognee/tasks/translation/exceptions.py +62 -0
- cognee/tasks/translation/models.py +72 -0
- cognee/tasks/translation/providers/__init__.py +44 -0
- cognee/tasks/translation/providers/azure_provider.py +192 -0
- cognee/tasks/translation/providers/base.py +85 -0
- cognee/tasks/translation/providers/google_provider.py +158 -0
- cognee/tasks/translation/providers/llm_provider.py +143 -0
- cognee/tasks/translation/translate_content.py +282 -0
- cognee/tasks/web_scraper/default_url_crawler.py +6 -2
- cognee/tests/cli_tests/cli_unit_tests/test_cli_commands.py +1 -0
- cognee/tests/cli_tests/cli_unit_tests/test_cli_edge_cases.py +3 -0
- cognee/tests/integration/retrieval/test_brute_force_triplet_search_with_cognify.py +62 -0
- cognee/tests/integration/retrieval/test_chunks_retriever.py +351 -0
- cognee/tests/integration/retrieval/test_graph_completion_retriever.py +276 -0
- cognee/tests/integration/retrieval/test_graph_completion_retriever_context_extension.py +228 -0
- cognee/tests/integration/retrieval/test_graph_completion_retriever_cot.py +217 -0
- cognee/tests/integration/retrieval/test_rag_completion_retriever.py +319 -0
- cognee/tests/integration/retrieval/test_structured_output.py +258 -0
- cognee/tests/integration/retrieval/test_summaries_retriever.py +195 -0
- cognee/tests/integration/retrieval/test_temporal_retriever.py +336 -0
- cognee/tests/integration/retrieval/test_triplet_retriever.py +45 -1
- cognee/tests/integration/shared/test_usage_logger_integration.py +255 -0
- cognee/tests/tasks/translation/README.md +147 -0
- cognee/tests/tasks/translation/__init__.py +1 -0
- cognee/tests/tasks/translation/config_test.py +93 -0
- cognee/tests/tasks/translation/detect_language_test.py +118 -0
- cognee/tests/tasks/translation/providers_test.py +151 -0
- cognee/tests/tasks/translation/translate_content_test.py +213 -0
- cognee/tests/test_chromadb.py +1 -1
- cognee/tests/test_cleanup_unused_data.py +165 -0
- cognee/tests/test_custom_data_label.py +68 -0
- cognee/tests/test_delete_by_id.py +6 -6
- cognee/tests/test_extract_usage_frequency.py +308 -0
- cognee/tests/test_kuzu.py +17 -7
- cognee/tests/test_lancedb.py +3 -1
- cognee/tests/test_library.py +1 -1
- cognee/tests/test_neo4j.py +17 -7
- cognee/tests/test_neptune_analytics_vector.py +3 -1
- cognee/tests/test_permissions.py +172 -187
- cognee/tests/test_pgvector.py +3 -1
- cognee/tests/test_relational_db_migration.py +15 -1
- cognee/tests/test_remote_kuzu.py +3 -1
- cognee/tests/test_s3_file_storage.py +1 -1
- cognee/tests/test_search_db.py +345 -205
- cognee/tests/test_usage_logger_e2e.py +268 -0
- cognee/tests/unit/api/test_get_raw_data_endpoint.py +206 -0
- cognee/tests/unit/eval_framework/answer_generation_test.py +4 -3
- cognee/tests/unit/eval_framework/benchmark_adapters_test.py +25 -0
- cognee/tests/unit/eval_framework/corpus_builder_test.py +33 -4
- cognee/tests/unit/infrastructure/databases/cache/test_cache_config.py +2 -0
- cognee/tests/unit/infrastructure/databases/relational/test_RelationalConfig.py +69 -0
- cognee/tests/unit/modules/graph/cognee_graph_elements_test.py +42 -2
- cognee/tests/unit/modules/graph/cognee_graph_test.py +329 -31
- cognee/tests/unit/modules/retrieval/chunks_retriever_test.py +122 -168
- cognee/tests/unit/modules/retrieval/conversation_history_test.py +338 -0
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_context_extension_test.py +486 -157
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_cot_test.py +693 -155
- cognee/tests/unit/modules/retrieval/graph_completion_retriever_test.py +619 -200
- cognee/tests/unit/modules/retrieval/rag_completion_retriever_test.py +300 -171
- cognee/tests/unit/modules/retrieval/summaries_retriever_test.py +184 -155
- cognee/tests/unit/modules/retrieval/temporal_retriever_test.py +544 -79
- cognee/tests/unit/modules/retrieval/test_brute_force_triplet_search.py +476 -28
- cognee/tests/unit/modules/retrieval/test_completion.py +343 -0
- cognee/tests/unit/modules/retrieval/test_graph_summary_completion_retriever.py +157 -0
- cognee/tests/unit/modules/retrieval/test_node_edge_vector_search.py +273 -0
- cognee/tests/unit/modules/retrieval/test_user_qa_feedback.py +312 -0
- cognee/tests/unit/modules/retrieval/triplet_retriever_test.py +267 -7
- cognee/tests/unit/modules/search/test_get_search_type_retriever_instance.py +125 -0
- cognee/tests/unit/modules/search/test_search.py +96 -20
- cognee/tests/unit/modules/search/test_search_prepare_search_result_contract.py +190 -0
- cognee/tests/unit/modules/users/test_tutorial_notebook_creation.py +511 -297
- cognee/tests/unit/shared/test_usage_logger.py +241 -0
- cognee/tests/unit/users/permissions/test_has_user_management_permission.py +46 -0
- {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/METADATA +22 -17
- {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/RECORD +258 -157
- cognee/api/.env.example +0 -5
- cognee/modules/retrieval/base_graph_retriever.py +0 -24
- cognee/modules/search/methods/get_search_type_tools.py +0 -223
- cognee/modules/search/methods/no_access_control_search.py +0 -62
- cognee/modules/search/utils/prepare_search_result.py +0 -63
- cognee/tests/test_feedback_enrichment.py +0 -174
- cognee/tests/unit/modules/retrieval/structured_output_test.py +0 -204
- {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/WHEEL +0 -0
- {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/entry_points.txt +0 -0
- {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/licenses/LICENSE +0 -0
- {cognee-0.5.1.dist-info → cognee-0.5.2.dist-info}/licenses/NOTICE.md +0 -0
|
@@ -1,201 +1,155 @@
|
|
|
1
|
-
import os
|
|
2
1
|
import pytest
|
|
3
|
-
import
|
|
4
|
-
from
|
|
5
|
-
|
|
6
|
-
from cognee.low_level import setup
|
|
7
|
-
from cognee.tasks.storage import add_data_points
|
|
8
|
-
from cognee.infrastructure.databases.vector import get_vector_engine
|
|
9
|
-
from cognee.modules.chunking.models import DocumentChunk
|
|
10
|
-
from cognee.modules.data.processing.document_types import TextDocument
|
|
11
|
-
from cognee.modules.retrieval.exceptions.exceptions import NoDataError
|
|
2
|
+
from types import SimpleNamespace
|
|
3
|
+
from unittest.mock import AsyncMock, patch, MagicMock
|
|
4
|
+
|
|
12
5
|
from cognee.modules.retrieval.chunks_retriever import ChunksRetriever
|
|
13
|
-
from cognee.
|
|
14
|
-
from cognee.
|
|
15
|
-
from cognee.modules.engine.models import Entity
|
|
6
|
+
from cognee.modules.retrieval.exceptions.exceptions import NoDataError
|
|
7
|
+
from cognee.infrastructure.databases.vector.exceptions import CollectionNotFoundError
|
|
16
8
|
|
|
17
9
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
contains: List[Entity] = None
|
|
10
|
+
@pytest.fixture
|
|
11
|
+
def mock_vector_engine():
|
|
12
|
+
"""Create a mock vector engine."""
|
|
13
|
+
engine = AsyncMock()
|
|
14
|
+
engine.search = AsyncMock()
|
|
15
|
+
return engine
|
|
25
16
|
|
|
26
|
-
metadata: dict = {"index_fields": ["text"]}
|
|
27
17
|
|
|
18
|
+
@pytest.mark.asyncio
|
|
19
|
+
async def test_get_context_success(mock_vector_engine):
|
|
20
|
+
"""Test successful retrieval of chunk context."""
|
|
21
|
+
mock_result1 = MagicMock()
|
|
22
|
+
mock_result1.payload = {"text": "Steve Rodger", "chunk_index": 0}
|
|
23
|
+
mock_result2 = MagicMock()
|
|
24
|
+
mock_result2.payload = {"text": "Mike Broski", "chunk_index": 1}
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
@pytest.mark.asyncio
|
|
31
|
-
async def test_chunk_context_simple(self):
|
|
32
|
-
system_directory_path = os.path.join(
|
|
33
|
-
pathlib.Path(__file__).parent, ".cognee_system/test_chunk_context_simple"
|
|
34
|
-
)
|
|
35
|
-
cognee.config.system_root_directory(system_directory_path)
|
|
36
|
-
data_directory_path = os.path.join(
|
|
37
|
-
pathlib.Path(__file__).parent, ".data_storage/test_chunk_context_simple"
|
|
38
|
-
)
|
|
39
|
-
cognee.config.data_root_directory(data_directory_path)
|
|
26
|
+
mock_vector_engine.search.return_value = [mock_result1, mock_result2]
|
|
40
27
|
|
|
41
|
-
|
|
42
|
-
await cognee.prune.prune_system(metadata=True)
|
|
43
|
-
await setup()
|
|
28
|
+
retriever = ChunksRetriever(top_k=5)
|
|
44
29
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
)
|
|
30
|
+
with patch(
|
|
31
|
+
"cognee.modules.retrieval.chunks_retriever.get_vector_engine",
|
|
32
|
+
return_value=mock_vector_engine,
|
|
33
|
+
):
|
|
34
|
+
objects = await retriever.get_retrieved_objects("test query")
|
|
51
35
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
contains=[],
|
|
59
|
-
)
|
|
60
|
-
chunk2 = DocumentChunk(
|
|
61
|
-
text="Mike Broski",
|
|
62
|
-
chunk_size=2,
|
|
63
|
-
chunk_index=1,
|
|
64
|
-
cut_type="sentence_end",
|
|
65
|
-
is_part_of=document,
|
|
66
|
-
contains=[],
|
|
67
|
-
)
|
|
68
|
-
chunk3 = DocumentChunk(
|
|
69
|
-
text="Christina Mayer",
|
|
70
|
-
chunk_size=2,
|
|
71
|
-
chunk_index=2,
|
|
72
|
-
cut_type="sentence_end",
|
|
73
|
-
is_part_of=document,
|
|
74
|
-
contains=[],
|
|
75
|
-
)
|
|
36
|
+
assert len(objects) == 2
|
|
37
|
+
assert objects[0].payload["text"] == "Steve Rodger"
|
|
38
|
+
assert objects[1].payload["text"] == "Mike Broski"
|
|
39
|
+
mock_vector_engine.search.assert_awaited_once_with(
|
|
40
|
+
"DocumentChunk_text", "test query", limit=5, include_payload=True
|
|
41
|
+
)
|
|
76
42
|
|
|
77
|
-
entities = [chunk1, chunk2, chunk3]
|
|
78
43
|
|
|
79
|
-
|
|
44
|
+
@pytest.mark.asyncio
|
|
45
|
+
async def test_get_context_collection_not_found_error(mock_vector_engine):
|
|
46
|
+
"""Test that CollectionNotFoundError is converted to NoDataError."""
|
|
47
|
+
mock_vector_engine.search.side_effect = CollectionNotFoundError("Collection not found")
|
|
80
48
|
|
|
81
|
-
|
|
49
|
+
retriever = ChunksRetriever()
|
|
82
50
|
|
|
83
|
-
|
|
51
|
+
with patch(
|
|
52
|
+
"cognee.modules.retrieval.chunks_retriever.get_vector_engine",
|
|
53
|
+
return_value=mock_vector_engine,
|
|
54
|
+
):
|
|
55
|
+
with pytest.raises(NoDataError, match="No data found"):
|
|
56
|
+
await retriever.get_retrieved_objects("test query")
|
|
84
57
|
|
|
85
|
-
assert context[0]["text"] == "Mike Broski", "Failed to get Mike Broski"
|
|
86
58
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
)
|
|
92
|
-
cognee.config.system_root_directory(system_directory_path)
|
|
93
|
-
data_directory_path = os.path.join(
|
|
94
|
-
pathlib.Path(__file__).parent, ".data_storage/test_chunk_context_complex"
|
|
95
|
-
)
|
|
96
|
-
cognee.config.data_root_directory(data_directory_path)
|
|
59
|
+
@pytest.mark.asyncio
|
|
60
|
+
async def test_get_context_empty_results(mock_vector_engine):
|
|
61
|
+
"""Test that empty list is returned when no chunks are found."""
|
|
62
|
+
mock_vector_engine.search.return_value = []
|
|
97
63
|
|
|
98
|
-
|
|
99
|
-
await cognee.prune.prune_system(metadata=True)
|
|
100
|
-
await setup()
|
|
64
|
+
retriever = ChunksRetriever()
|
|
101
65
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
)
|
|
66
|
+
with patch(
|
|
67
|
+
"cognee.modules.retrieval.chunks_retriever.get_vector_engine",
|
|
68
|
+
return_value=mock_vector_engine,
|
|
69
|
+
):
|
|
70
|
+
objects = await retriever.get_retrieved_objects("test query")
|
|
108
71
|
|
|
109
|
-
|
|
110
|
-
name="Car List",
|
|
111
|
-
raw_data_location="somewhere",
|
|
112
|
-
external_metadata="",
|
|
113
|
-
mime_type="text/plain",
|
|
114
|
-
)
|
|
72
|
+
assert objects == []
|
|
115
73
|
|
|
116
|
-
chunk1 = DocumentChunk(
|
|
117
|
-
text="Steve Rodger",
|
|
118
|
-
chunk_size=2,
|
|
119
|
-
chunk_index=0,
|
|
120
|
-
cut_type="sentence_end",
|
|
121
|
-
is_part_of=document1,
|
|
122
|
-
contains=[],
|
|
123
|
-
)
|
|
124
|
-
chunk2 = DocumentChunk(
|
|
125
|
-
text="Mike Broski",
|
|
126
|
-
chunk_size=2,
|
|
127
|
-
chunk_index=1,
|
|
128
|
-
cut_type="sentence_end",
|
|
129
|
-
is_part_of=document1,
|
|
130
|
-
contains=[],
|
|
131
|
-
)
|
|
132
|
-
chunk3 = DocumentChunk(
|
|
133
|
-
text="Christina Mayer",
|
|
134
|
-
chunk_size=2,
|
|
135
|
-
chunk_index=2,
|
|
136
|
-
cut_type="sentence_end",
|
|
137
|
-
is_part_of=document1,
|
|
138
|
-
contains=[],
|
|
139
|
-
)
|
|
140
74
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
contains=[],
|
|
148
|
-
)
|
|
149
|
-
chunk5 = DocumentChunk(
|
|
150
|
-
text="Hyundai",
|
|
151
|
-
chunk_size=2,
|
|
152
|
-
chunk_index=1,
|
|
153
|
-
cut_type="sentence_end",
|
|
154
|
-
is_part_of=document2,
|
|
155
|
-
contains=[],
|
|
156
|
-
)
|
|
157
|
-
chunk6 = DocumentChunk(
|
|
158
|
-
text="Chrysler",
|
|
159
|
-
chunk_size=2,
|
|
160
|
-
chunk_index=2,
|
|
161
|
-
cut_type="sentence_end",
|
|
162
|
-
is_part_of=document2,
|
|
163
|
-
contains=[],
|
|
164
|
-
)
|
|
75
|
+
@pytest.mark.asyncio
|
|
76
|
+
async def test_get_context_top_k_limit(mock_vector_engine):
|
|
77
|
+
"""Test that top_k parameter limits the number of results."""
|
|
78
|
+
mock_results = [MagicMock() for _ in range(3)]
|
|
79
|
+
for i, result in enumerate(mock_results):
|
|
80
|
+
result.payload = {"text": f"Chunk {i}"}
|
|
165
81
|
|
|
166
|
-
|
|
82
|
+
mock_vector_engine.search.return_value = mock_results
|
|
167
83
|
|
|
168
|
-
|
|
84
|
+
retriever = ChunksRetriever(top_k=3)
|
|
169
85
|
|
|
170
|
-
|
|
86
|
+
with patch(
|
|
87
|
+
"cognee.modules.retrieval.chunks_retriever.get_vector_engine",
|
|
88
|
+
return_value=mock_vector_engine,
|
|
89
|
+
):
|
|
90
|
+
objects = await retriever.get_retrieved_objects("test query")
|
|
171
91
|
|
|
172
|
-
|
|
92
|
+
assert len(objects) == 3
|
|
93
|
+
mock_vector_engine.search.assert_awaited_once_with(
|
|
94
|
+
"DocumentChunk_text", "test query", limit=3, include_payload=True
|
|
95
|
+
)
|
|
173
96
|
|
|
174
|
-
assert context[0]["text"] == "Christina Mayer", "Failed to get Christina Mayer"
|
|
175
97
|
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
98
|
+
@pytest.mark.asyncio
|
|
99
|
+
async def test_get_context(mock_vector_engine):
|
|
100
|
+
"""Test get_completion returns provided context."""
|
|
101
|
+
retriever = ChunksRetriever()
|
|
102
|
+
|
|
103
|
+
retrieved_objects = [
|
|
104
|
+
{"payload": {"text": "Steve Rodger"}},
|
|
105
|
+
{"payload": {"text": "Mike Broski"}},
|
|
106
|
+
]
|
|
107
|
+
# Wrap the outer dictionary so payload is an attribute
|
|
108
|
+
mock_objects = [SimpleNamespace(**obj) for obj in retrieved_objects]
|
|
109
|
+
|
|
110
|
+
context = await retriever.get_context_from_objects("test query", retrieved_objects=mock_objects)
|
|
111
|
+
|
|
112
|
+
assert context == "Steve Rodger\nMike Broski"
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
@pytest.mark.asyncio
|
|
116
|
+
async def test_init_defaults():
|
|
117
|
+
"""Test ChunksRetriever initialization with defaults."""
|
|
118
|
+
retriever = ChunksRetriever()
|
|
119
|
+
|
|
120
|
+
assert retriever.top_k == 5
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
@pytest.mark.asyncio
|
|
124
|
+
async def test_init_custom_top_k():
|
|
125
|
+
"""Test ChunksRetriever initialization with custom top_k."""
|
|
126
|
+
retriever = ChunksRetriever(top_k=10)
|
|
127
|
+
|
|
128
|
+
assert retriever.top_k == 10
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
@pytest.mark.asyncio
|
|
132
|
+
async def test_init_none_top_k():
|
|
133
|
+
"""Test ChunksRetriever initialization with None top_k."""
|
|
134
|
+
retriever = ChunksRetriever(top_k=None)
|
|
135
|
+
|
|
136
|
+
assert retriever.top_k is None
|
|
186
137
|
|
|
187
|
-
await cognee.prune.prune_data()
|
|
188
|
-
await cognee.prune.prune_system(metadata=True)
|
|
189
138
|
|
|
190
|
-
|
|
139
|
+
@pytest.mark.asyncio
|
|
140
|
+
async def test_get_context_empty_payload(mock_vector_engine):
|
|
141
|
+
"""Test get_context handles empty payload."""
|
|
142
|
+
mock_vector_engine.search.return_value = []
|
|
191
143
|
|
|
192
|
-
|
|
193
|
-
await retriever.get_context("Christina Mayer")
|
|
144
|
+
retriever = ChunksRetriever()
|
|
194
145
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
146
|
+
with patch(
|
|
147
|
+
"cognee.modules.retrieval.chunks_retriever.get_vector_engine",
|
|
148
|
+
return_value=mock_vector_engine,
|
|
149
|
+
):
|
|
150
|
+
retrieved_objects = await retriever.get_retrieved_objects("test query")
|
|
151
|
+
context = await retriever.get_context_from_objects(
|
|
152
|
+
"test query", retrieved_objects=retrieved_objects
|
|
198
153
|
)
|
|
199
154
|
|
|
200
|
-
|
|
201
|
-
assert len(context) == 0, "Found chunks when none should exist"
|
|
155
|
+
assert context == ""
|
|
@@ -152,3 +152,341 @@ class TestConversationHistoryUtils:
|
|
|
152
152
|
assert result is True
|
|
153
153
|
call_kwargs = mock_cache.add_qa.call_args.kwargs
|
|
154
154
|
assert call_kwargs["session_id"] == "default_session"
|
|
155
|
+
|
|
156
|
+
@pytest.mark.asyncio
|
|
157
|
+
async def test_save_conversation_history_no_user_id(self):
|
|
158
|
+
"""Test save_conversation_history returns False when user_id is None."""
|
|
159
|
+
session_user.set(None)
|
|
160
|
+
|
|
161
|
+
with patch("cognee.modules.retrieval.utils.session_cache.CacheConfig") as MockCacheConfig:
|
|
162
|
+
mock_config = MagicMock()
|
|
163
|
+
mock_config.caching = True
|
|
164
|
+
MockCacheConfig.return_value = mock_config
|
|
165
|
+
|
|
166
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
167
|
+
save_conversation_history,
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
result = await save_conversation_history(
|
|
171
|
+
query="Test question",
|
|
172
|
+
context_summary="Test context",
|
|
173
|
+
answer="Test answer",
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
assert result is False
|
|
177
|
+
|
|
178
|
+
@pytest.mark.asyncio
|
|
179
|
+
async def test_save_conversation_history_caching_disabled(self):
|
|
180
|
+
"""Test save_conversation_history returns False when caching is disabled."""
|
|
181
|
+
user = create_mock_user()
|
|
182
|
+
session_user.set(user)
|
|
183
|
+
|
|
184
|
+
with patch("cognee.modules.retrieval.utils.session_cache.CacheConfig") as MockCacheConfig:
|
|
185
|
+
mock_config = MagicMock()
|
|
186
|
+
mock_config.caching = False
|
|
187
|
+
MockCacheConfig.return_value = mock_config
|
|
188
|
+
|
|
189
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
190
|
+
save_conversation_history,
|
|
191
|
+
)
|
|
192
|
+
|
|
193
|
+
result = await save_conversation_history(
|
|
194
|
+
query="Test question",
|
|
195
|
+
context_summary="Test context",
|
|
196
|
+
answer="Test answer",
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
assert result is False
|
|
200
|
+
|
|
201
|
+
@pytest.mark.asyncio
|
|
202
|
+
async def test_save_conversation_history_cache_engine_none(self):
|
|
203
|
+
"""Test save_conversation_history returns False when cache_engine is None."""
|
|
204
|
+
user = create_mock_user()
|
|
205
|
+
session_user.set(user)
|
|
206
|
+
|
|
207
|
+
cache_module = importlib.import_module(
|
|
208
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
with patch.object(cache_module, "get_cache_engine", return_value=None):
|
|
212
|
+
with patch(
|
|
213
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
214
|
+
) as MockCacheConfig:
|
|
215
|
+
mock_config = MagicMock()
|
|
216
|
+
mock_config.caching = True
|
|
217
|
+
MockCacheConfig.return_value = mock_config
|
|
218
|
+
|
|
219
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
220
|
+
save_conversation_history,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
result = await save_conversation_history(
|
|
224
|
+
query="Test question",
|
|
225
|
+
context_summary="Test context",
|
|
226
|
+
answer="Test answer",
|
|
227
|
+
)
|
|
228
|
+
|
|
229
|
+
assert result is False
|
|
230
|
+
|
|
231
|
+
@pytest.mark.asyncio
|
|
232
|
+
async def test_save_conversation_history_cache_connection_error(self):
|
|
233
|
+
"""Test save_conversation_history handles CacheConnectionError gracefully."""
|
|
234
|
+
user = create_mock_user()
|
|
235
|
+
session_user.set(user)
|
|
236
|
+
|
|
237
|
+
from cognee.infrastructure.databases.exceptions import CacheConnectionError
|
|
238
|
+
|
|
239
|
+
mock_cache = create_mock_cache_engine([])
|
|
240
|
+
mock_cache.add_qa = AsyncMock(side_effect=CacheConnectionError("Connection failed"))
|
|
241
|
+
|
|
242
|
+
cache_module = importlib.import_module(
|
|
243
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
with patch.object(cache_module, "get_cache_engine", return_value=mock_cache):
|
|
247
|
+
with patch(
|
|
248
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
249
|
+
) as MockCacheConfig:
|
|
250
|
+
mock_config = MagicMock()
|
|
251
|
+
mock_config.caching = True
|
|
252
|
+
MockCacheConfig.return_value = mock_config
|
|
253
|
+
|
|
254
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
255
|
+
save_conversation_history,
|
|
256
|
+
)
|
|
257
|
+
|
|
258
|
+
result = await save_conversation_history(
|
|
259
|
+
query="Test question",
|
|
260
|
+
context_summary="Test context",
|
|
261
|
+
answer="Test answer",
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
assert result is False
|
|
265
|
+
|
|
266
|
+
@pytest.mark.asyncio
|
|
267
|
+
async def test_save_conversation_history_generic_exception(self):
|
|
268
|
+
"""Test save_conversation_history handles generic exceptions gracefully."""
|
|
269
|
+
user = create_mock_user()
|
|
270
|
+
session_user.set(user)
|
|
271
|
+
|
|
272
|
+
mock_cache = create_mock_cache_engine([])
|
|
273
|
+
mock_cache.add_qa = AsyncMock(side_effect=ValueError("Unexpected error"))
|
|
274
|
+
|
|
275
|
+
cache_module = importlib.import_module(
|
|
276
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
with patch.object(cache_module, "get_cache_engine", return_value=mock_cache):
|
|
280
|
+
with patch(
|
|
281
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
282
|
+
) as MockCacheConfig:
|
|
283
|
+
mock_config = MagicMock()
|
|
284
|
+
mock_config.caching = True
|
|
285
|
+
MockCacheConfig.return_value = mock_config
|
|
286
|
+
|
|
287
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
288
|
+
save_conversation_history,
|
|
289
|
+
)
|
|
290
|
+
|
|
291
|
+
result = await save_conversation_history(
|
|
292
|
+
query="Test question",
|
|
293
|
+
context_summary="Test context",
|
|
294
|
+
answer="Test answer",
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
assert result is False
|
|
298
|
+
|
|
299
|
+
@pytest.mark.asyncio
|
|
300
|
+
async def test_get_conversation_history_no_user_id(self):
|
|
301
|
+
"""Test get_conversation_history returns empty string when user_id is None."""
|
|
302
|
+
session_user.set(None)
|
|
303
|
+
|
|
304
|
+
with patch("cognee.modules.retrieval.utils.session_cache.CacheConfig") as MockCacheConfig:
|
|
305
|
+
mock_config = MagicMock()
|
|
306
|
+
mock_config.caching = True
|
|
307
|
+
MockCacheConfig.return_value = mock_config
|
|
308
|
+
|
|
309
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
310
|
+
get_conversation_history,
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
result = await get_conversation_history(session_id="test_session")
|
|
314
|
+
|
|
315
|
+
assert result == ""
|
|
316
|
+
|
|
317
|
+
@pytest.mark.asyncio
|
|
318
|
+
async def test_get_conversation_history_caching_disabled(self):
|
|
319
|
+
"""Test get_conversation_history returns empty string when caching is disabled."""
|
|
320
|
+
user = create_mock_user()
|
|
321
|
+
session_user.set(user)
|
|
322
|
+
|
|
323
|
+
with patch("cognee.modules.retrieval.utils.session_cache.CacheConfig") as MockCacheConfig:
|
|
324
|
+
mock_config = MagicMock()
|
|
325
|
+
mock_config.caching = False
|
|
326
|
+
MockCacheConfig.return_value = mock_config
|
|
327
|
+
|
|
328
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
329
|
+
get_conversation_history,
|
|
330
|
+
)
|
|
331
|
+
|
|
332
|
+
result = await get_conversation_history(session_id="test_session")
|
|
333
|
+
|
|
334
|
+
assert result == ""
|
|
335
|
+
|
|
336
|
+
@pytest.mark.asyncio
|
|
337
|
+
async def test_get_conversation_history_default_session(self):
|
|
338
|
+
"""Test get_conversation_history uses 'default_session' when session_id is None."""
|
|
339
|
+
user = create_mock_user()
|
|
340
|
+
session_user.set(user)
|
|
341
|
+
|
|
342
|
+
mock_cache = create_mock_cache_engine([])
|
|
343
|
+
|
|
344
|
+
cache_module = importlib.import_module(
|
|
345
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
with patch.object(cache_module, "get_cache_engine", return_value=mock_cache):
|
|
349
|
+
with patch(
|
|
350
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
351
|
+
) as MockCacheConfig:
|
|
352
|
+
mock_config = MagicMock()
|
|
353
|
+
mock_config.caching = True
|
|
354
|
+
MockCacheConfig.return_value = mock_config
|
|
355
|
+
|
|
356
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
357
|
+
get_conversation_history,
|
|
358
|
+
)
|
|
359
|
+
|
|
360
|
+
await get_conversation_history(session_id=None)
|
|
361
|
+
|
|
362
|
+
mock_cache.get_latest_qa.assert_called_once_with(str(user.id), "default_session")
|
|
363
|
+
|
|
364
|
+
@pytest.mark.asyncio
|
|
365
|
+
async def test_get_conversation_history_cache_engine_none(self):
|
|
366
|
+
"""Test get_conversation_history returns empty string when cache_engine is None."""
|
|
367
|
+
user = create_mock_user()
|
|
368
|
+
session_user.set(user)
|
|
369
|
+
|
|
370
|
+
cache_module = importlib.import_module(
|
|
371
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
with patch.object(cache_module, "get_cache_engine", return_value=None):
|
|
375
|
+
with patch(
|
|
376
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
377
|
+
) as MockCacheConfig:
|
|
378
|
+
mock_config = MagicMock()
|
|
379
|
+
mock_config.caching = True
|
|
380
|
+
MockCacheConfig.return_value = mock_config
|
|
381
|
+
|
|
382
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
383
|
+
get_conversation_history,
|
|
384
|
+
)
|
|
385
|
+
|
|
386
|
+
result = await get_conversation_history(session_id="test_session")
|
|
387
|
+
|
|
388
|
+
assert result == ""
|
|
389
|
+
|
|
390
|
+
@pytest.mark.asyncio
|
|
391
|
+
async def test_get_conversation_history_cache_connection_error(self):
|
|
392
|
+
"""Test get_conversation_history handles CacheConnectionError gracefully."""
|
|
393
|
+
user = create_mock_user()
|
|
394
|
+
session_user.set(user)
|
|
395
|
+
|
|
396
|
+
from cognee.infrastructure.databases.exceptions import CacheConnectionError
|
|
397
|
+
|
|
398
|
+
mock_cache = create_mock_cache_engine([])
|
|
399
|
+
mock_cache.get_latest_qa = AsyncMock(side_effect=CacheConnectionError("Connection failed"))
|
|
400
|
+
|
|
401
|
+
cache_module = importlib.import_module(
|
|
402
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
403
|
+
)
|
|
404
|
+
|
|
405
|
+
with patch.object(cache_module, "get_cache_engine", return_value=mock_cache):
|
|
406
|
+
with patch(
|
|
407
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
408
|
+
) as MockCacheConfig:
|
|
409
|
+
mock_config = MagicMock()
|
|
410
|
+
mock_config.caching = True
|
|
411
|
+
MockCacheConfig.return_value = mock_config
|
|
412
|
+
|
|
413
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
414
|
+
get_conversation_history,
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
result = await get_conversation_history(session_id="test_session")
|
|
418
|
+
|
|
419
|
+
assert result == ""
|
|
420
|
+
|
|
421
|
+
@pytest.mark.asyncio
|
|
422
|
+
async def test_get_conversation_history_generic_exception(self):
|
|
423
|
+
"""Test get_conversation_history handles generic exceptions gracefully."""
|
|
424
|
+
user = create_mock_user()
|
|
425
|
+
session_user.set(user)
|
|
426
|
+
|
|
427
|
+
mock_cache = create_mock_cache_engine([])
|
|
428
|
+
mock_cache.get_latest_qa = AsyncMock(side_effect=ValueError("Unexpected error"))
|
|
429
|
+
|
|
430
|
+
cache_module = importlib.import_module(
|
|
431
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
432
|
+
)
|
|
433
|
+
|
|
434
|
+
with patch.object(cache_module, "get_cache_engine", return_value=mock_cache):
|
|
435
|
+
with patch(
|
|
436
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
437
|
+
) as MockCacheConfig:
|
|
438
|
+
mock_config = MagicMock()
|
|
439
|
+
mock_config.caching = True
|
|
440
|
+
MockCacheConfig.return_value = mock_config
|
|
441
|
+
|
|
442
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
443
|
+
get_conversation_history,
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
result = await get_conversation_history(session_id="test_session")
|
|
447
|
+
|
|
448
|
+
assert result == ""
|
|
449
|
+
|
|
450
|
+
@pytest.mark.asyncio
|
|
451
|
+
async def test_get_conversation_history_missing_keys(self):
|
|
452
|
+
"""Test get_conversation_history handles missing keys in history entries."""
|
|
453
|
+
user = create_mock_user()
|
|
454
|
+
session_user.set(user)
|
|
455
|
+
|
|
456
|
+
mock_history = [
|
|
457
|
+
{
|
|
458
|
+
"time": "2024-01-15 10:30:45",
|
|
459
|
+
"question": "What is AI?",
|
|
460
|
+
},
|
|
461
|
+
{
|
|
462
|
+
"context": "AI is artificial intelligence",
|
|
463
|
+
"answer": "AI stands for Artificial Intelligence",
|
|
464
|
+
},
|
|
465
|
+
{},
|
|
466
|
+
]
|
|
467
|
+
mock_cache = create_mock_cache_engine(mock_history)
|
|
468
|
+
|
|
469
|
+
cache_module = importlib.import_module(
|
|
470
|
+
"cognee.infrastructure.databases.cache.get_cache_engine"
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
with patch.object(cache_module, "get_cache_engine", return_value=mock_cache):
|
|
474
|
+
with patch(
|
|
475
|
+
"cognee.modules.retrieval.utils.session_cache.CacheConfig"
|
|
476
|
+
) as MockCacheConfig:
|
|
477
|
+
mock_config = MagicMock()
|
|
478
|
+
mock_config.caching = True
|
|
479
|
+
MockCacheConfig.return_value = mock_config
|
|
480
|
+
|
|
481
|
+
from cognee.modules.retrieval.utils.session_cache import (
|
|
482
|
+
get_conversation_history,
|
|
483
|
+
)
|
|
484
|
+
|
|
485
|
+
result = await get_conversation_history(session_id="test_session")
|
|
486
|
+
|
|
487
|
+
assert "Previous conversation:" in result
|
|
488
|
+
assert "[2024-01-15 10:30:45]" in result
|
|
489
|
+
assert "QUESTION: What is AI?" in result
|
|
490
|
+
assert "Unknown time" in result
|
|
491
|
+
assert "CONTEXT: AI is artificial intelligence" in result
|
|
492
|
+
assert "ANSWER: AI stands for Artificial Intelligence" in result
|