Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
test_search_api.py123 linesDownload Raw Back to api
1"""Tests for the Search API endpoint."""
2
3from typing import Tuple
4from uuid import uuid4
5
6import pytest
7
8from chromadb.api import ClientAPI
9from chromadb.api.models.Collection import Collection
10from chromadb.api.types import Embeddings, ReadLevel
11from chromadb.execution.expression import Knn, Search
12from chromadb.test.conftest import (
13    ClientFactories,
14    is_spann_disabled_mode,
15    skip_reason_spann_disabled,
16)
17
18
19def _create_test_collection(
20    client_factories: ClientFactories,
21) -> Tuple[Collection, ClientAPI]:
22    """Create a test collection with some data."""
23    client = client_factories.create_client_from_system()
24    client.reset()
25
26    collection_name = f"search_api_test_{uuid4().hex}"
27    collection = client.get_or_create_collection(name=collection_name)
28
29    return collection, client
30
31
32@pytest.mark.skipif(is_spann_disabled_mode, reason=skip_reason_spann_disabled)
33def test_search_with_read_level_index_and_wal(
34    client_factories: ClientFactories,
35) -> None:
36    """Test search with ReadLevel.INDEX_AND_WAL (default) returns results."""
37    collection, _ = _create_test_collection(client_factories)
38
39    # Add some data
40    collection.add(
41        ids=["doc1", "doc2", "doc3"],
42        documents=["apple fruit", "banana fruit", "car vehicle"],
43        embeddings=[[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5], [0.9, 0.8, 0.7, 0.6]],
44    )
45
46    # Search with explicit INDEX_AND_WAL (default behavior)
47    search = Search().rank(Knn(query=[0.1, 0.2, 0.3, 0.4], limit=10))
48    results = collection.search(search, read_level=ReadLevel.INDEX_AND_WAL)
49
50    assert results["ids"] is not None
51    assert len(results["ids"]) == 1
52    assert len(results["ids"][0]) > 0
53
54
55@pytest.mark.skipif(is_spann_disabled_mode, reason=skip_reason_spann_disabled)
56def test_search_with_read_level_index_only(
57    client_factories: ClientFactories,
58) -> None:
59    """Test search with ReadLevel.INDEX_ONLY returns results."""
60    collection, _ = _create_test_collection(client_factories)
61
62    # Add some data
63    collection.add(
64        ids=["doc1", "doc2", "doc3"],
65        documents=["apple fruit", "banana fruit", "car vehicle"],
66        embeddings=[[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5], [0.9, 0.8, 0.7, 0.6]],
67    )
68
69    # Search with INDEX_ONLY - this skips the WAL
70    # Note: Results may or may not include recent writes depending on compaction state
71    search = Search().rank(Knn(query=[0.1, 0.2, 0.3, 0.4], limit=10))
72    results = collection.search(search, read_level=ReadLevel.INDEX_ONLY)
73
74    # Just verify the API works and returns a valid response structure
75    assert results["ids"] is not None
76    assert len(results["ids"]) == 1
77    # Results may be empty if data hasn't been compacted yet, which is expected behavior
78
79
80@pytest.mark.skipif(is_spann_disabled_mode, reason=skip_reason_spann_disabled)
81def test_search_with_read_level_index_and_bounded_wal(
82    client_factories: ClientFactories,
83) -> None:
84    """Test search with ReadLevel.INDEX_AND_BOUNDED_WAL returns results."""
85    collection, _ = _create_test_collection(client_factories)
86
87    collection.add(
88        ids=["doc1", "doc2", "doc3"],
89        documents=["apple fruit", "banana fruit", "car vehicle"],
90        embeddings=[[0.1, 0.2, 0.3, 0.4], [0.2, 0.3, 0.4, 0.5], [0.9, 0.8, 0.7, 0.6]],
91    )
92
93    # Search with INDEX_AND_BOUNDED_WAL reads up to a server-configured number of WAL entries
94    search = Search().rank(Knn(query=[0.1, 0.2, 0.3, 0.4], limit=10))
95    results = collection.search(search, read_level=ReadLevel.INDEX_AND_BOUNDED_WAL)
96
97    assert results["ids"] is not None
98    assert len(results["ids"]) == 1
99
100
101@pytest.mark.skipif(is_spann_disabled_mode, reason=skip_reason_spann_disabled)
102def test_search_default_read_level(
103    client_factories: ClientFactories,
104) -> None:
105    """Test search without explicit read_level uses default (INDEX_AND_WAL)."""
106    collection, _ = _create_test_collection(client_factories)
107
108    # Add some data
109    collection.add(
110        ids=["doc1", "doc2"],
111        documents=["hello world", "goodbye world"],
112        embeddings=[[0.1, 0.2, 0.3, 0.4], [0.5, 0.6, 0.7, 0.8]],
113    )
114
115    # Search without specifying read_level (should use default)
116    search = Search().rank(Knn(query=[0.1, 0.2, 0.3, 0.4], limit=10))
117    results = collection.search(search)
118
119    # Should return results since default is INDEX_AND_WAL (full consistency)
120    assert results["ids"] is not None
121    assert len(results["ids"]) == 1
122    assert len(results["ids"][0]) > 0
123 
codekingpro/portable-devtools · Team Ai