Skip to content

Commit 7253e67

Browse files
committed
chore: tidy up
1 parent 8932620 commit 7253e67

3 files changed

Lines changed: 117 additions & 0 deletions

File tree

aperag/index/manager.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ async def rebuild_document_indexes(
120120
document_id: Document ID
121121
index_types: List of index types to rebuild
122122
"""
123+
if len(set(index_types)) != len(index_types):
124+
raise Exception("Duplicate index types are not allowed")
125+
123126
for index_type in index_types:
124127
stmt = select(DocumentIndex).where(
125128
and_(DocumentIndex.document_id == document_id, DocumentIndex.index_type == index_type)

aperag/service/document_service.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,9 @@ async def rebuild_document_indexes(
417417
Returns:
418418
dict: Success response
419419
"""
420+
if len(set(index_types)) != len(index_types):
421+
raise invalid_param("index_types", "duplicate index types are not allowed")
422+
420423
logger.info(f"Rebuilding indexes for document {document_id} with types: {index_types}")
421424

422425
# Convert index types to enum values outside transaction

tests/e2e_test/test_document.py

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,114 @@ def test_upload_duplicate_then_delete_and_reupload(client, collection):
129129
assert "items" in data3
130130
assert len(data3["items"]) == 1
131131
assert data3["items"][0]["name"] == filename
132+
133+
134+
# Document index rebuild tests
135+
def test_rebuild_single_index_type(client, document, collection):
136+
"""Test rebuilding a single index type (vector)"""
137+
doc_id = document["id"]
138+
collection_id = collection["id"]
139+
140+
# Test rebuilding vector index
141+
rebuild_request = {"index_types": ["vector"]}
142+
response = client.post(
143+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
144+
json=rebuild_request
145+
)
146+
assert response.status_code == HTTPStatus.OK, response.text
147+
data = response.json()
148+
assert data["code"] == "200"
149+
assert "vector" in data["message"]
150+
151+
152+
def test_rebuild_all_index_types(client, document, collection):
153+
"""Test rebuilding all supported index types"""
154+
doc_id = document["id"]
155+
collection_id = collection["id"]
156+
157+
# Test rebuilding all index types
158+
rebuild_request = {"index_types": ["vector", "fulltext", "graph"]}
159+
response = client.post(
160+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
161+
json=rebuild_request
162+
)
163+
assert response.status_code == HTTPStatus.OK, response.text
164+
data = response.json()
165+
assert data["code"] == "200"
166+
assert "vector" in data["message"]
167+
assert "fulltext" in data["message"]
168+
assert "graph" in data["message"]
169+
170+
171+
def test_rebuild_index_invalid_index_type(client, document, collection):
172+
"""Test rebuilding with invalid index type"""
173+
doc_id = document["id"]
174+
collection_id = collection["id"]
175+
176+
# Test with invalid index type
177+
rebuild_request = {"index_types": ["invalid_type"]}
178+
response = client.post(
179+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
180+
json=rebuild_request
181+
)
182+
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
183+
184+
185+
def test_rebuild_index_empty_index_types(client, document, collection):
186+
"""Test rebuilding with empty index types array"""
187+
doc_id = document["id"]
188+
collection_id = collection["id"]
189+
190+
# Test with empty index types
191+
rebuild_request = {"index_types": []}
192+
response = client.post(
193+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
194+
json=rebuild_request
195+
)
196+
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY, response.text
197+
198+
199+
@pytest.mark.parametrize("index_type", ["vector", "fulltext", "graph"])
200+
def test_rebuild_individual_index_types(client, document, collection, index_type):
201+
"""Test rebuilding each individual index type"""
202+
doc_id = document["id"]
203+
collection_id = collection["id"]
204+
205+
rebuild_request = {"index_types": [index_type]}
206+
response = client.post(
207+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
208+
json=rebuild_request
209+
)
210+
assert response.status_code == HTTPStatus.OK, response.text
211+
data = response.json()
212+
assert data["code"] == "200"
213+
assert index_type in data["message"]
214+
215+
216+
def test_rebuild_index_duplicate_types(client, document, collection):
217+
"""Test rebuilding with duplicate index types in request"""
218+
doc_id = document["id"]
219+
collection_id = collection["id"]
220+
221+
# Test with duplicate index types
222+
rebuild_request = {"index_types": ["vector", "vector", "fulltext"]}
223+
response = client.post(
224+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
225+
json=rebuild_request
226+
)
227+
# Should fail
228+
assert response.status_code == HTTPStatus.BAD_REQUEST, response.text
229+
230+
231+
def test_rebuild_index_case_sensitivity(client, document, collection):
232+
"""Test rebuilding with different case index types"""
233+
doc_id = document["id"]
234+
collection_id = collection["id"]
235+
236+
# Test with uppercase index type (should fail)
237+
rebuild_request = {"index_types": ["VECTOR"]}
238+
response = client.post(
239+
f"/api/v1/collections/{collection_id}/documents/{doc_id}/rebuild_indexes",
240+
json=rebuild_request
241+
)
242+
assert response.status_code == HTTPStatus.UNPROCESSABLE_ENTITY, response.text

0 commit comments

Comments
 (0)