[PR #28539] feat: add comprehensive unit tests for dataset service retrieval/list… #32079

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

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

State: closed
Merged: Yes


Description

This PR adds comprehensive unit test coverage for DatasetService retrieval and list methods, significantly improving test coverage for dataset querying and filtering functionality.

Test Coverage Added

get_datasets Method Tests (10 test cases)

Basic Functionality:

  • Basic pagination without user or filters
  • Search functionality with keyword filtering
  • Tag filtering (with tags, empty tags)

Permission-Based Filtering:

  • Without user (only shows ALL_TEAM datasets)
  • OWNER with include_all flag (sees all datasets)
  • NORMAL user with ONLY_ME permission (sees own datasets)
  • NORMAL user with ALL_TEAM permission (sees team datasets)
  • NORMAL user with PARTIAL_TEAM permission (sees permitted datasets)
  • DATASET_OPERATOR with permissions (sees only permitted datasets)
  • DATASET_OPERATOR without permissions (returns empty result)

get_dataset Method Tests (2 test cases)

  • Successful single dataset retrieval
  • Dataset not found (returns None)

get_datasets_by_ids Method Tests (3 test cases)

  • Successful bulk retrieval of multiple datasets
  • Empty list handling (returns empty result)
  • None list handling (returns empty result)

get_process_rules Method Tests (2 test cases)

  • Retrieval with existing process rule
  • Retrieval without existing rule (returns default rules)

get_dataset_queries Method Tests (2 test cases)

  • Successful retrieval of dataset queries with pagination
  • Empty results when no queries exist

get_related_apps Method Tests (2 test cases)

  • Successful retrieval of apps using the dataset
  • Empty results when no related apps exist

Testing Approach

  • Follows TDD principles with Arrange-Act-Assert structure
  • Uses factory pattern for test data creation (consistent with existing tests)
  • Comprehensive mocking of dependencies (database, pagination, tag service)
  • Tests cover both success paths and error conditions
  • Follows project conventions from existing test files:
    • test_dataset_service_update_dataset.py
    • test_dataset_service_delete_dataset.py
    • test_dataset_permission.py

Test Statistics

  • Total test cases: 21 test cases
  • New test file: test_dataset_service_retrieval.py
  • Lines of test code: ~767 lines
  • Coverage: All major scenarios including edge cases

Related Work

This complements the existing test coverage:

  • test_dataset_models.py - Dataset model tests (PR #28512)
  • test_dataset_service_update_dataset.py - Update operations
  • test_dataset_service_delete_dataset.py - Delete operations
  • test_dataset_permission.py - Permission checks

Checklist

  • Tests follow TDD principles
  • All tests pass locally
  • Code follows project style guidelines
  • No linting errors
  • Test coverage is comprehensive
  • Tests use proper mocking and fixtures
  • All edge cases covered
  • Permission logic thoroughly tested

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28539 **State:** closed **Merged:** Yes --- ## Description This PR adds comprehensive unit test coverage for DatasetService retrieval and list methods, significantly improving test coverage for dataset querying and filtering functionality. ## Test Coverage Added ### `get_datasets` Method Tests (10 test cases) **Basic Functionality:** - ✅ Basic pagination without user or filters - ✅ Search functionality with keyword filtering - ✅ Tag filtering (with tags, empty tags) **Permission-Based Filtering:** - ✅ Without user (only shows ALL_TEAM datasets) - ✅ OWNER with include_all flag (sees all datasets) - ✅ NORMAL user with ONLY_ME permission (sees own datasets) - ✅ NORMAL user with ALL_TEAM permission (sees team datasets) - ✅ NORMAL user with PARTIAL_TEAM permission (sees permitted datasets) - ✅ DATASET_OPERATOR with permissions (sees only permitted datasets) - ✅ DATASET_OPERATOR without permissions (returns empty result) ### `get_dataset` Method Tests (2 test cases) - ✅ Successful single dataset retrieval - ✅ Dataset not found (returns None) ### `get_datasets_by_ids` Method Tests (3 test cases) - ✅ Successful bulk retrieval of multiple datasets - ✅ Empty list handling (returns empty result) - ✅ None list handling (returns empty result) ### `get_process_rules` Method Tests (2 test cases) - ✅ Retrieval with existing process rule - ✅ Retrieval without existing rule (returns default rules) ### `get_dataset_queries` Method Tests (2 test cases) - ✅ Successful retrieval of dataset queries with pagination - ✅ Empty results when no queries exist ### `get_related_apps` Method Tests (2 test cases) - ✅ Successful retrieval of apps using the dataset - ✅ Empty results when no related apps exist ## Testing Approach - Follows **TDD principles** with Arrange-Act-Assert structure - Uses **factory pattern** for test data creation (consistent with existing tests) - Comprehensive **mocking** of dependencies (database, pagination, tag service) - Tests cover both **success paths** and **error conditions** - Follows project conventions from existing test files: - `test_dataset_service_update_dataset.py` - `test_dataset_service_delete_dataset.py` - `test_dataset_permission.py` ## Test Statistics - **Total test cases**: 21 test cases - **New test file**: `test_dataset_service_retrieval.py` - **Lines of test code**: ~767 lines - **Coverage**: All major scenarios including edge cases ## Related Work This complements the existing test coverage: - ✅ `test_dataset_models.py` - Dataset model tests (PR #28512) - ✅ `test_dataset_service_update_dataset.py` - Update operations - ✅ `test_dataset_service_delete_dataset.py` - Delete operations - ✅ `test_dataset_permission.py` - Permission checks ## Checklist - [x] Tests follow TDD principles - [x] All tests pass locally - [x] Code follows project style guidelines - [x] No linting errors - [x] Test coverage is comprehensive - [x] Tests use proper mocking and fixtures - [x] All edge cases covered - [x] Permission logic thoroughly tested Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:50:42 -05:00
yindo closed this issue 2026-02-21 20:50:43 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32079