[PR #28757] test: add unit tests for dataset service update/delete operations #32169

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

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

State: closed
Merged: Yes


Summary

This PR adds comprehensive unit tests for DatasetService update and delete operations, covering critical dataset lifecycle management functionality.

Fix: #28755

Test Coverage

1. Dataset Service — Update Operations (update_dataset)

  • Internal dataset updates
  • External dataset updates
  • Permission validation
  • Duplicate name detection
  • Dataset not found error handling

2. Dataset Service — Delete Operations (delete_dataset)

  • Successful dataset deletion with event signaling
  • Permission validation before deletion
  • Database cleanup and transaction commit
  • Dataset not found handling
  • Event handler integration (dataset_was_deleted)

3. Dataset Use Check (dataset_use_check)

  • Detection when dataset is in use by applications
  • Detection when dataset is not in use
  • Database query validation via AppDatasetJoin

4. Dataset API Status Management (update_dataset_api_status)

  • Enabling API access for datasets
  • Disabling API access for datasets
  • Current user validation
  • Dataset not found error handling
  • Timestamp and user tracking updates

5. RAG Pipeline Dataset Settings (update_rag_pipeline_dataset_settings)

  • Unpublished dataset configuration updates
  • Published dataset configuration updates
  • Chunk structure validation (prevents changes on published datasets)
  • Indexing technique changes (high_quality ↔ economy)
  • Embedding model configuration
  • Keyword number updates for economy indexing
  • Error handling for invalid operations

Test Structure

  • Test Data Factory: Centralized mock object creation for consistency
  • Comprehensive Documentation: Extensive comments explaining test scenarios and architecture
  • Error Path Coverage: All error conditions and edge cases are tested
  • Mock Strategy: Proper isolation using unittest.mock for dependencies

Files Changed

  • api/tests/unit_tests/services/dataset_service_update_delete.py (1,233 lines)

Testing Approach

  • Follows existing test patterns from other DatasetService test files
  • Uses Arrange-Act-Assert structure
  • Comprehensive mocking of dependencies (database, events, services)
  • Tests both success and error paths
  • Validates permission checks and data integrity

Related Issues

This test suite addresses the need for comprehensive coverage of dataset update and delete operations, ensuring safe refactoring and preventing regressions in critical dataset lifecycle management functionality.

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28757 **State:** closed **Merged:** Yes --- ## Summary This PR adds comprehensive unit tests for DatasetService update and delete operations, covering critical dataset lifecycle management functionality. Fix: #28755 ## Test Coverage ### 1. Dataset Service — Update Operations (`update_dataset`) - ✅ Internal dataset updates - ✅ External dataset updates - ✅ Permission validation - ✅ Duplicate name detection - ✅ Dataset not found error handling ### 2. Dataset Service — Delete Operations (`delete_dataset`) - ✅ Successful dataset deletion with event signaling - ✅ Permission validation before deletion - ✅ Database cleanup and transaction commit - ✅ Dataset not found handling - ✅ Event handler integration (`dataset_was_deleted`) ### 3. Dataset Use Check (`dataset_use_check`) - ✅ Detection when dataset is in use by applications - ✅ Detection when dataset is not in use - ✅ Database query validation via `AppDatasetJoin` ### 4. Dataset API Status Management (`update_dataset_api_status`) - ✅ Enabling API access for datasets - ✅ Disabling API access for datasets - ✅ Current user validation - ✅ Dataset not found error handling - ✅ Timestamp and user tracking updates ### 5. RAG Pipeline Dataset Settings (`update_rag_pipeline_dataset_settings`) - ✅ Unpublished dataset configuration updates - ✅ Published dataset configuration updates - ✅ Chunk structure validation (prevents changes on published datasets) - ✅ Indexing technique changes (high_quality ↔ economy) - ✅ Embedding model configuration - ✅ Keyword number updates for economy indexing - ✅ Error handling for invalid operations ## Test Structure - **Test Data Factory**: Centralized mock object creation for consistency - **Comprehensive Documentation**: Extensive comments explaining test scenarios and architecture - **Error Path Coverage**: All error conditions and edge cases are tested - **Mock Strategy**: Proper isolation using `unittest.mock` for dependencies ## Files Changed - `api/tests/unit_tests/services/dataset_service_update_delete.py` (1,233 lines) ## Testing Approach - Follows existing test patterns from other DatasetService test files - Uses Arrange-Act-Assert structure - Comprehensive mocking of dependencies (database, events, services) - Tests both success and error paths - Validates permission checks and data integrity ## Related Issues This test suite addresses the need for comprehensive coverage of dataset update and delete operations, ensuring safe refactoring and preventing regressions in critical dataset lifecycle management functionality. Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:53 -05:00
yindo closed this issue 2026-02-21 20:50:53 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32169