๐Ÿ” Code Extractor

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
Complexity:
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

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function test_chroma_collections 86.0% similar

    A diagnostic function that tests connectivity to ChromaDB instances across multiple connection methods and lists all available collections with their metadata.

    From: /tf/active/vicechatdev/test_chroma_collections.py
  • function main_v31 76.7% similar

    Entry point function that executes a comprehensive test suite for Chroma DB collections, including collection listing and creation tests, followed by troubleshooting suggestions.

    From: /tf/active/vicechatdev/test_chroma_collections.py
  • function reset_collection 70.4% similar

    Deletes an existing ChromaDB collection and logs the operation, requiring an application restart to recreate the collection.

    From: /tf/active/vicechatdev/docchat/reset_collection.py
  • function main_v58 65.9% similar

    Command-line interface function that orchestrates the cleaning of ChromaDB collections by removing duplicates and similar documents, with options to skip collections and customize the cleaning process.

    From: /tf/active/vicechatdev/chromadb-cleanup/main.py
  • function test_collection_logic 64.3% similar

    A test function that validates collection processing logic, specifically testing how collections are handled when selected, deselected, or missing, with focus on data clearing behavior.

    From: /tf/active/vicechatdev/vice_ai/test_collection_fix.py
โ† Back to Browse