[PR #28897] feat: complete test script of dataset indexing task #32227

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

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

State: closed
Merged: Yes


Add Comprehensive Unit Tests for Dataset Indexing Tasks

Important

  1. Make sure you have read our contribution guidelines
  2. Ensure there is an associated issue and you have been assigned to it
  3. Use the correct syntax to link this PR: Fixes #<issue number>.

Summary

This PR adds comprehensive unit tests for the dataset indexing task functionality, covering all critical workflows and edge cases. The test suite includes 43 tests organized into 9 test classes, ensuring robust coverage of the document indexing pipeline.

Fixes

Closes https://github.com/langgenius/dify/issues/28896

What's Added

New Test File: api/tests/unit_tests/tasks/test_dataset_indexing_task.py

Test Coverage (43 tests total):

  • Task Enqueuing (5 tests) - Different queue types (normal, priority, tenant-isolated)
  • Batch Processing (4 tests) - Multiple documents, limits, sandbox restrictions
  • Progress Tracking (4 tests) - Status progression, timestamps, queue management
  • Error Handling (6 tests) - Validation errors, exceptions, retries
  • Task Cancellation (4 tests) - Cleanup, session management, tenant isolation
  • Advanced Scenarios (6 tests) - Mixed failures, concurrency, FIFO ordering
  • Integration Tests (3 tests) - Complete workflows for normal and priority tasks
  • Edge Cases (5 tests) - Single documents, special characters, rapid enqueuing, limit boundaries
  • Performance Scenarios (3 tests) - Large batches, burst traffic, multi-tenant isolation
  • Robustness (3 tests) - Exception handling, resource cleanup, service failures

Key Features

  • Comprehensive Documentation: Every test includes detailed docstrings explaining the scenario, expected behavior, and test rationale
  • Inline Comments: Complex logic is explained with comments for better understanding
  • Best Practices: Follows TDD principles with Arrange-Act-Assert pattern
  • Proper Mocking: Uses unittest.mock with appropriate fixtures for database, Redis, and external services
  • Edge Case Coverage: Tests boundary conditions, error scenarios, and performance limits
  • Code Quality: Passes all ruff lint checks and follows project conventions

Testing Scenarios Covered

  1. Queue Routing: Validates correct task dispatch based on billing plans (sandbox, professional, self-hosted)
  2. Batch Limits: Ensures batch upload limits are enforced correctly
  3. Tenant Isolation: Verifies tasks are isolated per tenant with proper concurrency control
  4. Error Recovery: Tests graceful handling of various error conditions
  5. Resource Management: Ensures database sessions and Redis connections are properly cleaned up
  6. Performance: Validates handling of large batches and burst traffic

Screenshots

Not applicable - this is a backend testing change with no UI impact.

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

Additional Notes

  • All 43 tests pass successfully
  • Code is fully compliant with ruff lint standards
  • No changes to production code - only test additions
  • Tests use existing fixtures and follow project testing patterns

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28897 **State:** closed **Merged:** Yes --- # Add Comprehensive Unit Tests for Dataset Indexing Tasks > [!IMPORTANT] > > 1. Make sure you have read our [contribution guidelines](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) > 1. Ensure there is an associated issue and you have been assigned to it > 1. Use the correct syntax to link this PR: `Fixes #<issue number>`. ## Summary This PR adds comprehensive unit tests for the dataset indexing task functionality, covering all critical workflows and edge cases. The test suite includes 43 tests organized into 9 test classes, ensuring robust coverage of the document indexing pipeline. ### Fixes Closes https://github.com/langgenius/dify/issues/28896 ### What's Added **New Test File**: `api/tests/unit_tests/tasks/test_dataset_indexing_task.py` **Test Coverage** (43 tests total): - ✅ **Task Enqueuing** (5 tests) - Different queue types (normal, priority, tenant-isolated) - ✅ **Batch Processing** (4 tests) - Multiple documents, limits, sandbox restrictions - ✅ **Progress Tracking** (4 tests) - Status progression, timestamps, queue management - ✅ **Error Handling** (6 tests) - Validation errors, exceptions, retries - ✅ **Task Cancellation** (4 tests) - Cleanup, session management, tenant isolation - ✅ **Advanced Scenarios** (6 tests) - Mixed failures, concurrency, FIFO ordering - ✅ **Integration Tests** (3 tests) - Complete workflows for normal and priority tasks - ✅ **Edge Cases** (5 tests) - Single documents, special characters, rapid enqueuing, limit boundaries - ✅ **Performance Scenarios** (3 tests) - Large batches, burst traffic, multi-tenant isolation - ✅ **Robustness** (3 tests) - Exception handling, resource cleanup, service failures ### Key Features - **Comprehensive Documentation**: Every test includes detailed docstrings explaining the scenario, expected behavior, and test rationale - **Inline Comments**: Complex logic is explained with comments for better understanding - **Best Practices**: Follows TDD principles with Arrange-Act-Assert pattern - **Proper Mocking**: Uses `unittest.mock` with appropriate fixtures for database, Redis, and external services - **Edge Case Coverage**: Tests boundary conditions, error scenarios, and performance limits - **Code Quality**: Passes all ruff lint checks and follows project conventions ### Testing Scenarios Covered 1. **Queue Routing**: Validates correct task dispatch based on billing plans (sandbox, professional, self-hosted) 2. **Batch Limits**: Ensures batch upload limits are enforced correctly 3. **Tenant Isolation**: Verifies tasks are isolated per tenant with proper concurrency control 4. **Error Recovery**: Tests graceful handling of various error conditions 5. **Resource Management**: Ensures database sessions and Redis connections are properly cleaned up 6. **Performance**: Validates handling of large batches and burst traffic ## Screenshots Not applicable - this is a backend testing change with no UI impact. ## Checklist - [ ] 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 ## Additional Notes - All 43 tests pass successfully - Code is fully compliant with ruff lint standards - No changes to production code - only test additions - Tests use existing fixtures and follow project testing patterns Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:51:00 -05:00
yindo closed this issue 2026-02-21 20:51:00 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32227