Logo

Testing

In your tests and development environment we suggest hitting production turbopuffer for the best end to end testing. Since creating a namespace in turbopuffer is virtually free, you can create a namespace for each test with a random name, and simply delete it after the test. We recommend each developer has their own namespace for their dev namespaces.

import pytest
import os
import string
import random
import turbopuffer as tpuf
tpuf.api_key = os.environ["TURBOPUFFER_API_KEY"]

# Create a namespace for each test, and always delete it afterwards
@pytest.fixture
def tpuf_ns():
    random_suffix = ''.join(random.choices(string.ascii_letters + string.digits, k=32))
    ns_name = f"test-{random_suffix}"
    ns = tpuf.Namespace(ns_name)
    try:
        yield ns
    finally:
        try:
            ns.delete_all()
        except tpuf.error.APIError as e:
            if e.status_code != 404:  # If the error is not due to the namespace not existing
                raise


def test_query(tpuf_ns):
    tpuf_ns.upsert(ids=[1, 2], vectors=[[1, 1], [2, 2]])
    res = tpuf_ns.query(vector=[1.1, 1.1], distance_metric="euclidean_squared")
    assert res[0].id == 1

if __name__ == "__main__":
    pytest.main()
© 2024 turbopuffer Inc.
Privacy PolicyTerms of service