[PR #22663] fix: resolve Redis mock import error in test configuration #30009

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

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

State: closed
Merged: Yes


Fix Redis mock import error in test configuration

Description

This PR fixes the AttributeError: module 'extensions' has no attribute 'ext_redis' error that occurs when running unit tests. The issue was caused by incorrect module path resolution in the test environment.

Root Cause

The original code used string-based patching:

redis_patcher = patch("extensions.ext_redis.redis_client", redis_mock)

This approach failed when the Python interpreter couldn't resolve the extensions.ext_redis module path correctly during test execution.

Solution

  1. Add API directory to Python path: Ensures modules can be imported correctly regardless of where tests are run from
  2. Use patch.object instead of string-based patch: More reliable approach that works with the actual imported module object
# Add the API directory to Python path to ensure proper imports
import sys
sys.path.insert(0, PROJECT_DIR)

# Use patch.object for more reliable mocking
from extensions import ext_redis
redis_patcher = patch.object(ext_redis, "redis_client", redis_mock)

Impact

  • All 861 unit tests pass
  • Fixes test execution issues in various environments
  • No impact on production code
  • Improves test reliability and maintainability

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/22663 **State:** closed **Merged:** Yes --- # Fix Redis mock import error in test configuration ## Description This PR fixes the `AttributeError: module 'extensions' has no attribute 'ext_redis'` error that occurs when running unit tests. The issue was caused by incorrect module path resolution in the test environment. ## Root Cause The original code used string-based patching: ```python redis_patcher = patch("extensions.ext_redis.redis_client", redis_mock) ``` This approach failed when the Python interpreter couldn't resolve the `extensions.ext_redis` module path correctly during test execution. ## Solution 1. **Add API directory to Python path**: Ensures modules can be imported correctly regardless of where tests are run from 2. **Use `patch.object` instead of string-based patch**: More reliable approach that works with the actual imported module object ```python # Add the API directory to Python path to ensure proper imports import sys sys.path.insert(0, PROJECT_DIR) # Use patch.object for more reliable mocking from extensions import ext_redis redis_patcher = patch.object(ext_redis, "redis_client", redis_mock) ``` ## Impact - All 861 unit tests pass - Fixes test execution issues in various environments - No impact on production code - Improves test reliability and maintainability ## 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:46:40 -05:00
yindo closed this issue 2026-02-21 20:46:40 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#30009