[PR #28834] test: add comprehensive unit tests for VectorService and Vector classes #32201

Closed
opened 2026-02-21 20:50:57 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/28834

State: closed
Merged: Yes


Summary

This PR adds comprehensive unit tests for the VectorService and Vector classes, which are critical components in the RAG (Retrieval-Augmented Generation) pipeline. The test suite ensures proper vector database operations, collection management, embedding storage and retrieval, vector index operations, and metadata filtering functionality.

Fix: #28831

Test Coverage

1. VectorService — Segment Vector Operations

create_segments_vector

  • Regular indexing (non-hierarchical) with keywords
  • Parent-child indexing with child chunk generation
  • Missing document handling (skips with warning)
  • Missing processing rule error handling
  • Economy indexing technique validation
  • Empty documents list handling

update_segment_vector

  • High-quality indexing with vector store updates
  • Economy indexing with keywords
  • Economy indexing without keywords
  • Duplicate checking in vector updates

2. VectorService — Child Chunk Operations

generate_child_chunks

  • Child chunk generation with children
  • Regenerate mode (cleans existing chunks)
  • No children generated handling
  • Full doc mode for parent chunks

create_child_chunk_vector

  • High-quality indexing creates child chunk vectors
  • Economy indexing skips vector creation
  • Duplicate checking enabled

update_child_chunk_vector

  • Batch updates with new, update, and delete operations
  • Only new chunks (no deletion)
  • Only deleted chunks (no addition)
  • Economy indexing skips updates

delete_child_chunk_vector

  • High-quality indexing deletes child chunk vectors
  • Economy indexing skips deletion

3. Vector Class — Initialization and Configuration

  • Initialization with default attributes
  • Initialization with custom attributes
  • Dataset configuration handling

4. Vector Class — Document Operations

create

  • Batch document embedding and creation
  • Empty texts list handling
  • Large batch processing (2500+ documents in chunks of 1000)
  • Batch embedding generation

add_texts

  • Adding documents without duplicate check
  • Adding documents with duplicate check (filters duplicates)
  • Embedding generation for new documents

5. Vector Class — Query Operations

text_exists

  • Returns True when document exists
  • Returns False when document does not exist

search_by_vector

  • Query embedding generation
  • Vector similarity search
  • Returns search results

search_by_full_text

  • Full-text search without embedding
  • Returns search results

6. Vector Class — Deletion Operations

delete_by_ids

  • Deletes documents by their IDs
  • Handles multiple IDs

delete_by_metadata_field

  • Deletes documents by metadata field value
  • Supports various metadata fields

delete

  • Deletes entire collection
  • Clears Redis cache for collection existence

7. Vector Class — Factory and Utilities

get_vector_factory

  • Returns correct factory for Chroma
  • Returns correct factory for Milvus
  • Raises ValueError for invalid vector types
  • Supports all vector database backends

_filter_duplicate_texts

  • Filters documents that already exist
  • Handles documents without metadata
  • Handles documents with empty metadata

_get_embeddings

  • Retrieves embedding model from ModelManager
  • Wraps in CacheEmbedding for performance
  • Handles model configuration

Test Structure

  • Test Data Factory: Centralized mock object creation for consistency
  • Comprehensive Documentation: Extensive comments explaining test scenarios, architecture, and vector operations
  • Error Path Coverage: All error conditions and edge cases are tested
  • Mock Strategy: Proper isolation using unittest.mock for dependencies (ModelManager, IndexProcessor, Redis, vector processors)
  • Validation Testing: Ensures all vector operations are correctly performed

Files Changed

  • api/tests/unit_tests/services/vector_service.py (1,200+ lines)

Testing Approach

  • Follows existing test patterns from other service test files
  • Uses Arrange-Act-Assert structure
  • Comprehensive mocking of dependencies (ModelManager, IndexProcessor, Redis, BaseVector implementations)
  • Tests both success and error paths
  • Validates vector operations, embedding generation, and search functionality
  • Ensures proper integration with various components

Integration Points

These tests ensure proper integration with:

  • ModelManager: Embedding model instance retrieval and configuration
  • IndexProcessor: Document transformation and indexing
  • Redis: Collection locking and cache management
  • BaseVector: Vector database backend abstraction
  • Database Session: Child chunk persistence
  • Vector Database Backends: Support for multiple vector stores (Chroma, Milvus, Qdrant, etc.)

Key Features Tested

  1. Vector Database Operations: CRUD operations on vector stores
  2. Collection Management: Creation with Redis locking, deletion with cache clearing
  3. Embedding Operations: Generation, batch processing, caching
  4. Search Functionality: Vector similarity search and full-text search
  5. Metadata Filtering: Querying and deletion by metadata fields
  6. Hierarchical Indexing: Parent-child chunk generation and management
  7. Duplicate Handling: Detection and filtering of duplicate documents
  8. Batch Processing: Efficient handling of large document sets

Related Issues

This test suite addresses the need for comprehensive coverage of vector service functionality, ensuring safe refactoring and preventing regressions in critical RAG pipeline components that affect document indexing, retrieval, and search performance.

Contribution by Gittensor, learn more at https://gittensor.io/

**Original Pull Request:** https://github.com/langgenius/dify/pull/28834 **State:** closed **Merged:** Yes --- ## Summary This PR adds comprehensive unit tests for the `VectorService` and `Vector` classes, which are critical components in the RAG (Retrieval-Augmented Generation) pipeline. The test suite ensures proper vector database operations, collection management, embedding storage and retrieval, vector index operations, and metadata filtering functionality. Fix: #28831 ## Test Coverage ### 1. VectorService — Segment Vector Operations #### `create_segments_vector` - ✅ Regular indexing (non-hierarchical) with keywords - ✅ Parent-child indexing with child chunk generation - ✅ Missing document handling (skips with warning) - ✅ Missing processing rule error handling - ✅ Economy indexing technique validation - ✅ Empty documents list handling #### `update_segment_vector` - ✅ High-quality indexing with vector store updates - ✅ Economy indexing with keywords - ✅ Economy indexing without keywords - ✅ Duplicate checking in vector updates ### 2. VectorService — Child Chunk Operations #### `generate_child_chunks` - ✅ Child chunk generation with children - ✅ Regenerate mode (cleans existing chunks) - ✅ No children generated handling - ✅ Full doc mode for parent chunks #### `create_child_chunk_vector` - ✅ High-quality indexing creates child chunk vectors - ✅ Economy indexing skips vector creation - ✅ Duplicate checking enabled #### `update_child_chunk_vector` - ✅ Batch updates with new, update, and delete operations - ✅ Only new chunks (no deletion) - ✅ Only deleted chunks (no addition) - ✅ Economy indexing skips updates #### `delete_child_chunk_vector` - ✅ High-quality indexing deletes child chunk vectors - ✅ Economy indexing skips deletion ### 3. Vector Class — Initialization and Configuration - ✅ Initialization with default attributes - ✅ Initialization with custom attributes - ✅ Dataset configuration handling ### 4. Vector Class — Document Operations #### `create` - ✅ Batch document embedding and creation - ✅ Empty texts list handling - ✅ Large batch processing (2500+ documents in chunks of 1000) - ✅ Batch embedding generation #### `add_texts` - ✅ Adding documents without duplicate check - ✅ Adding documents with duplicate check (filters duplicates) - ✅ Embedding generation for new documents ### 5. Vector Class — Query Operations #### `text_exists` - ✅ Returns True when document exists - ✅ Returns False when document does not exist #### `search_by_vector` - ✅ Query embedding generation - ✅ Vector similarity search - ✅ Returns search results #### `search_by_full_text` - ✅ Full-text search without embedding - ✅ Returns search results ### 6. Vector Class — Deletion Operations #### `delete_by_ids` - ✅ Deletes documents by their IDs - ✅ Handles multiple IDs #### `delete_by_metadata_field` - ✅ Deletes documents by metadata field value - ✅ Supports various metadata fields #### `delete` - ✅ Deletes entire collection - ✅ Clears Redis cache for collection existence ### 7. Vector Class — Factory and Utilities #### `get_vector_factory` - ✅ Returns correct factory for Chroma - ✅ Returns correct factory for Milvus - ✅ Raises ValueError for invalid vector types - ✅ Supports all vector database backends #### `_filter_duplicate_texts` - ✅ Filters documents that already exist - ✅ Handles documents without metadata - ✅ Handles documents with empty metadata #### `_get_embeddings` - ✅ Retrieves embedding model from ModelManager - ✅ Wraps in CacheEmbedding for performance - ✅ Handles model configuration ## Test Structure - **Test Data Factory**: Centralized mock object creation for consistency - **Comprehensive Documentation**: Extensive comments explaining test scenarios, architecture, and vector operations - **Error Path Coverage**: All error conditions and edge cases are tested - **Mock Strategy**: Proper isolation using `unittest.mock` for dependencies (ModelManager, IndexProcessor, Redis, vector processors) - **Validation Testing**: Ensures all vector operations are correctly performed ## Files Changed - `api/tests/unit_tests/services/vector_service.py` (1,200+ lines) ## Testing Approach - Follows existing test patterns from other service test files - Uses Arrange-Act-Assert structure - Comprehensive mocking of dependencies (ModelManager, IndexProcessor, Redis, BaseVector implementations) - Tests both success and error paths - Validates vector operations, embedding generation, and search functionality - Ensures proper integration with various components ## Integration Points These tests ensure proper integration with: - **ModelManager**: Embedding model instance retrieval and configuration - **IndexProcessor**: Document transformation and indexing - **Redis**: Collection locking and cache management - **BaseVector**: Vector database backend abstraction - **Database Session**: Child chunk persistence - **Vector Database Backends**: Support for multiple vector stores (Chroma, Milvus, Qdrant, etc.) ## Key Features Tested 1. **Vector Database Operations**: CRUD operations on vector stores 2. **Collection Management**: Creation with Redis locking, deletion with cache clearing 3. **Embedding Operations**: Generation, batch processing, caching 4. **Search Functionality**: Vector similarity search and full-text search 5. **Metadata Filtering**: Querying and deletion by metadata fields 6. **Hierarchical Indexing**: Parent-child chunk generation and management 7. **Duplicate Handling**: Detection and filtering of duplicate documents 8. **Batch Processing**: Efficient handling of large document sets ## Related Issues This test suite addresses the need for comprehensive coverage of vector service functionality, ensuring safe refactoring and preventing regressions in critical RAG pipeline components that affect document indexing, retrieval, and search performance. Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:57 -05:00
yindo closed this issue 2026-02-21 20:50:57 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32201