[PR #28762] feat: complete test script of dataset retrieval #32171

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

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

State: closed
Merged: Yes


Add Comprehensive Unit Tests for Dataset Retrieval

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 a comprehensive test suite for the dataset retrieval functionality in api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py.

Fixes

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

What's Added

New Test File: api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py

  • 30 passing tests covering all retrieval methods and edge cases
  • 3 helper functions for standardized test data creation
  • Comprehensive documentation with detailed docstrings and inline comments

Test Coverage

The test suite validates the following core retrieval mechanisms:

  1. Vector Search (5 tests)

    • Basic semantic search functionality
    • Document ID filtering
    • Empty results handling
    • Metadata-based filtering
    • Score threshold filtering
  2. Keyword Search (3 tests)

    • Basic keyword matching
    • Special character handling and query escaping
    • Document filtering
  3. Hybrid Search (3 tests)

    • Vector + full-text search combination
    • Document deduplication with score retention
    • Weighted score merging algorithms
  4. Full-Text Search (1 test)

    • BM25-based text matching
  5. Score Merging Algorithms (4 tests)

    • Basic deduplication logic
    • Higher score retention
    • Empty list handling
    • Provider-specific handling
  6. Error Handling (3 tests)

    • Empty query validation
    • Non-existent dataset handling
    • Exception propagation
  7. Additional Features (11 tests)

    • Query escaping for special characters
    • Reranking integration
    • Top-K limiting
    • RetrievalMethod enum validation
    • Document model creation and equality

Key Features

  • Self-documenting code: Extensive docstrings and comments explain complex concepts
  • Helper functions: Reusable utilities for creating mock documents and side effects
  • Proper mocking strategy: Method-level mocking to avoid ThreadPoolExecutor complexity
  • TDD principles: All tests follow Arrange-Act-Assert pattern
  • No external dependencies: Tests run without database or API connections

Technical Details

  • Tests mock at the method level (embedding_search, keyword_search, full_text_index_search) rather than class level
  • ThreadPoolExecutor is mocked to run synchronously for deterministic testing
  • All tests are isolated and independent
  • Follows project conventions with proper type hints and error handling

Screenshots

Not applicable - this is a test-only change with no UI impact.

Test Results

# All 30 tests pass successfully
pytest api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py -v

====================================================== 30 passed in ~50s ======================================================

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 (comprehensive inline documentation in test file).
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

Additional Notes

  • The test file includes a comprehensive summary section at the end with statistics, design decisions, and maintenance notes
  • Helper functions are provided to reduce code duplication and improve test readability
  • All tests pass linting (Ruff) and type checking requirements
  • No changes to production code - this is purely additive testing

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28762 **State:** closed **Merged:** Yes --- # Add Comprehensive Unit Tests for Dataset Retrieval > [!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 a comprehensive test suite for the dataset retrieval functionality in `api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py`. ### Fixes Closes https://github.com/langgenius/dify/issues/28761 ### What's Added **New Test File**: `api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py` - **30 passing tests** covering all retrieval methods and edge cases - **3 helper functions** for standardized test data creation - **Comprehensive documentation** with detailed docstrings and inline comments ### Test Coverage The test suite validates the following core retrieval mechanisms: 1. **Vector Search (5 tests)** - Basic semantic search functionality - Document ID filtering - Empty results handling - Metadata-based filtering - Score threshold filtering 2. **Keyword Search (3 tests)** - Basic keyword matching - Special character handling and query escaping - Document filtering 3. **Hybrid Search (3 tests)** - Vector + full-text search combination - Document deduplication with score retention - Weighted score merging algorithms 4. **Full-Text Search (1 test)** - BM25-based text matching 5. **Score Merging Algorithms (4 tests)** - Basic deduplication logic - Higher score retention - Empty list handling - Provider-specific handling 6. **Error Handling (3 tests)** - Empty query validation - Non-existent dataset handling - Exception propagation 7. **Additional Features (11 tests)** - Query escaping for special characters - Reranking integration - Top-K limiting - RetrievalMethod enum validation - Document model creation and equality ### Key Features - **Self-documenting code**: Extensive docstrings and comments explain complex concepts - **Helper functions**: Reusable utilities for creating mock documents and side effects - **Proper mocking strategy**: Method-level mocking to avoid ThreadPoolExecutor complexity - **TDD principles**: All tests follow Arrange-Act-Assert pattern - **No external dependencies**: Tests run without database or API connections ### Technical Details - Tests mock at the method level (`embedding_search`, `keyword_search`, `full_text_index_search`) rather than class level - ThreadPoolExecutor is mocked to run synchronously for deterministic testing - All tests are isolated and independent - Follows project conventions with proper type hints and error handling ## Screenshots Not applicable - this is a test-only change with no UI impact. ## Test Results ```bash # All 30 tests pass successfully pytest api/tests/unit_tests/core/rag/retrieval/test_dataset_retrieval.py -v ====================================================== 30 passed in ~50s ====================================================== ``` ## 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 (comprehensive inline documentation in test file). - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods ## Additional Notes - The test file includes a comprehensive summary section at the end with statistics, design decisions, and maintenance notes - Helper functions are provided to reduce code duplication and improve test readability - All tests pass linting (Ruff) and type checking requirements - No changes to production code - this is purely additive testing 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#32171