Fix CI tests: Improve test isolation and mock handling

- Add numpy to requirements-nodes.txt
- Create conftest.py for test configuration
- Fix test_numpy_subprocess.py to import real numpy before any mocking
- Fix test_media.py to import real numpy early
- Fix test_image_node_sanitization.py to use MockTensor instead of torch.zeros
This commit is contained in:
AEmotionStudio
2026-01-21 00:46:02 -08:00
parent 73b2bfe5b1
commit 87112b8b2a
4 changed files with 90 additions and 5 deletions
+52
View File
@@ -0,0 +1,52 @@
"""
Pytest configuration file for tests.
This module handles test isolation by ensuring that real packages are imported
before any mocking occurs, and by providing cleanup fixtures.
"""
import sys
import os
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Store references to real modules before any mocking
# This ensures tests that need real numpy/PIL can use them
_real_numpy = None
_real_PIL = None
def pytest_configure(config):
"""Called after command line options have been parsed and all plugins loaded."""
global _real_numpy, _real_PIL
# Import real modules and store references
try:
import numpy
_real_numpy = numpy
except ImportError:
pass
try:
import PIL
import PIL.Image
import PIL.PngImagePlugin
_real_PIL = PIL
except ImportError:
pass
def get_real_numpy():
"""Get the real numpy module, not a mock."""
if _real_numpy is None:
import numpy
return numpy
return _real_numpy
def get_real_PIL():
"""Get the real PIL module, not a mock."""
if _real_PIL is None:
import PIL
return PIL
return _real_PIL
+23 -3
View File
@@ -50,9 +50,29 @@ class TestDiscordImageNodeOptimization(unittest.TestCase):
self.github_token = "ghp_sensitive12345"
def test_save_images_sanitization(self):
# Create a mock image (needs to be proper shape for the node)
import torch
mock_image = torch.zeros((1, 64, 64, 3))
# Create a mock image tensor using numpy (torch not available in CI)
# The image_node iterates over images and accesses shape, so we need
# an object that supports iteration and has proper shape
import numpy as np
# Create a simple class that mimics torch.Tensor behavior for the node
class MockTensor:
def __init__(self, data):
self._data = data
self.shape = data.shape
def __len__(self):
return len(self._data)
def __getitem__(self, idx):
return self._data[idx]
def __iter__(self):
return iter(self._data)
# Create a 1x64x64x3 "image batch" using numpy
image_data = np.zeros((1, 64, 64, 3), dtype=np.float32)
mock_image = MockTensor(image_data)
# Create prompt and extra_pnginfo with sensitive data
prompt = {
+5 -1
View File
@@ -3,7 +3,11 @@ import sys
import unittest
from unittest.mock import MagicMock, patch
# Create a dummy torch module
# IMPORTANT: Import real numpy FIRST before any mocking
# This ensures test_power_of_two_math uses real numpy
import numpy as np
# Create a dummy torch module (torch is not installed in CI)
mock_torch = MagicMock()
sys.modules["torch"] = mock_torch
sys.modules["folder_paths"] = MagicMock()
+10 -1
View File
@@ -1,9 +1,18 @@
import unittest
import numpy as np
import subprocess
import sys
import os
# Ensure we get real numpy, not a mock from other test files
# Remove any mocked numpy before importing
if 'numpy' in sys.modules and hasattr(sys.modules['numpy'], '_mock_name'):
del sys.modules['numpy']
import numpy as np
# Verify numpy is real
assert hasattr(np, 'arange'), "numpy.arange not found - numpy may be mocked"
class TestNumpyToSubprocess(unittest.TestCase):
"""
Verify that subprocess.Popen.stdin.write accepts numpy arrays directly.