mirror of
https://git.datalinker.icu/comfyanonymous/ComfyUI
synced 2026-11-12 11:46:37 +08:00
Fix tests.
This commit is contained in:
parent
e37dd27efb
commit
9d67d9618d
@ -1,6 +0,0 @@
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add the project root directory to the Python path
|
||||
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
|
||||
sys.path.insert(0, project_root)
|
||||
@ -2,26 +2,30 @@ import pytest
|
||||
from aiohttp import web
|
||||
from unittest.mock import MagicMock, patch
|
||||
from api_server.routes.internal.internal_routes import InternalRoutes
|
||||
from api_server.services.file_service import FileService
|
||||
from folder_paths import models_dir, user_directory, output_directory
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def internal_routes():
|
||||
print("Creating InternalRoutes instance")
|
||||
return InternalRoutes()
|
||||
|
||||
@pytest.fixture
|
||||
async def client(aiohttp_client, internal_routes):
|
||||
app = internal_routes.get_app()
|
||||
return await aiohttp_client(app)
|
||||
def aiohttp_client_factory(aiohttp_client, internal_routes):
|
||||
async def _get_client():
|
||||
app = internal_routes.get_app()
|
||||
return await aiohttp_client(app)
|
||||
return _get_client
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_files_valid_directory(client, internal_routes):
|
||||
async def test_list_files_valid_directory(aiohttp_client_factory, internal_routes):
|
||||
mock_file_list = [
|
||||
{"name": "file1.txt", "path": "file1.txt", "type": "file", "size": 100},
|
||||
{"name": "dir1", "path": "dir1", "type": "directory"}
|
||||
]
|
||||
internal_routes.file_service.list_files = MagicMock(return_value=mock_file_list)
|
||||
|
||||
client = await aiohttp_client_factory()
|
||||
resp = await client.get('/files?directory=models')
|
||||
assert resp.status == 200
|
||||
data = await resp.json()
|
||||
@ -30,9 +34,9 @@ async def test_list_files_valid_directory(client, internal_routes):
|
||||
assert data['files'] == mock_file_list
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_files_invalid_directory(client, internal_routes):
|
||||
async def test_list_files_invalid_directory(aiohttp_client_factory, internal_routes):
|
||||
internal_routes.file_service.list_files = MagicMock(side_effect=ValueError("Invalid directory key"))
|
||||
|
||||
client = await aiohttp_client_factory()
|
||||
resp = await client.get('/files?directory=invalid')
|
||||
assert resp.status == 400
|
||||
data = await resp.json()
|
||||
@ -40,9 +44,9 @@ async def test_list_files_invalid_directory(client, internal_routes):
|
||||
assert data['error'] == "Invalid directory key"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_files_exception(client, internal_routes):
|
||||
async def test_list_files_exception(aiohttp_client_factory, internal_routes):
|
||||
internal_routes.file_service.list_files = MagicMock(side_effect=Exception("Unexpected error"))
|
||||
|
||||
client = await aiohttp_client_factory()
|
||||
resp = await client.get('/files?directory=models')
|
||||
assert resp.status == 500
|
||||
data = await resp.json()
|
||||
@ -50,25 +54,16 @@ async def test_list_files_exception(client, internal_routes):
|
||||
assert data['error'] == "Unexpected error"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_list_files_no_directory_param(client, internal_routes):
|
||||
async def test_list_files_no_directory_param(aiohttp_client_factory, internal_routes):
|
||||
mock_file_list = []
|
||||
internal_routes.file_service.list_files = MagicMock(return_value=mock_file_list)
|
||||
|
||||
client = await aiohttp_client_factory()
|
||||
resp = await client.get('/files')
|
||||
assert resp.status == 200
|
||||
data = await resp.json()
|
||||
assert 'files' in data
|
||||
assert len(data['files']) == 0
|
||||
|
||||
@patch('server.routes.internal_routes.FileService')
|
||||
def test_file_service_initialization(mock_file_service):
|
||||
InternalRoutes()
|
||||
mock_file_service.assert_called_once_with({
|
||||
"models": models_dir,
|
||||
"user": user_directory,
|
||||
"output": output_directory
|
||||
})
|
||||
|
||||
def test_setup_routes(internal_routes):
|
||||
internal_routes.setup_routes()
|
||||
routes = internal_routes.routes
|
||||
@ -84,11 +79,42 @@ def test_get_app_reuse(internal_routes):
|
||||
app2 = internal_routes.get_app()
|
||||
assert app1 is app2
|
||||
|
||||
# Additional test to check if routes are added to the application
|
||||
@pytest.mark.asyncio
|
||||
async def test_routes_added_to_app(aiohttp_client, internal_routes):
|
||||
app = internal_routes.get_app()
|
||||
client = await aiohttp_client(app)
|
||||
async def test_routes_added_to_app(aiohttp_client_factory, internal_routes):
|
||||
print("Starting test_routes_added_to_app")
|
||||
|
||||
# Create the client
|
||||
client = await aiohttp_client_factory()
|
||||
|
||||
# This will raise an exception if the route doesn't exist
|
||||
await client.get('/files')
|
||||
print("Attempting to send GET request to /files")
|
||||
try:
|
||||
resp = await client.get('/files')
|
||||
print(f"Response received: status {resp.status}")
|
||||
except Exception as e:
|
||||
print(f"Exception occurred during GET request: {e}")
|
||||
raise
|
||||
|
||||
# We're not checking the response content here, just that the route exists
|
||||
# and returns a response (even if it's an error response)
|
||||
print(f"Asserting response status is not 404")
|
||||
assert resp.status != 404, "Route /files does not exist"
|
||||
|
||||
print("Test completed successfully")
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_file_service_initialization():
|
||||
with patch('api_server.routes.internal.internal_routes.FileService') as MockFileService:
|
||||
# Create a mock instance
|
||||
mock_file_service_instance = MagicMock(spec=FileService)
|
||||
MockFileService.return_value = mock_file_service_instance
|
||||
internal_routes = InternalRoutes()
|
||||
|
||||
# Check if FileService was initialized with the correct parameters
|
||||
MockFileService.assert_called_once_with({
|
||||
"models": models_dir,
|
||||
"user": user_directory,
|
||||
"output": output_directory
|
||||
})
|
||||
|
||||
# Verify that the file_service attribute of InternalRoutes is set
|
||||
assert internal_routes.file_service == mock_file_service_instance
|
||||
Loading…
x
Reference in New Issue
Block a user