forked from blib-la/runpod-worker-comfy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added unit tests for everthing, refactored the code to make it …
…better testable, added test images
- Loading branch information
1 parent
dc92334
commit a7492ec
Showing
6 changed files
with
213 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
venv | ||
.env | ||
data | ||
models | ||
models | ||
simulated_uploaded | ||
__pycache__ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
import unittest | ||
from unittest.mock import patch, MagicMock, mock_open, Mock | ||
import sys | ||
import os | ||
import json | ||
|
||
# Make sure that "src" is known and can be used to import rp_handler.py | ||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'src'))) | ||
from src import rp_handler | ||
|
||
# Local folder for test resources | ||
RUNPOD_WORKER_COMFY_TEST_RESOURCES_IMAGES = "./test_resources/images" | ||
|
||
class TestRunpodWorkerComfy(unittest.TestCase): | ||
@patch('rp_handler.requests.get') | ||
def test_check_server_server_up(self, mock_requests): | ||
mock_response = MagicMock() | ||
mock_response.status_code = 200 | ||
mock_requests.return_value = mock_response | ||
|
||
result = rp_handler.check_server('http://127.0.0.1:8188', 1, 50) | ||
self.assertTrue(result) | ||
|
||
@patch('rp_handler.requests.get') | ||
def test_check_server_server_down(self, mock_requests): | ||
mock_requests.get.side_effect = rp_handler.requests.RequestException() | ||
result = rp_handler.check_server('http://127.0.0.1:8188', 1, 50) | ||
self.assertFalse(result) | ||
|
||
@patch('rp_handler.urllib.request.urlopen') | ||
def test_queue_prompt(self, mock_urlopen): | ||
mock_response = MagicMock() | ||
mock_response.read.return_value = json.dumps({"prompt_id": "123"}).encode() | ||
mock_urlopen.return_value = mock_response | ||
result = rp_handler.queue_prompt({"prompt": "test"}) | ||
self.assertEqual(result, {"prompt_id": "123"}) | ||
|
||
@patch('rp_handler.urllib.request.urlopen') | ||
def test_get_history(self, mock_urlopen): | ||
# Mock response data as a JSON string | ||
mock_response_data = json.dumps({"key": "value"}).encode('utf-8') | ||
|
||
# Define a mock response function for `read` | ||
def mock_read(): | ||
return mock_response_data | ||
|
||
# Create a mock response object | ||
mock_response = Mock() | ||
mock_response.read = mock_read | ||
|
||
# Mock the __enter__ and __exit__ methods to support the context manager | ||
mock_response.__enter__ = lambda s: s | ||
mock_response.__exit__ = Mock() | ||
|
||
# Set the return value of the urlopen mock | ||
mock_urlopen.return_value = mock_response | ||
|
||
# Call the function under test | ||
result = rp_handler.get_history("123") | ||
|
||
# Assertions | ||
self.assertEqual(result, {"key": "value"}) | ||
mock_urlopen.assert_called_with("http://127.0.0.1:8188/history/123") | ||
|
||
@patch('builtins.open', new_callable=mock_open, read_data=b'test') | ||
def test_base64_encode(self, mock_file): | ||
result = rp_handler.base64_encode("dummy_path") | ||
self.assertTrue(result.startswith("data:image/png;base64,")) | ||
|
||
@patch('rp_handler.os.path.exists') | ||
@patch('rp_handler.rp_upload.upload_image') | ||
@patch.dict(os.environ, {'COMFY_OUTPUT_PATH': RUNPOD_WORKER_COMFY_TEST_RESOURCES_IMAGES}) | ||
def test_bucket_endpoint_not_configured(self, mock_upload_image, mock_exists): | ||
mock_exists.return_value = True | ||
mock_upload_image.return_value = 'simulated_uploaded/image.png' | ||
|
||
outputs = {'node_id': {'images': [{'filename': 'ComfyUI_00001_.png'}]}} | ||
job_id = '123' | ||
|
||
result = rp_handler.process_output_images(outputs, job_id) | ||
|
||
self.assertEqual(result['status'], 'success') | ||
self.assertTrue(result['message'].startswith("data:image/png;base64,")) | ||
|
||
@patch('rp_handler.os.path.exists') | ||
@patch('rp_handler.rp_upload.upload_image') | ||
@patch.dict(os.environ, {'COMFY_OUTPUT_PATH': RUNPOD_WORKER_COMFY_TEST_RESOURCES_IMAGES, 'BUCKET_ENDPOINT_URL': 'http://example.com'}) | ||
def test_bucket_endpoint_configured(self, mock_upload_image, mock_exists): | ||
# Mock the os.path.exists to return True, simulating that the image exists | ||
mock_exists.return_value = True | ||
|
||
# Mock the rp_upload.upload_image to return a simulated URL | ||
mock_upload_image.return_value = 'http://example.com/uploaded/image.png' | ||
|
||
# Define the outputs and job_id for the test | ||
outputs = {'node_id': {'images': [{'filename': 'ComfyUI_00001_.png'}]}} | ||
job_id = '123' | ||
|
||
# Call the function under test | ||
result = rp_handler.process_output_images(outputs, job_id) | ||
|
||
# Assertions | ||
self.assertEqual(result['status'], 'success') | ||
self.assertEqual(result['message'], 'http://example.com/uploaded/image.png') | ||
mock_upload_image.assert_called_once_with(job_id, './test_resources/images/ComfyUI_00001_.png') | ||
|
||
|
||
@patch('rp_handler.os.path.exists') | ||
@patch('rp_handler.rp_upload.upload_image') | ||
@patch.dict(os.environ, { | ||
'COMFY_OUTPUT_PATH': RUNPOD_WORKER_COMFY_TEST_RESOURCES_IMAGES, | ||
'BUCKET_ENDPOINT_URL': 'http://example.com', | ||
'BUCKET_ACCESS_KEY_ID': '', | ||
'BUCKET_SECRET_ACCESS_KEY': '' | ||
}) | ||
def test_bucket_image_upload_fails_env_vars_wrong_or_missing(self, mock_upload_image, mock_exists): | ||
# Simulate the file existing in the output path | ||
mock_exists.return_value = True | ||
|
||
# When AWS credentials are wrong or missing, upload_image should return 'simulated_uploaded/...' | ||
mock_upload_image.return_value = 'simulated_uploaded/image.png' | ||
|
||
outputs = {'node_id': {'images': [{'filename': 'ComfyUI_00001_.png'}]}} | ||
job_id = '123' | ||
|
||
result = rp_handler.process_output_images(outputs, job_id) | ||
|
||
# Check if the image was saved to the 'simulated_uploaded' directory | ||
self.assertIn('simulated_uploaded', result['message']) | ||
self.assertEqual(result['status'], 'success') |