Skip to content

Commit 0672cbc

Browse files
committed
🧪 Add test files for knowledgebase
1 parent 172c4ef commit 0672cbc

File tree

8 files changed

+505
-89
lines changed

8 files changed

+505
-89
lines changed

‎test/backend/app/test_vectordatabase_app.py‎

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ async def test_create_new_index_success(vdb_core_mock, auth_data):
152152
# Setup mocks
153153
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
154154
patch("backend.apps.vectordatabase_app.get_current_user_id", return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
155-
patch("backend.apps.vectordatabase_app.ElasticSearchService.create_index") as mock_create:
155+
patch("backend.apps.vectordatabase_app.ElasticSearchService.create_knowledge_base") as mock_create:
156156

157157
expected_response = {"status": "success",
158158
"index_name": auth_data["index_name"]}
@@ -165,7 +165,13 @@ async def test_create_new_index_success(vdb_core_mock, auth_data):
165165
# Verify
166166
assert response.status_code == 200
167167
assert response.json() == expected_response
168+
# vdb_core is constructed inside router; accept ANY for instance
168169
mock_create.assert_called_once()
170+
called_args = mock_create.call_args[0]
171+
assert called_args[0] == auth_data["index_name"]
172+
assert called_args[1] == 768
173+
assert called_args[3] == auth_data["user_id"]
174+
assert called_args[4] == auth_data["tenant_id"]
169175

170176

171177
@pytest.mark.asyncio
@@ -177,7 +183,7 @@ async def test_create_new_index_error(vdb_core_mock, auth_data):
177183
# Setup mocks
178184
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
179185
patch("backend.apps.vectordatabase_app.get_current_user_id", return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
180-
patch("backend.apps.vectordatabase_app.ElasticSearchService.create_index") as mock_create:
186+
patch("backend.apps.vectordatabase_app.ElasticSearchService.create_knowledge_base") as mock_create:
181187

182188
mock_create.side_effect = Exception("Test error")
183189

@@ -702,10 +708,11 @@ async def test_get_index_chunks_success(vdb_core_mock):
702708
Test retrieving index chunks successfully.
703709
Verifies that the endpoint forwards query params and returns the service payload.
704710
"""
711+
index_name = "test_index"
705712
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
713+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value="resolved_index"), \
706714
patch("backend.apps.vectordatabase_app.ElasticSearchService.get_index_chunks") as mock_get_chunks:
707715

708-
index_name = "test_index"
709716
expected_response = {
710717
"status": "success",
711718
"message": "ok",
@@ -724,7 +731,7 @@ async def test_get_index_chunks_success(vdb_core_mock):
724731
assert response.status_code == 200
725732
assert response.json() == expected_response
726733
mock_get_chunks.assert_called_once_with(
727-
index_name=index_name,
734+
index_name="resolved_index",
728735
page=2,
729736
page_size=50,
730737
path_or_url="/foo",
@@ -738,18 +745,19 @@ async def test_get_index_chunks_error(vdb_core_mock):
738745
Test retrieving index chunks with service error.
739746
Ensures the endpoint maps the exception to HTTP 500.
740747
"""
748+
index_name = "test_index"
741749
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
750+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value="resolved_index"), \
742751
patch("backend.apps.vectordatabase_app.ElasticSearchService.get_index_chunks") as mock_get_chunks:
743752

744-
index_name = "test_index"
745753
mock_get_chunks.side_effect = Exception("Chunk failure")
746754

747755
response = client.post(f"/indices/{index_name}/chunks")
748756

749757
assert response.status_code == 500
750758
assert response.json() == {"detail": "Error getting chunks: Chunk failure"}
751759
mock_get_chunks.assert_called_once_with(
752-
index_name=index_name,
760+
index_name="resolved_index",
753761
page=None,
754762
page_size=None,
755763
path_or_url=None,
@@ -765,6 +773,7 @@ async def test_create_chunk_success(vdb_core_mock, auth_data):
765773
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
766774
patch("backend.apps.vectordatabase_app.get_current_user_id",
767775
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
776+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
768777
patch("backend.apps.vectordatabase_app.ElasticSearchService.create_chunk") as mock_create:
769778

770779
expected_response = {"status": "success", "chunk_id": "chunk-1"}
@@ -794,6 +803,7 @@ async def test_create_chunk_error(vdb_core_mock, auth_data):
794803
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
795804
patch("backend.apps.vectordatabase_app.get_current_user_id",
796805
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
806+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
797807
patch("backend.apps.vectordatabase_app.ElasticSearchService.create_chunk") as mock_create:
798808

799809
mock_create.side_effect = Exception("Create failed")
@@ -822,6 +832,7 @@ async def test_update_chunk_success(vdb_core_mock, auth_data):
822832
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
823833
patch("backend.apps.vectordatabase_app.get_current_user_id",
824834
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
835+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
825836
patch("backend.apps.vectordatabase_app.ElasticSearchService.update_chunk") as mock_update:
826837

827838
expected_response = {"status": "success", "chunk_id": "chunk-1"}
@@ -850,6 +861,7 @@ async def test_update_chunk_value_error(vdb_core_mock, auth_data):
850861
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
851862
patch("backend.apps.vectordatabase_app.get_current_user_id",
852863
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
864+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
853865
patch("backend.apps.vectordatabase_app.ElasticSearchService.update_chunk") as mock_update:
854866

855867
mock_update.side_effect = ValueError("Invalid update payload")
@@ -864,7 +876,8 @@ async def test_update_chunk_value_error(vdb_core_mock, auth_data):
864876
headers=auth_data["auth_header"],
865877
)
866878

867-
assert response.status_code == 400
879+
# ValueError is mapped to NOT_FOUND in app layer
880+
assert response.status_code == 404
868881
assert response.json() == {"detail": "Invalid update payload"}
869882
mock_update.assert_called_once()
870883

@@ -877,6 +890,7 @@ async def test_update_chunk_exception(vdb_core_mock, auth_data):
877890
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
878891
patch("backend.apps.vectordatabase_app.get_current_user_id",
879892
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
893+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
880894
patch("backend.apps.vectordatabase_app.ElasticSearchService.update_chunk") as mock_update:
881895

882896
mock_update.side_effect = Exception("Update failed")
@@ -904,6 +918,7 @@ async def test_delete_chunk_success(vdb_core_mock, auth_data):
904918
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
905919
patch("backend.apps.vectordatabase_app.get_current_user_id",
906920
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
921+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
907922
patch("backend.apps.vectordatabase_app.ElasticSearchService.delete_chunk") as mock_delete:
908923

909924
expected_response = {"status": "success", "chunk_id": "chunk-1"}
@@ -927,6 +942,7 @@ async def test_delete_chunk_not_found(vdb_core_mock, auth_data):
927942
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
928943
patch("backend.apps.vectordatabase_app.get_current_user_id",
929944
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
945+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
930946
patch("backend.apps.vectordatabase_app.ElasticSearchService.delete_chunk") as mock_delete:
931947

932948
mock_delete.side_effect = ValueError("Chunk not found")
@@ -949,6 +965,7 @@ async def test_delete_chunk_exception(vdb_core_mock, auth_data):
949965
with patch("backend.apps.vectordatabase_app.get_vector_db_core", return_value=vdb_core_mock), \
950966
patch("backend.apps.vectordatabase_app.get_current_user_id",
951967
return_value=(auth_data["user_id"], auth_data["tenant_id"])), \
968+
patch("backend.apps.vectordatabase_app.get_index_name_by_knowledge_name", return_value=auth_data["index_name"]), \
952969
patch("backend.apps.vectordatabase_app.ElasticSearchService.delete_chunk") as mock_delete:
953970

954971
mock_delete.side_effect = Exception("Delete failed")

0 commit comments

Comments
 (0)