๐Ÿ” 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 hardcoded to connect to 'vice_chroma' host - modify the host and port parameters if your Chroma DB instance is at a different location
  • The function uses broad exception handling which may mask specific errors - consider adding more granular error handling for production use
  • The test collection name 'test_collection_debug' is hardcoded - ensure this doesn't conflict with production collections
  • This function performs cleanup by deleting the test collection, but if an error occurs before cleanup, the collection may persist
  • All output is via print statements - consider using a logging framework for production environments
  • The function does not return success/failure status - consider returning a boolean or raising exceptions for programmatic use

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_v16 77.2% 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 test_filecloud_operations 54.4% similar

    Tests FileCloud basic operations by creating a test folder to verify connectivity and authentication with a FileCloud server.

    From: /tf/active/vicechatdev/SPFCsync/test_connections.py
  • function quick_test 52.1% similar

    A diagnostic function that tests SharePoint Graph API connectivity and verifies access to the main site library by checking for expected folder structures in the root directory.

    From: /tf/active/vicechatdev/SPFCsync/quick_test.py
  • function test_reference_system_completeness 50.9% similar

    A diagnostic test function that prints a comprehensive overview of a reference system's architecture, including backend storage, API endpoints, reference types, and content flow verification.

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