[PR #28668] test: add comprehensive unit tests for HitTestingService Fix: #28667 #32131

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

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

State: closed
Merged: Yes


Summary

Adds comprehensive unit test coverage for all HitTestingService methods to ensure reliability and correctness of retrieval testing operations.

Fix: #28667

Test Coverage

Retrieval Testing Operations

  • retrieve (hit_testing) - Tests retrieval from internal datasets with:
    • Default and custom retrieval models
    • Various retrieval methods (semantic search, keyword search, hybrid)
    • Reranking configuration
    • Metadata filtering conditions
    • Score thresholds
    • Document ID filtering
    • Query logging to database
  • external_retrieve - Tests retrieval from external knowledge bases with:
    • Query escaping for special characters
    • Metadata filtering conditions
    • Proper response formatting
    • Non-external provider handling (returns empty)
  • hit_testing_args_check - Validates query arguments:
    • Required query validation
    • Maximum length (250 characters) validation
    • Edge cases (empty, None, exactly 250 chars)
  • compact_retrieve_response - Formats retrieval results for internal datasets
  • compact_external_retrieve_response - Formats retrieval results for external knowledge bases with proper field handling
  • escape_query_for_search - Escapes special characters in queries for external search

Test Structure

  • Test Data Factory: HitTestingTestDataFactory provides reusable mock objects for datasets, users, documents, and retrieval records
  • Comprehensive Coverage: Each method tested with success scenarios, error conditions, and edge cases
  • Proper Mocking: All dependencies properly mocked (RetrievalService, DatasetRetrieval, db.session, time.perf_counter, etc.)
  • Follows Patterns: Test structure follows existing patterns from other service test files

Files Changed

  • api/tests/unit_tests/services/hit_service.py - New test file (598 lines)

Testing

  • All tests follow Arrange-Act-Assert pattern
  • Proper use of pytest fixtures for test setup
  • Mock objects properly configured to simulate real behavior
  • Error conditions and edge cases thoroughly tested
  • Tests cover various retrieval configurations and scenarios

Related

Part of ongoing effort to improve test coverage for dataset service methods.

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28668 **State:** closed **Merged:** Yes --- ## Summary Adds comprehensive unit test coverage for all `HitTestingService` methods to ensure reliability and correctness of retrieval testing operations. Fix: #28667 ## Test Coverage ### Retrieval Testing Operations - ✅ **retrieve** (hit_testing) - Tests retrieval from internal datasets with: - Default and custom retrieval models - Various retrieval methods (semantic search, keyword search, hybrid) - Reranking configuration - Metadata filtering conditions - Score thresholds - Document ID filtering - Query logging to database - ✅ **external_retrieve** - Tests retrieval from external knowledge bases with: - Query escaping for special characters - Metadata filtering conditions - Proper response formatting - Non-external provider handling (returns empty) - ✅ **hit_testing_args_check** - Validates query arguments: - Required query validation - Maximum length (250 characters) validation - Edge cases (empty, None, exactly 250 chars) - ✅ **compact_retrieve_response** - Formats retrieval results for internal datasets - ✅ **compact_external_retrieve_response** - Formats retrieval results for external knowledge bases with proper field handling - ✅ **escape_query_for_search** - Escapes special characters in queries for external search ## Test Structure - **Test Data Factory**: `HitTestingTestDataFactory` provides reusable mock objects for datasets, users, documents, and retrieval records - **Comprehensive Coverage**: Each method tested with success scenarios, error conditions, and edge cases - **Proper Mocking**: All dependencies properly mocked (RetrievalService, DatasetRetrieval, db.session, time.perf_counter, etc.) - **Follows Patterns**: Test structure follows existing patterns from other service test files ## Files Changed - `api/tests/unit_tests/services/hit_service.py` - New test file (598 lines) ## Testing - All tests follow Arrange-Act-Assert pattern - Proper use of pytest fixtures for test setup - Mock objects properly configured to simulate real behavior - Error conditions and edge cases thoroughly tested - Tests cover various retrieval configurations and scenarios ## Related Part of ongoing effort to improve test coverage for dataset service methods. Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:48 -05:00
yindo closed this issue 2026-02-21 20:50:48 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32131