[PR #28804] test: add unit tests for document service status management #32186

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

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

State: closed
Merged: Yes


Summary

This PR adds comprehensive unit tests for DocumentService status management methods, covering critical document lifecycle management functionality including pause, recover, retry, batch updates, and renaming operations.

Fix: #28797

Test Coverage

1. Document Service — Pause Operations (pause_document)

  • Pausing documents in waiting state
  • Pausing documents in indexing state
  • Pausing documents in parsing state
  • Pausing documents in cleaning and splitting states
  • Error handling for completed documents (cannot pause)
  • Error handling for error state documents (cannot pause)
  • Redis cache flag setting for pause state
  • Current user validation and tracking (paused_by, paused_at)

2. Document Service — Recovery Operations (recover_document)

  • Recovering paused documents successfully
  • Clearing pause flags (is_paused, paused_by, paused_at)
  • Redis cache flag deletion
  • Recovery task triggering for async indexing resumption
  • Error handling for non-paused documents (cannot recover)

3. Document Service — Retry Operations (retry_document)

  • Retrying single document successfully
  • Retrying multiple documents in batch
  • Preventing concurrent retry attempts (Redis flag check)
  • Retry flag management in Redis cache (600s TTL)
  • Resetting document indexing_status to "waiting"
  • Retry task triggering with document IDs and user ID
  • Current user validation
  • Error handling for documents already being retried

4. Document Service — Batch Status Updates (batch_update_document_status)

  • Batch enabling documents (with async indexing task)
  • Batch disabling documents (with async removal task)
  • Batch archiving documents (with conditional async tasks)
  • Batch unarchiving documents (with conditional async tasks)
  • Handling empty document lists gracefully
  • Invalid action error handling
  • Document indexing state validation (prevents updates during indexing)
  • Transaction handling (commit on success, rollback on error)
  • Redis cache flag management for indexing state
  • Async task execution after successful commit
  • Error propagation handling

5. Document Service — Rename Operations (rename_document)

  • Successful document renaming
  • Renaming with built-in fields enabled (metadata updates)
  • Renaming with associated upload files (file name updates)
  • Dataset not found error handling
  • Document not found error handling
  • Tenant permission validation
  • Database transaction handling

Document State Management

The test suite comprehensively covers document state transitions:

  • Indexing States: waiting, parsing, cleaning, splitting, indexing
  • Status Flags: is_paused, enabled, archived
  • Timestamps: paused_at, disabled_at, archived_at
  • User Tracking: paused_by, disabled_by, archived_by

Test Structure

  • Test Data Factory: Centralized mock object creation for consistency
  • Comprehensive Documentation: Extensive comments explaining test scenarios, architecture, and state management
  • Error Path Coverage: All error conditions and edge cases are tested
  • Mock Strategy: Proper isolation using unittest.mock for dependencies (database, Redis, async tasks)
  • State Transition Testing: Validates correct state changes and prevents invalid transitions

Files Changed

  • api/tests/unit_tests/services/document_service_status.py (1,335 lines)

Testing Approach

  • Follows existing test patterns from other DocumentService test files
  • Uses Arrange-Act-Assert structure
  • Comprehensive mocking of dependencies (database, Redis cache, async tasks, current_user)
  • Tests both success and error paths
  • Validates state transitions and prevents invalid operations
  • Ensures proper transaction handling and rollback on errors

Integration Points

These tests ensure proper integration with:

  • Redis Cache: Pause flags, retry flags, indexing flags
  • Async Task Queue: Recovery tasks, retry tasks, indexing tasks
  • Database: State persistence, transaction management
  • Permission System: Tenant validation, user tracking

Related Issues

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

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28804 **State:** closed **Merged:** Yes --- ## Summary This PR adds comprehensive unit tests for DocumentService status management methods, covering critical document lifecycle management functionality including pause, recover, retry, batch updates, and renaming operations. Fix: #28797 ## Test Coverage ### 1. Document Service — Pause Operations (`pause_document`) - ✅ Pausing documents in waiting state - ✅ Pausing documents in indexing state - ✅ Pausing documents in parsing state - ✅ Pausing documents in cleaning and splitting states - ✅ Error handling for completed documents (cannot pause) - ✅ Error handling for error state documents (cannot pause) - ✅ Redis cache flag setting for pause state - ✅ Current user validation and tracking (paused_by, paused_at) ### 2. Document Service — Recovery Operations (`recover_document`) - ✅ Recovering paused documents successfully - ✅ Clearing pause flags (is_paused, paused_by, paused_at) - ✅ Redis cache flag deletion - ✅ Recovery task triggering for async indexing resumption - ✅ Error handling for non-paused documents (cannot recover) ### 3. Document Service — Retry Operations (`retry_document`) - ✅ Retrying single document successfully - ✅ Retrying multiple documents in batch - ✅ Preventing concurrent retry attempts (Redis flag check) - ✅ Retry flag management in Redis cache (600s TTL) - ✅ Resetting document indexing_status to "waiting" - ✅ Retry task triggering with document IDs and user ID - ✅ Current user validation - ✅ Error handling for documents already being retried ### 4. Document Service — Batch Status Updates (`batch_update_document_status`) - ✅ Batch enabling documents (with async indexing task) - ✅ Batch disabling documents (with async removal task) - ✅ Batch archiving documents (with conditional async tasks) - ✅ Batch unarchiving documents (with conditional async tasks) - ✅ Handling empty document lists gracefully - ✅ Invalid action error handling - ✅ Document indexing state validation (prevents updates during indexing) - ✅ Transaction handling (commit on success, rollback on error) - ✅ Redis cache flag management for indexing state - ✅ Async task execution after successful commit - ✅ Error propagation handling ### 5. Document Service — Rename Operations (`rename_document`) - ✅ Successful document renaming - ✅ Renaming with built-in fields enabled (metadata updates) - ✅ Renaming with associated upload files (file name updates) - ✅ Dataset not found error handling - ✅ Document not found error handling - ✅ Tenant permission validation - ✅ Database transaction handling ## Document State Management The test suite comprehensively covers document state transitions: - **Indexing States**: waiting, parsing, cleaning, splitting, indexing - **Status Flags**: is_paused, enabled, archived - **Timestamps**: paused_at, disabled_at, archived_at - **User Tracking**: paused_by, disabled_by, archived_by ## Test Structure - **Test Data Factory**: Centralized mock object creation for consistency - **Comprehensive Documentation**: Extensive comments explaining test scenarios, architecture, and state management - **Error Path Coverage**: All error conditions and edge cases are tested - **Mock Strategy**: Proper isolation using `unittest.mock` for dependencies (database, Redis, async tasks) - **State Transition Testing**: Validates correct state changes and prevents invalid transitions ## Files Changed - `api/tests/unit_tests/services/document_service_status.py` (1,335 lines) ## Testing Approach - Follows existing test patterns from other DocumentService test files - Uses Arrange-Act-Assert structure - Comprehensive mocking of dependencies (database, Redis cache, async tasks, current_user) - Tests both success and error paths - Validates state transitions and prevents invalid operations - Ensures proper transaction handling and rollback on errors ## Integration Points These tests ensure proper integration with: - **Redis Cache**: Pause flags, retry flags, indexing flags - **Async Task Queue**: Recovery tasks, retry tasks, indexing tasks - **Database**: State persistence, transaction management - **Permission System**: Tenant validation, user tracking ## Related Issues This test suite addresses the need for comprehensive coverage of document status management functionality, ensuring safe refactoring and preventing regressions in critical document lifecycle management operations. Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:55 -05:00
yindo closed this issue 2026-02-21 20:50:55 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32186