-
Notifications
You must be signed in to change notification settings - Fork 0
VectorDB Service Refactor & Test Modernization with pinecone API integration #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are getting rid of this file completely. its testing pinecone client which we dont need |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,97 +1,46 @@ | ||
|
||
# Patch config.settings and env vars before any other imports | ||
import sys | ||
import types | ||
import os | ||
from pydantic import SecretStr | ||
|
||
# Set required env vars and patch config.settings before any other imports | ||
os.environ["INTERNAL_API_KEY"] = "test-key" | ||
|
||
class DummySettings: | ||
pinecone_api_key = SecretStr("test-key") | ||
pinecone_cloud = "aws" | ||
pinecone_region = "us-east-1" | ||
pinecone_index_name = "authormaton-core" | ||
embedding_model = "test-model" | ||
embedding_dimension = 16 | ||
embed_batch_size = 64 | ||
max_upload_mb = 25 | ||
|
||
dummy_config = types.ModuleType("config.settings") | ||
dummy_config.settings = DummySettings() | ||
sys.modules["config.settings"] = dummy_config | ||
|
||
|
||
# Patch Pinecone client for tests to avoid real API calls | ||
from unittest.mock import MagicMock | ||
class MockIndex: | ||
def __init__(self, dimension): | ||
self.dimension = dimension | ||
self.upserted = [] | ||
def upsert(self, items, namespace=None): | ||
self.upserted.extend(items) | ||
def query(self, vector, top_k=8, namespace=None, filter=None): | ||
return {'matches': [{'id': 'test', 'score': 0.99}]} | ||
|
||
mock_pc = MagicMock() | ||
mock_pc.list_indexes.return_value = [] | ||
mock_pc.create_index.return_value = None | ||
mock_pc.Index.side_effect = lambda name: MockIndex(16) | ||
mock_pc.describe_index.return_value = {"dimension": 16} | ||
|
||
mock_pinecone = MagicMock() | ||
mock_pinecone.Pinecone = MagicMock(return_value=mock_pc) | ||
mock_pinecone.ServerlessSpec = MagicMock() | ||
sys.modules["pinecone"] = mock_pinecone | ||
|
||
# Now import everything else | ||
import pytest | ||
from services.vector_db_service import VectorDBService | ||
from services.vector_db_service import VectorDBClient | ||
|
||
class MockIndex: | ||
class DummyIndex: | ||
def __init__(self, dimension): | ||
self.dimension = dimension | ||
self.upserted = [] | ||
def upsert(self, items, namespace=None): | ||
self.upserted.extend(items) | ||
def query(self, vector, top_k=8, namespace=None, filter=None): | ||
return {'matches': [{'id': 'test', 'score': 0.99}]} | ||
|
||
def test_ensure_index_idempotent(monkeypatch): | ||
svc = VectorDBService() | ||
# Simulate: first call creates the index; second call sees it and skips creation. | ||
mock_pc.create_index.reset_mock() | ||
import types | ||
mock_pc.list_indexes.side_effect = [ | ||
[], | ||
[types.SimpleNamespace(name=dummy_config.settings.pinecone_index_name)], | ||
] | ||
|
||
svc.ensure_index(svc.embedding_dimension) | ||
svc.ensure_index(svc.embedding_dimension) | ||
# Should only ever create the index once | ||
assert mock_pc.create_index.call_count == 1 | ||
assert svc.index.dimension == svc.embedding_dimension | ||
|
||
def test_upsert_dimension_guard(monkeypatch): | ||
svc = VectorDBService() | ||
monkeypatch.setattr(svc, 'index', MockIndex(svc.embedding_dimension)) | ||
ids = ['a', 'b'] | ||
vectors = [[0.0]*svc.embedding_dimension, [0.0]*svc.embedding_dimension] | ||
metadata = [{}, {}] | ||
count = svc.upsert(namespace='proj', ids=ids, vectors=vectors, metadata=metadata) | ||
assert count == 2 | ||
def upsert(self, vectors=None): | ||
if vectors: | ||
self.upserted.extend(vectors) | ||
def query(self, vector, top_k=5): | ||
return {'matches': [{'id': 'id1', 'score': 0.99}]} | ||
|
||
@pytest.fixture | ||
def vdb(monkeypatch): | ||
svc = VectorDBClient(dimension=8, index_name="test-index") | ||
monkeypatch.setattr(svc, 'index', DummyIndex(svc.dimension)) | ||
return svc | ||
|
||
def test_create_index(monkeypatch): | ||
svc = VectorDBClient(dimension=8, index_name="test-index") | ||
monkeypatch.setattr(svc.pc, 'list_indexes', lambda: []) | ||
monkeypatch.setattr(svc.pc, 'create_index', lambda **kwargs: None) | ||
monkeypatch.setattr(svc.pc, 'Index', lambda name: DummyIndex(8)) | ||
svc.create_index() | ||
assert svc.index.dimension == 8 | ||
|
||
def test_upsert_vectors(vdb): | ||
ids = ["id1", "id2"] | ||
vectors = [[0.0]*8, [1.0]*8] | ||
vdb.upsert_vectors(vectors, ids) | ||
assert len(vdb.index.upserted) == 2 | ||
# Wrong dimension | ||
with pytest.raises(ValueError): | ||
svc.upsert(namespace='proj', ids=ids, vectors=[[0.0]*10, [0.0]*10], metadata=metadata) | ||
vdb.upsert_vectors([[0.0]*5, [1.0]*5], ids) | ||
|
||
def test_query_dimension_guard(monkeypatch): | ||
svc = VectorDBService() | ||
monkeypatch.setattr(svc, 'index', MockIndex(svc.embedding_dimension)) | ||
vector = [0.0]*svc.embedding_dimension | ||
matches = svc.query(namespace='proj', vector=vector) | ||
assert matches[0]['id'] == 'test' | ||
def test_query(vdb): | ||
vector = [0.0]*8 | ||
result = vdb.query(vector) | ||
assert result['matches'][0]['id'] == 'id1' | ||
# Wrong dimension | ||
with pytest.raises(ValueError): | ||
svc.query(namespace='proj', vector=[0.0]*10) | ||
vdb.query([0.0]*5) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.