Testing Index Operations

1. Setting Up Test Environment

ApproachDetail
Dedicated test indexServerless: cheap, on-demand
Ephemeral per CI runCreate + tear down
Shared dev indexUse per-PR namespace

2. Writing Unit Tests

Example: pytest Fixture

import pytest, uuid
from pinecone import Pinecone, ServerlessSpec

@pytest.fixture(scope="session")
def index():
    pc = Pinecone(api_key=os.environ["PINECONE_API_KEY"])
    name = f"test-{uuid.uuid4().hex[:8]}"
    pc.create_index(name=name, dimension=8, metric="cosine",
        spec=ServerlessSpec(cloud="aws", region="us-east-1"))
    yield pc.Index(name)
    pc.delete_index(name)

3. Mocking Pinecone Client

Example: Mock

from unittest.mock import MagicMock

idx = MagicMock()
idx.query.return_value = {"matches": [
    {"id": "a", "score": 0.9, "metadata": {"t": "hi"}},
]}
assert my_search_fn(idx, "q")[0] == "a"

4. Testing Upsert Operations

Example: Upsert Test

def test_upsert(index):
    index.upsert(vectors=[{"id": "t1", "values": [0.1]*8}])
    time.sleep(2)
    assert index.fetch(ids=["t1"])["vectors"]["t1"]

5. Testing Query Operations

Example: Query Test

def test_query(index):
    index.upsert(vectors=[{"id": "q1", "values": [1.0]+[0]*7}])
    time.sleep(2)
    r = index.query(vector=[1.0]+[0]*7, top_k=1)
    assert r["matches"][0]["id"] == "q1"

6. Testing Metadata Filtering

Example: Filter Test

def test_filter(index):
    index.upsert(vectors=[
        {"id": "a", "values": [0.1]*8, "metadata": {"cat": "x"}},
        {"id": "b", "values": [0.1]*8, "metadata": {"cat": "y"}},
    ])
    time.sleep(2)
    r = index.query(vector=[0.1]*8, top_k=5, filter={"cat": "x"})
    assert all(m["id"] == "a" for m in r["matches"])

7. Testing Namespace Operations

Example: Namespace Isolation

def test_namespaces(index):
    index.upsert(vectors=[{"id": "n", "values": [0.5]*8}], namespace="ns1")
    time.sleep(2)
    assert index.fetch(ids=["n"], namespace="ns1")["vectors"]
    assert not index.fetch(ids=["n"], namespace="ns2")["vectors"]

8. Implementing Integration Tests

PatternDetail
End-to-end RAGEmbed → upsert → query → assert relevance
Per-tenant isolationTenant A can't see tenant B data
Schema migrationOld + new client compatibility

9. Running Tests in CI/CD

Example: GitHub Actions

jobs:
  test:
    runs-on: ubuntu-latest
    env:
      PINECONE_API_KEY: ${{ secrets.PINECONE_TEST_KEY }}
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with: { python-version: "3.12" }
      - run: pip install -r requirements.txt
      - run: pytest -v

10. Cleaning Up Test Data

CleanupWhen
Delete test namespaceAfter each test
Delete test indexAfter CI run (fixture teardown)
Nightly purgeSweep stale test-* indexes