[PR #28923] Feat/ implement test script of content moderation #32238

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

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

State: closed
Merged: Yes


Pull Request: Comprehensive Test Suite for Content Moderation

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 implements a comprehensive test suite for the content moderation system in api/tests/unit_tests/core/moderation/test_content_moderation.py.

Fixes

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

What's Included

77 test cases covering all aspects of content moderation:

  • Keywords Moderation (19 tests): Configuration validation, input/output moderation, case-insensitive matching, partial matching, disabled configs, empty keyword filtering, multiple inputs, and error handling

  • OpenAI Moderation (11 tests): Configuration validation, input/output moderation with API integration, query inclusion, disabled configs, ModelManager parameter verification, and error handling

  • Advanced Keywords Tests (13 tests): Unicode/international character support, special regex character handling, whitespace variations, boundary conditions (10000 char limit, 100 row limit), nested dictionaries, numeric/boolean values, empty strings, and very long text performance

  • Advanced OpenAI Tests (5 tests): Timeout handling, rate limit errors, multiple input fields, empty text handling, and model instance caching verification

  • Integration Tests (10 tests): InputModeration logic patterns, OutputModeration logic patterns, ModerationFactory delegation, and rule structure validation

  • Base Classes & Enums (6 tests): ModerationAction enum values, ModerationInputsResult/OutputsResult defaults and custom values, ModerationError exception handling

  • Preset Management (6 tests): Keyword and OpenAI preset responses for inputs/outputs, length validation, and different responses for inputs vs outputs

  • Configuration Edge Cases (5 tests): Invalid config types, both inputs/outputs disabled, preset response boundary conditions, and empty preset responses

  • Concurrent Scenarios (3 tests): Multiple keywords in single input, keyword position detection, and case variation handling

Key Features

Comprehensive coverage of all moderation types (Keywords, OpenAI)
Edge case testing including Unicode, special characters, and boundary conditions
Error handling validation for timeouts, rate limits, and invalid configurations
Performance testing with very long texts (60,000+ characters)
Well-documented with detailed docstrings and inline comments
Ruff compliant - passes all linting and formatting checks
All tests passing - 77/77 tests pass successfully

Testing Approach

  • Uses pytest with fixtures for reusable test configurations
  • Employs unittest.mock to isolate units and avoid external dependencies
  • Follows Arrange-Act-Assert pattern for clarity
  • Includes both positive and negative test cases
  • Tests real-world scenarios (international content, nested data, type conversions)

Screenshots

Not applicable - this is a backend test suite implementation.

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

Test Results

======================================================= 77 passed in 35.83s ========================================================

All tests pass successfully with comprehensive coverage of:

  • Input moderation workflows
  • Output moderation workflows
  • OpenAI API integration
  • Custom keyword filtering
  • Configuration validation
  • Preset response management
  • Error handling and edge cases

Code Quality

  • Ruff lint: All checks passed
  • Ruff format: Code properly formatted
  • Type hints: Proper type annotations throughout
  • Documentation: Comprehensive docstrings for all test classes and methods

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

**Original Pull Request:** https://github.com/langgenius/dify/pull/28923 **State:** closed **Merged:** Yes --- # Pull Request: Comprehensive Test Suite for Content Moderation > [!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 implements a comprehensive test suite for the content moderation system in `api/tests/unit_tests/core/moderation/test_content_moderation.py`. ### Fixes Closes https://github.com/langgenius/dify/issues/28875 ### What's Included **77 test cases** covering all aspects of content moderation: - **Keywords Moderation (19 tests)**: Configuration validation, input/output moderation, case-insensitive matching, partial matching, disabled configs, empty keyword filtering, multiple inputs, and error handling - **OpenAI Moderation (11 tests)**: Configuration validation, input/output moderation with API integration, query inclusion, disabled configs, ModelManager parameter verification, and error handling - **Advanced Keywords Tests (13 tests)**: Unicode/international character support, special regex character handling, whitespace variations, boundary conditions (10000 char limit, 100 row limit), nested dictionaries, numeric/boolean values, empty strings, and very long text performance - **Advanced OpenAI Tests (5 tests)**: Timeout handling, rate limit errors, multiple input fields, empty text handling, and model instance caching verification - **Integration Tests (10 tests)**: InputModeration logic patterns, OutputModeration logic patterns, ModerationFactory delegation, and rule structure validation - **Base Classes & Enums (6 tests)**: ModerationAction enum values, ModerationInputsResult/OutputsResult defaults and custom values, ModerationError exception handling - **Preset Management (6 tests)**: Keyword and OpenAI preset responses for inputs/outputs, length validation, and different responses for inputs vs outputs - **Configuration Edge Cases (5 tests)**: Invalid config types, both inputs/outputs disabled, preset response boundary conditions, and empty preset responses - **Concurrent Scenarios (3 tests)**: Multiple keywords in single input, keyword position detection, and case variation handling ### Key Features ✅ **Comprehensive coverage** of all moderation types (Keywords, OpenAI) ✅ **Edge case testing** including Unicode, special characters, and boundary conditions ✅ **Error handling** validation for timeouts, rate limits, and invalid configurations ✅ **Performance testing** with very long texts (60,000+ characters) ✅ **Well-documented** with detailed docstrings and inline comments ✅ **Ruff compliant** - passes all linting and formatting checks ✅ **All tests passing** - 77/77 tests pass successfully ### Testing Approach - Uses `pytest` with fixtures for reusable test configurations - Employs `unittest.mock` to isolate units and avoid external dependencies - Follows Arrange-Act-Assert pattern for clarity - Includes both positive and negative test cases - Tests real-world scenarios (international content, nested data, type conversions) ## Screenshots Not applicable - this is a backend test suite implementation. ## 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 ## Test Results ```bash ======================================================= 77 passed in 35.83s ======================================================== ``` All tests pass successfully with comprehensive coverage of: - Input moderation workflows - Output moderation workflows - OpenAI API integration - Custom keyword filtering - Configuration validation - Preset response management - Error handling and edge cases ## Code Quality - ✅ **Ruff lint**: All checks passed - ✅ **Ruff format**: Code properly formatted - ✅ **Type hints**: Proper type annotations throughout - ✅ **Documentation**: Comprehensive docstrings for all test classes and methods Contribution by Gittensor, learn more at https://gittensor.io/
yindo added the pull-request label 2026-02-21 20:51:01 -05:00
yindo closed this issue 2026-02-21 20:51:01 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#32238