[PR #28724] feat: Add comprehensive unit tests for DatasetCollectionBindingService (dataset collection binding methods) #32154

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

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

State: closed
Merged: Yes


Summary

Adds comprehensive unit test coverage for DatasetCollectionBindingService to ensure reliability and correctness of dataset collection binding operations.

Fix: #28723

Test Coverage

Collection Binding Retrieval and Creation

  • get_dataset_collection_binding - Retrieve or create bindings by provider, model, and type
    • Retrieves existing bindings without creating duplicates
    • Creates new bindings when none exist with generated collection names
    • Handles different collection types (dataset, custom types)
    • Uses default collection type ("dataset") when not specified
    • Supports different provider/model combinations
    • Properly generates collection names using Dataset.gen_collection_name_by_id
    • Commits new bindings to database

Collection Binding Retrieval by ID and Type

  • get_dataset_collection_binding_by_id_and_type - Retrieve specific bindings by ID and type
    • Successfully retrieves existing bindings by ID and type
    • Raises ValueError("Dataset collection binding not found") when binding doesn't exist
    • Handles different collection types correctly
    • Uses default collection type ("dataset") when not specified
    • Properly filters by both ID and type (doesn't return bindings with wrong type)

Test Structure

  • Test Data Factory: DatasetCollectionBindingTestDataFactory provides reusable mock objects for bindings and datasets
  • Comprehensive Coverage: Each method tested with success scenarios, error conditions, and edge cases
  • Proper Mocking: All dependencies properly mocked (db.session.query, query chain methods, Dataset.gen_collection_name_by_id, uuid.uuid4)
  • Follows Patterns: Test structure follows existing patterns from other service test files
  • Detailed Documentation: Extensive docstrings and comments (671+ lines) for clarity and maintainability

Implementation Details

get_dataset_collection_binding

  • Queries database for existing binding by provider_name, model_name, and collection_type
  • Orders results by created_at and returns first match
  • If no binding exists, creates new one with:
    • Provided provider_name and model_name
    • Generated collection_name using Dataset.gen_collection_name_by_id(uuid4())
    • Provided collection_type
  • Adds new binding to session and commits transaction
  • Returns binding (either existing or newly created)

get_dataset_collection_binding_by_id_and_type

  • Queries database for binding by collection_binding_id and collection_type
  • Orders results by created_at and returns first match
  • Raises ValueError if no binding found (does NOT create new binding)
  • Returns found binding

Files Changed

  • api/tests/unit_tests/services/dataset_collection_binding.py - New test file (671 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
  • Database transaction handling verified (add, commit operations)

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28724 **State:** closed **Merged:** Yes --- ## Summary Adds comprehensive unit test coverage for `DatasetCollectionBindingService` to ensure reliability and correctness of dataset collection binding operations. Fix: #28723 ## Test Coverage ### Collection Binding Retrieval and Creation - ✅ **get_dataset_collection_binding** - Retrieve or create bindings by provider, model, and type - Retrieves existing bindings without creating duplicates - Creates new bindings when none exist with generated collection names - Handles different collection types (dataset, custom types) - Uses default collection type ("dataset") when not specified - Supports different provider/model combinations - Properly generates collection names using `Dataset.gen_collection_name_by_id` - Commits new bindings to database ### Collection Binding Retrieval by ID and Type - ✅ **get_dataset_collection_binding_by_id_and_type** - Retrieve specific bindings by ID and type - Successfully retrieves existing bindings by ID and type - Raises `ValueError("Dataset collection binding not found")` when binding doesn't exist - Handles different collection types correctly - Uses default collection type ("dataset") when not specified - Properly filters by both ID and type (doesn't return bindings with wrong type) ## Test Structure - **Test Data Factory**: `DatasetCollectionBindingTestDataFactory` provides reusable mock objects for bindings and datasets - **Comprehensive Coverage**: Each method tested with success scenarios, error conditions, and edge cases - **Proper Mocking**: All dependencies properly mocked (`db.session.query`, query chain methods, `Dataset.gen_collection_name_by_id`, `uuid.uuid4`) - **Follows Patterns**: Test structure follows existing patterns from other service test files - **Detailed Documentation**: Extensive docstrings and comments (671+ lines) for clarity and maintainability ## Implementation Details ### get_dataset_collection_binding - Queries database for existing binding by `provider_name`, `model_name`, and `collection_type` - Orders results by `created_at` and returns first match - If no binding exists, creates new one with: - Provided `provider_name` and `model_name` - Generated `collection_name` using `Dataset.gen_collection_name_by_id(uuid4())` - Provided `collection_type` - Adds new binding to session and commits transaction - Returns binding (either existing or newly created) ### get_dataset_collection_binding_by_id_and_type - Queries database for binding by `collection_binding_id` and `collection_type` - Orders results by `created_at` and returns first match - Raises `ValueError` if no binding found (does NOT create new binding) - Returns found binding ## Files Changed - `api/tests/unit_tests/services/dataset_collection_binding.py` - New test file (671 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 - Database transaction handling verified (add, commit operations) Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:51 -05:00
yindo closed this issue 2026-02-21 20:50:51 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32154