[PR #28872] test: add comprehensive unit tests for ExternalDatasetService #32221

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

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

State: closed
Merged: Yes


  • Add 94 unit tests covering all 15 methods across 15 test classes
  • Test scenarios include:
    • Get External Knowledge APIs (10 tests): pagination, search, empty results, large datasets
    • Validate API List (8 tests): endpoint/API key validation, empty/none handling
    • Create External Knowledge API (8 tests): full creation, minimal fields, Unicode, JSON serialization
    • Check Endpoint and API Key (20 tests): HTTPS/HTTP, URL validation, connection errors, HTTP status codes
    • Get External Knowledge API (2 tests): successful retrieval, not found
    • Update External Knowledge API (5 tests): full update, hidden key preservation, partial updates
    • Delete External Knowledge API (3 tests): successful deletion, not found, tenant mismatch
    • API Use Check (3 tests): in use (single/multiple), not in use
    • Get External Knowledge Binding (2 tests): successful retrieval, not found
    • Document Create Args Validate (5 tests): all params, missing required, optional params
    • Process External API (9 tests): GET/POST/PUT/DELETE/PATCH/HEAD, invalid method, file uploads
    • Assembling Headers (9 tests): Bearer/Basic/Custom auth, custom headers, error handling
    • Get External Knowledge API Settings (1 test): successful parsing
    • Create External Dataset (5 tests): full creation, duplicate name, missing IDs
    • Fetch External Knowledge Retrieval (4 tests): success with results, empty, score threshold, non-200 status
  • Use factory pattern for mock data creation (5 factory methods)
  • All tests follow Arrange-Act-Assert structure
  • Comprehensive error handling coverage
  • Multi-tenancy validation
  • HTTP method coverage (GET, POST, PUT, DELETE, PATCH, HEAD)
  • Authorization types (Bearer, Basic, Custom)
  • Edge cases and boundary conditions
  • All 94 tests passing (5.44s)
  • First tests for ExternalDatasetService (previously had zero coverage)

Fixes #28871

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

Screenshots

Before After
... ...
diffy_unit_test

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.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods
**Original Pull Request:** https://github.com/langgenius/dify/pull/28872 **State:** closed **Merged:** Yes --- - Add 94 unit tests covering all 15 methods across 15 test classes - Test scenarios include: * Get External Knowledge APIs (10 tests): pagination, search, empty results, large datasets * Validate API List (8 tests): endpoint/API key validation, empty/none handling * Create External Knowledge API (8 tests): full creation, minimal fields, Unicode, JSON serialization * Check Endpoint and API Key (20 tests): HTTPS/HTTP, URL validation, connection errors, HTTP status codes * Get External Knowledge API (2 tests): successful retrieval, not found * Update External Knowledge API (5 tests): full update, hidden key preservation, partial updates * Delete External Knowledge API (3 tests): successful deletion, not found, tenant mismatch * API Use Check (3 tests): in use (single/multiple), not in use * Get External Knowledge Binding (2 tests): successful retrieval, not found * Document Create Args Validate (5 tests): all params, missing required, optional params * Process External API (9 tests): GET/POST/PUT/DELETE/PATCH/HEAD, invalid method, file uploads * Assembling Headers (9 tests): Bearer/Basic/Custom auth, custom headers, error handling * Get External Knowledge API Settings (1 test): successful parsing * Create External Dataset (5 tests): full creation, duplicate name, missing IDs * Fetch External Knowledge Retrieval (4 tests): success with results, empty, score threshold, non-200 status - Use factory pattern for mock data creation (5 factory methods) - All tests follow Arrange-Act-Assert structure - Comprehensive error handling coverage - Multi-tenancy validation - HTTP method coverage (GET, POST, PUT, DELETE, PATCH, HEAD) - Authorization types (Bearer, Basic, Custom) - Edge cases and boundary conditions - All 94 tests passing (5.44s) - First tests for ExternalDatasetService (previously had zero coverage) Fixes #28871 > [!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 <!-- Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. --> ## Screenshots | Before | After | |--------|-------| | ... | ... | <img width="824" height="664" alt="diffy_unit_test" src="https://github.com/user-attachments/assets/46b9d9f9-5b69-4a7b-85dd-9e8d866975bc" /> ## 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. - [x] I ran `dev/reformat`(backend) and `cd web && npx lint-staged`(frontend) to appease the lint gods
yindo added the pull-request label 2026-02-21 20:50:59 -05:00
yindo closed this issue 2026-02-21 20:50:59 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32221