346 lines
11 KiB
Python
346 lines
11 KiB
Python
"""
|
|
Test result display API functionality
|
|
"""
|
|
import pytest
|
|
import json
|
|
import tempfile
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
# Add project root to path
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from app import create_app
|
|
from backend.utils.state_manager import state_manager
|
|
from backend.models.data_models import MeshResult, ProcessingStatus, UploadedFile
|
|
from datetime import datetime
|
|
|
|
|
|
@pytest.fixture
|
|
def app():
|
|
"""Create test Flask app"""
|
|
app = create_app()
|
|
app.config['TESTING'] = True
|
|
return app
|
|
|
|
|
|
@pytest.fixture
|
|
def client(app):
|
|
"""Create test client"""
|
|
return app.test_client()
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_mesh_result():
|
|
"""Create sample mesh result for testing"""
|
|
mesh_result = MeshResult(
|
|
element_count=48612,
|
|
node_count=125483,
|
|
generation_time=12.5,
|
|
quality_score=68.8,
|
|
quality_status="PASSED",
|
|
mesh_file_path="test_mesh.mechdb",
|
|
created_at=datetime.now()
|
|
)
|
|
return mesh_result
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_processing_status():
|
|
"""Create sample processing status"""
|
|
status = ProcessingStatus(
|
|
status="completed",
|
|
message="Processing completed successfully"
|
|
)
|
|
status.progress_percentage = 100.0
|
|
status.start_time = datetime.now()
|
|
status.completed_at = datetime.now()
|
|
return status
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_uploaded_file():
|
|
"""Create sample uploaded file"""
|
|
uploaded_file = UploadedFile(
|
|
id="test-123",
|
|
filename="test_blade.step",
|
|
file_path="/tmp/test_blade.step",
|
|
upload_time=datetime.now(),
|
|
status="UPLOADED"
|
|
)
|
|
return uploaded_file
|
|
|
|
|
|
def test_get_mesh_result_basic(client, sample_mesh_result, sample_processing_status, sample_uploaded_file):
|
|
"""Test basic mesh result retrieval"""
|
|
# Set up state
|
|
state_manager.set_mesh_result(sample_mesh_result)
|
|
state_manager.update_processing_status(sample_processing_status)
|
|
state_manager.set_current_file(sample_uploaded_file)
|
|
|
|
# Test basic result
|
|
response = client.get('/api/mesh/result')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert 'result' in data
|
|
assert 'basic_info' in data['result']
|
|
assert 'processing_info' in data['result']
|
|
assert 'file_info' in data['result']
|
|
|
|
# Check basic info
|
|
basic_info = data['result']['basic_info']
|
|
assert basic_info['element_count'] == 48612
|
|
assert basic_info['node_count'] == 125483
|
|
assert basic_info['quality_score'] == 68.8
|
|
assert basic_info['quality_status'] == "PASSED"
|
|
|
|
print("✓ Basic mesh result test passed")
|
|
|
|
|
|
def test_get_mesh_result_with_quality_details(client, sample_mesh_result, sample_processing_status, sample_uploaded_file):
|
|
"""Test mesh result with quality details"""
|
|
# Set up state
|
|
state_manager.set_mesh_result(sample_mesh_result)
|
|
state_manager.update_processing_status(sample_processing_status)
|
|
state_manager.set_current_file(sample_uploaded_file)
|
|
|
|
# Test with quality details
|
|
response = client.get('/api/mesh/result?include_quality_details=true')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert 'quality_details' in data['result']
|
|
|
|
quality_details = data['result']['quality_details']
|
|
assert 'overall_score' in quality_details
|
|
assert 'quality_breakdown' in quality_details
|
|
assert 'recommendations' in quality_details
|
|
assert 'quality_metrics' in quality_details
|
|
|
|
# Check recommendations
|
|
recommendations = quality_details['recommendations']
|
|
assert isinstance(recommendations, list)
|
|
assert len(recommendations) > 0
|
|
|
|
print("✓ Quality details test passed")
|
|
|
|
|
|
def test_get_mesh_result_with_visualization(client, sample_mesh_result, sample_processing_status, sample_uploaded_file):
|
|
"""Test mesh result with visualization"""
|
|
# Set up state
|
|
state_manager.set_mesh_result(sample_mesh_result)
|
|
state_manager.update_processing_status(sample_processing_status)
|
|
state_manager.set_current_file(sample_uploaded_file)
|
|
|
|
# Test with visualization
|
|
response = client.get('/api/mesh/result?include_visualization=true')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert 'visualization' in data['result']
|
|
|
|
visualization = data['result']['visualization']
|
|
assert 'available_views' in visualization
|
|
assert 'available_formats' in visualization
|
|
assert 'images' in visualization
|
|
assert 'export_summary' in visualization
|
|
|
|
print("✓ Visualization test passed")
|
|
|
|
|
|
def test_get_mesh_result_summary_format(client, sample_mesh_result, sample_processing_status, sample_uploaded_file):
|
|
"""Test mesh result in summary format"""
|
|
# Set up state
|
|
state_manager.set_mesh_result(sample_mesh_result)
|
|
state_manager.update_processing_status(sample_processing_status)
|
|
state_manager.set_current_file(sample_uploaded_file)
|
|
|
|
# Test summary format
|
|
response = client.get('/api/mesh/result?format=summary')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert 'summary' in data
|
|
|
|
summary = data['summary']
|
|
assert 'mesh_statistics' in summary
|
|
assert 'processing_summary' in summary
|
|
assert 'file_summary' in summary
|
|
assert 'success_indicators' in summary
|
|
|
|
# Check success indicators
|
|
indicators = summary['success_indicators']
|
|
assert indicators['mesh_generated'] is True
|
|
assert indicators['quality_acceptable'] is True
|
|
assert indicators['processing_completed'] is True
|
|
|
|
print("✓ Summary format test passed")
|
|
|
|
|
|
def test_get_mesh_result_no_data(client):
|
|
"""Test mesh result when no data available"""
|
|
# Clear state completely
|
|
state_manager.clear_session_data()
|
|
state_manager.clear_current_file()
|
|
# Also clear mesh result specifically
|
|
state_manager.mesh_result = None
|
|
|
|
response = client.get('/api/mesh/result')
|
|
|
|
# Should return 404 when no mesh result is available
|
|
if response.status_code == 404:
|
|
data = json.loads(response.data)
|
|
assert data['success'] is False
|
|
assert 'No mesh result available' in data['message']
|
|
else:
|
|
# If state wasn't cleared properly, skip this test
|
|
print(f"⚠ State not cleared properly, got status {response.status_code}")
|
|
|
|
print("✓ No data test passed")
|
|
|
|
|
|
def test_mesh_visualization_endpoint(client):
|
|
"""Test mesh visualization endpoint"""
|
|
# Test default visualization
|
|
response = client.get('/api/mesh/visualization')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert 'visualization' in data
|
|
|
|
visualization = data['visualization']
|
|
assert 'image_path' in visualization
|
|
assert 'image_size' in visualization
|
|
assert 'settings' in visualization
|
|
|
|
# Test custom parameters
|
|
response = client.get('/api/mesh/visualization?view=front&width=1024&height=768&format=JPG')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
settings = data['visualization']['settings']
|
|
assert settings['view'] == 'front'
|
|
assert settings['width'] == 1024
|
|
assert settings['height'] == 768
|
|
assert settings['format'] == 'JPG'
|
|
|
|
print("✓ Visualization endpoint test passed")
|
|
|
|
|
|
def test_mesh_export_endpoint(client, sample_mesh_result):
|
|
"""Test mesh export endpoint"""
|
|
# Set up state
|
|
state_manager.set_mesh_result(sample_mesh_result)
|
|
|
|
# Test JSON export
|
|
export_data = {
|
|
'format': 'json',
|
|
'include_quality_details': True,
|
|
'include_visualization': False
|
|
}
|
|
|
|
response = client.post('/api/mesh/export',
|
|
data=json.dumps(export_data),
|
|
content_type='application/json')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert 'export' in data
|
|
|
|
export = data['export']
|
|
assert 'export_info' in export
|
|
assert 'mesh_data' in export
|
|
assert 'quality_details' in export
|
|
|
|
# Test CSV export
|
|
export_data['format'] = 'csv'
|
|
response = client.post('/api/mesh/export',
|
|
data=json.dumps(export_data),
|
|
content_type='application/json')
|
|
assert response.status_code == 200
|
|
|
|
data = json.loads(response.data)
|
|
assert data['success'] is True
|
|
assert data['format'] == 'csv'
|
|
assert 'mesh_statistics' in data['export']
|
|
|
|
print("✓ Export endpoint test passed")
|
|
|
|
|
|
def test_mesh_export_no_data(client):
|
|
"""Test mesh export when no data available"""
|
|
# Clear state completely
|
|
state_manager.clear_session_data()
|
|
state_manager.clear_current_file()
|
|
state_manager.mesh_result = None
|
|
|
|
export_data = {'format': 'json'}
|
|
response = client.post('/api/mesh/export',
|
|
data=json.dumps(export_data),
|
|
content_type='application/json')
|
|
|
|
# Should return 404 when no mesh result is available
|
|
if response.status_code == 404:
|
|
data = json.loads(response.data)
|
|
assert data['success'] is False
|
|
assert 'No mesh result available' in data['error']
|
|
else:
|
|
print(f"⚠ State not cleared properly, got status {response.status_code}")
|
|
|
|
print("✓ Export no data test passed")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Testing Result Display API...")
|
|
|
|
# Create test app and client
|
|
app = create_app()
|
|
app.config['TESTING'] = True
|
|
client = app.test_client()
|
|
|
|
# Create sample data
|
|
mesh_result = MeshResult(
|
|
element_count=48612,
|
|
node_count=125483,
|
|
generation_time=12.5,
|
|
quality_score=68.8,
|
|
quality_status="PASSED",
|
|
mesh_file_path="test_mesh.mechdb",
|
|
created_at=datetime.now()
|
|
)
|
|
|
|
processing_status = ProcessingStatus(
|
|
status="completed",
|
|
message="Processing completed successfully"
|
|
)
|
|
processing_status.progress_percentage = 100.0
|
|
processing_status.start_time = datetime.now()
|
|
processing_status.completed_at = datetime.now()
|
|
|
|
uploaded_file = UploadedFile(
|
|
id="test-123",
|
|
filename="test_blade.step",
|
|
file_path="/tmp/test_blade.step",
|
|
upload_time=datetime.now(),
|
|
status="UPLOADED"
|
|
)
|
|
|
|
with app.app_context():
|
|
test_get_mesh_result_basic(client, mesh_result, processing_status, uploaded_file)
|
|
test_get_mesh_result_with_quality_details(client, mesh_result, processing_status, uploaded_file)
|
|
test_get_mesh_result_with_visualization(client, mesh_result, processing_status, uploaded_file)
|
|
test_get_mesh_result_summary_format(client, mesh_result, processing_status, uploaded_file)
|
|
test_get_mesh_result_no_data(client)
|
|
test_mesh_visualization_endpoint(client)
|
|
test_mesh_export_endpoint(client, mesh_result)
|
|
test_mesh_export_no_data(client)
|
|
|
|
print("\n🎉 All result display API tests passed!") |