[PR #28918] feat: Add comprehensive unit tests for RAG pipeline dataset operation… #32241

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

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

State: closed
Merged: No


Summary

This PR adds comprehensive unit tests for the RAG pipeline dataset operations in the DatasetService class. The test suite provides complete coverage for RAG pipeline dataset creation, knowledge base node updates, and settings configuration operations, ensuring robust pipeline workflow integration and dataset management functionality.

Fix: #28917

What's Changed

  • Test Coverage: Added comprehensive unit tests covering 3 critical RAG pipeline dataset methods in DatasetService
  • Documentation: Extensive inline documentation, comments, and docstrings explaining test logic and expected behavior
  • Test Organization: Tests organized into three main test classes:
    • TestRAGPipelineDatasetCreation: Dataset and pipeline creation operations
    • TestPipelineKnowledgeBaseNodeUpdates: Knowledge-index node synchronization
    • TestPipelineDatasetSettingsUpdates: Dataset settings configuration and restrictions

Test Coverage Details

RAG Pipeline Dataset Creation

  • create_empty_rag_pipeline_dataset: Dataset and pipeline creation
    • Successful creation with custom name
    • Auto-name generation for unnamed datasets (uses generate_incremental_name)
    • Duplicate name validation (raises DatasetNameDuplicateError)
    • Pipeline and dataset integration (linked via pipeline_id)
    • Current user validation
    • Database session operations (add, flush, commit)
    • Runtime mode set to "rag_pipeline"
    • Provider set to "vendor"
    • Icon info handling

Pipeline Knowledge Base Node Updates

  • _update_pipeline_knowledge_base_node_data: Knowledge-index node synchronization
    • Updates knowledge-index nodes in published workflows
    • Updates knowledge-index nodes in draft workflows
    • Node data synchronization with dataset settings (embedding model, retrieval model, chunk structure, indexing technique)
    • Early return for non-RAG pipeline datasets (runtime_mode != "rag_pipeline")
    • Handling missing pipelines (graceful skip)
    • Workflow version creation when changes detected
    • Error handling during node updates
    • RagPipelineService integration

Pipeline Dataset Settings Updates

  • update_rag_pipeline_dataset_settings: Dataset configuration management
    • Unpublished Dataset Settings:
      • High quality indexing configuration (embedding model setup)
      • Economy indexing configuration (keyword number setup)
      • Full configuration flexibility
      • ModelManager integration for embedding models
      • Collection binding retrieval
    • Published Dataset Settings (with restrictions):
      • Chunk structure update restriction (raises ValueError)
      • Indexing technique change restriction (cannot change to economy)
      • High quality indexing updates allowed
      • Embedding model configuration for published datasets
    • Current user validation
    • Invalid index method error handling
    • Session merge and save operations

Key Features

  • 100% Function Coverage: All specified RAG pipeline dataset methods are tested
  • Edge Case Handling: Tests cover early returns, missing pipelines, invalid configurations, published vs unpublished restrictions
  • Security Testing: User validation, tenant isolation, permission checks
  • Mocking Strategy: All external dependencies (database, RagPipelineService, ModelManager, DatasetCollectionBindingService, current_user) are properly mocked
  • Factory Pattern: Uses RAGPipelineDatasetTestDataFactory for consistent test data creation
  • AAA Pattern: All tests follow Arrange-Act-Assert structure

Testing Approach

  • Mocked database session and queries for fast, isolated unit tests
  • Mocked RagPipelineService for workflow operations
  • Mocked ModelManager and DatasetCollectionBindingService for embedding model operations
  • Mocked current_user for authentication context
  • Comprehensive error handling tests (DatasetNameDuplicateError, ValueError)
  • Validation tests for duplicate names, user authentication, configuration restrictions
  • Published vs unpublished dataset behavior verification

Screenshots

N/A (Backend unit tests, no UI changes)

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Related Files

  • api/services/dataset_service.py - Service under test (methods: create_empty_rag_pipeline_dataset, _update_pipeline_knowledge_base_node_data, update_rag_pipeline_dataset_settings)
  • api/tests/unit_tests/services/test_rag_pipeline_dataset.py - Test suite (1,147+ lines)
  • api/models/dataset.py - Dataset and Pipeline models
  • api/services/rag_pipeline/rag_pipeline.py - RagPipelineService for workflow operations
  • api/services/entities/knowledge_entities/rag_pipeline_entities.py - RagPipelineDatasetCreateEntity and KnowledgeConfiguration

Benefits

  • Improved Code Reliability: Comprehensive test coverage ensures RAG pipeline dataset operations work correctly
  • Enhanced Pipeline Integration: Tests validate proper integration between datasets and pipeline workflows
  • Easier Refactoring: Tests provide confidence when making changes to RAG pipeline dataset logic
  • Better Documentation: Extensive comments serve as documentation for expected behavior
  • Regression Prevention: Tests catch regressions when making changes to pipeline-related code
  • Published vs Unpublished Validation: Tests verify restrictions are properly enforced for published datasets
  • Workflow Synchronization: Tests confirm knowledge-index nodes are properly synchronized with dataset settings
  • Configuration Management: Tests verify embedding model and indexing technique configurations work correctly

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28918 **State:** closed **Merged:** No --- ## Summary This PR adds comprehensive unit tests for the RAG pipeline dataset operations in the `DatasetService` class. The test suite provides complete coverage for RAG pipeline dataset creation, knowledge base node updates, and settings configuration operations, ensuring robust pipeline workflow integration and dataset management functionality. Fix: #28917 ### What's Changed - **Test Coverage**: Added comprehensive unit tests covering 3 critical RAG pipeline dataset methods in `DatasetService` - **Documentation**: Extensive inline documentation, comments, and docstrings explaining test logic and expected behavior - **Test Organization**: Tests organized into three main test classes: - `TestRAGPipelineDatasetCreation`: Dataset and pipeline creation operations - `TestPipelineKnowledgeBaseNodeUpdates`: Knowledge-index node synchronization - `TestPipelineDatasetSettingsUpdates`: Dataset settings configuration and restrictions ### Test Coverage Details #### RAG Pipeline Dataset Creation - ✅ `create_empty_rag_pipeline_dataset`: Dataset and pipeline creation - Successful creation with custom name - Auto-name generation for unnamed datasets (uses `generate_incremental_name`) - Duplicate name validation (raises `DatasetNameDuplicateError`) - Pipeline and dataset integration (linked via `pipeline_id`) - Current user validation - Database session operations (add, flush, commit) - Runtime mode set to `"rag_pipeline"` - Provider set to `"vendor"` - Icon info handling #### Pipeline Knowledge Base Node Updates - ✅ `_update_pipeline_knowledge_base_node_data`: Knowledge-index node synchronization - Updates knowledge-index nodes in published workflows - Updates knowledge-index nodes in draft workflows - Node data synchronization with dataset settings (embedding model, retrieval model, chunk structure, indexing technique) - Early return for non-RAG pipeline datasets (`runtime_mode != "rag_pipeline"`) - Handling missing pipelines (graceful skip) - Workflow version creation when changes detected - Error handling during node updates - RagPipelineService integration #### Pipeline Dataset Settings Updates - ✅ `update_rag_pipeline_dataset_settings`: Dataset configuration management - **Unpublished Dataset Settings**: - High quality indexing configuration (embedding model setup) - Economy indexing configuration (keyword number setup) - Full configuration flexibility - ModelManager integration for embedding models - Collection binding retrieval - **Published Dataset Settings** (with restrictions): - Chunk structure update restriction (raises `ValueError`) - Indexing technique change restriction (cannot change to economy) - High quality indexing updates allowed - Embedding model configuration for published datasets - Current user validation - Invalid index method error handling - Session merge and save operations ### Key Features - **100% Function Coverage**: All specified RAG pipeline dataset methods are tested - **Edge Case Handling**: Tests cover early returns, missing pipelines, invalid configurations, published vs unpublished restrictions - **Security Testing**: User validation, tenant isolation, permission checks - **Mocking Strategy**: All external dependencies (database, RagPipelineService, ModelManager, DatasetCollectionBindingService, current_user) are properly mocked - **Factory Pattern**: Uses `RAGPipelineDatasetTestDataFactory` for consistent test data creation - **AAA Pattern**: All tests follow Arrange-Act-Assert structure ### Testing Approach - Mocked database session and queries for fast, isolated unit tests - Mocked RagPipelineService for workflow operations - Mocked ModelManager and DatasetCollectionBindingService for embedding model operations - Mocked current_user for authentication context - Comprehensive error handling tests (DatasetNameDuplicateError, ValueError) - Validation tests for duplicate names, user authentication, configuration restrictions - Published vs unpublished dataset behavior verification ## Screenshots N/A (Backend unit tests, no UI changes) ## Checklist - [x] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods ### Related Files - `api/services/dataset_service.py` - Service under test (methods: create_empty_rag_pipeline_dataset, _update_pipeline_knowledge_base_node_data, update_rag_pipeline_dataset_settings) - `api/tests/unit_tests/services/test_rag_pipeline_dataset.py` - Test suite (1,147+ lines) - `api/models/dataset.py` - Dataset and Pipeline models - `api/services/rag_pipeline/rag_pipeline.py` - RagPipelineService for workflow operations - `api/services/entities/knowledge_entities/rag_pipeline_entities.py` - RagPipelineDatasetCreateEntity and KnowledgeConfiguration ### Benefits - **Improved Code Reliability**: Comprehensive test coverage ensures RAG pipeline dataset operations work correctly - **Enhanced Pipeline Integration**: Tests validate proper integration between datasets and pipeline workflows - **Easier Refactoring**: Tests provide confidence when making changes to RAG pipeline dataset logic - **Better Documentation**: Extensive comments serve as documentation for expected behavior - **Regression Prevention**: Tests catch regressions when making changes to pipeline-related code - **Published vs Unpublished Validation**: Tests verify restrictions are properly enforced for published datasets - **Workflow Synchronization**: Tests confirm knowledge-index nodes are properly synchronized with dataset settings - **Configuration Management**: Tests verify embedding model and indexing technique configurations work correctly Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:51:01 -05:00
yindo closed this issue 2026-02-21 20:51:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32241