function test_collection_creation
Maturity: 43
A diagnostic test function that verifies Chroma DB functionality by creating a test collection, adding a document, querying it, and cleaning up.
File:
/tf/active/vicechatdev/test_chroma_collections.py
Lines:
134 - 183
134 - 183
Complexity:
simple
simple
Purpose
This function serves as a health check and debugging tool for Chroma DB connectivity and operations. It tests the complete lifecycle of a collection: creation, document insertion, querying, and deletion. Useful for verifying that a Chroma DB instance (specifically at 'vice_chroma:8000') is operational and accessible.
Source Code
def test_collection_creation():
"""Test creating a sample collection to verify Chroma DB functionality."""
print(f"\n๐งช TESTING COLLECTION CREATION")
print("-" * 40)
try:
# Try with vice_chroma first
client = chromadb.HttpClient(host='vice_chroma', port=8000)
# Try to create a test collection
test_collection_name = "test_collection_debug"
try:
# Delete if exists
try:
client.delete_collection(test_collection_name)
print(f"๐๏ธ Deleted existing test collection")
except:
pass
# Create new test collection
test_collection = client.create_collection(test_collection_name)
print(f"โ
Successfully created test collection: {test_collection_name}")
# Add a test document
test_collection.add(
documents=["This is a test document for debugging"],
ids=["test_doc_1"],
metadatas=[{"source": "debug_test"}]
)
print(f"โ
Successfully added test document")
# Query the test collection
results = test_collection.query(
query_texts=["test document"],
n_results=1
)
print(f"โ
Successfully queried test collection")
print(f" - Found {len(results['documents'][0])} documents")
# Clean up
client.delete_collection(test_collection_name)
print(f"๐๏ธ Cleaned up test collection")
except Exception as create_error:
print(f"โ Error creating/testing collection: {create_error}")
except Exception as client_error:
print(f"โ Error connecting for collection test: {client_error}")
Return Value
This function does not return any value (implicitly returns None). It outputs diagnostic information to stdout via print statements, indicating success or failure of each operation with emoji-prefixed messages.
Dependencies
chromadb
Required Imports
import chromadb
Usage Example
import chromadb
def test_collection_creation():
"""Test creating a sample collection to verify Chroma DB functionality."""
print(f"\n๐งช TESTING COLLECTION CREATION")
print("-" * 40)
try:
client = chromadb.HttpClient(host='vice_chroma', port=8000)
test_collection_name = "test_collection_debug"
try:
try:
client.delete_collection(test_collection_name)
print(f"๐๏ธ Deleted existing test collection")
except:
pass
test_collection = client.create_collection(test_collection_name)
print(f"โ
Successfully created test collection: {test_collection_name}")
test_collection.add(
documents=["This is a test document for debugging"],
ids=["test_doc_1"],
metadatas=[{"source": "debug_test"}]
)
print(f"โ
Successfully added test document")
results = test_collection.query(
query_texts=["test document"],
n_results=1
)
print(f"โ
Successfully queried test collection")
print(f" - Found {len(results['documents'][0])} documents")
client.delete_collection(test_collection_name)
print(f"๐๏ธ Cleaned up test collection")
except Exception as create_error:
print(f"โ Error creating/testing collection: {create_error}")
except Exception as client_error:
print(f"โ Error connecting for collection test: {client_error}")
# Run the test
test_collection_creation()
Best Practices
- This function is designed for testing/debugging purposes only and should not be used in production code
- The function uses hardcoded host 'vice_chroma' and port 8000 - modify these values if your Chroma DB instance is at a different location
- All exceptions are caught and printed rather than raised, making this suitable for diagnostic scripts but not for programmatic error handling
- The function performs cleanup by deleting the test collection, but if an error occurs during cleanup, it will be silently ignored
- Consider wrapping this in a try-finally block if you need guaranteed cleanup in production scenarios
- The test collection name 'test_collection_debug' is hardcoded - ensure this doesn't conflict with existing collections
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_chroma_collections 86.0% similar
-
function main_v31 76.7% similar
-
function reset_collection 70.4% similar
-
function main_v58 65.9% similar
-
function test_collection_logic 64.3% similar