[PR #28566] feat: add unit tests for SegmentService #32089

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

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

State: closed
Merged: No


Summary

Adds comprehensive unit test coverage for SegmentService methods, covering segment and child chunk operations.

Test Coverage

Segment Operations

  • create_segment: Tests for basic creation, high-quality indexing, QA model documents, and vector indexing failures
  • update_segment: Tests for content updates, enable/disable operations, indexing-in-progress errors, and disabled segment update errors
  • delete_segment: Tests for single segment deletion with and without index cleanup, and deletion-in-progress errors
  • delete_segments: Tests for batch deletion, empty list handling, and child chunk cleanup
  • update_segments_status: Tests for enabling/disabling multiple segments with proper task scheduling
  • get_segments: Tests for retrieval with status filtering, keyword search, and pagination
  • get_segment_by_id: Tests for single segment retrieval and not found cases

Child Chunk Operations

  • get_child_chunks: Tests for retrieval with keyword filtering and pagination
  • get_child_chunk_by_id: Tests for single child chunk retrieval and not found cases
  • create_child_chunk: Tests for creation with vector indexing and error handling
  • update_child_chunk: Tests for content updates with vector indexing and error handling
  • delete_child_chunk: Tests for deletion with vector cleanup and error handling

Test Structure

  • SegmentTestDataFactory: Factory class for creating mock objects (segments, child chunks, documents, datasets, users)
  • Test Classes: Organized by method with clear test cases covering success paths, error conditions, and edge cases
  • Mocking: Proper mocking of database sessions, Redis locks, vector services, and external dependencies

Test Statistics

  • Total Test Classes: 11
  • Total Test Methods: 30+
  • Coverage: All public SegmentService methods with comprehensive scenarios

Related

Part of ongoing effort to improve test coverage for dataset-related services.

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28566 **State:** closed **Merged:** No --- ## Summary Adds comprehensive unit test coverage for `SegmentService` methods, covering segment and child chunk operations. ## Test Coverage ### Segment Operations - **create_segment**: Tests for basic creation, high-quality indexing, QA model documents, and vector indexing failures - **update_segment**: Tests for content updates, enable/disable operations, indexing-in-progress errors, and disabled segment update errors - **delete_segment**: Tests for single segment deletion with and without index cleanup, and deletion-in-progress errors - **delete_segments**: Tests for batch deletion, empty list handling, and child chunk cleanup - **update_segments_status**: Tests for enabling/disabling multiple segments with proper task scheduling - **get_segments**: Tests for retrieval with status filtering, keyword search, and pagination - **get_segment_by_id**: Tests for single segment retrieval and not found cases ### Child Chunk Operations - **get_child_chunks**: Tests for retrieval with keyword filtering and pagination - **get_child_chunk_by_id**: Tests for single child chunk retrieval and not found cases - **create_child_chunk**: Tests for creation with vector indexing and error handling - **update_child_chunk**: Tests for content updates with vector indexing and error handling - **delete_child_chunk**: Tests for deletion with vector cleanup and error handling ## Test Structure - **SegmentTestDataFactory**: Factory class for creating mock objects (segments, child chunks, documents, datasets, users) - **Test Classes**: Organized by method with clear test cases covering success paths, error conditions, and edge cases - **Mocking**: Proper mocking of database sessions, Redis locks, vector services, and external dependencies ## Test Statistics - **Total Test Classes**: 11 - **Total Test Methods**: 30+ - **Coverage**: All public SegmentService methods with comprehensive scenarios ## Related Part of ongoing effort to improve test coverage for dataset-related services. Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:44 -05:00
yindo closed this issue 2026-02-21 20:50:44 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32089