252 lines
8.2 KiB
Python
252 lines
8.2 KiB
Python
"""
|
|
Test visualization exporter functionality
|
|
"""
|
|
import pytest
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
# Add project root to path
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from backend.utils.visualization_exporter import (
|
|
VisualizationExporter,
|
|
VisualizationSettings,
|
|
VisualizationResult
|
|
)
|
|
|
|
|
|
def test_visualization_exporter_initialization():
|
|
"""Test VisualizationExporter initialization"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
exporter = VisualizationExporter(output_dir=temp_dir)
|
|
|
|
assert exporter.simulation_mode is True
|
|
assert exporter.output_dir == Path(temp_dir)
|
|
assert exporter.output_dir.exists()
|
|
|
|
|
|
def test_visualization_settings():
|
|
"""Test VisualizationSettings data class"""
|
|
settings = VisualizationSettings()
|
|
|
|
# Test default values
|
|
assert settings.width == 1280
|
|
assert settings.height == 720
|
|
assert settings.background_color == "white"
|
|
assert settings.show_edges is True
|
|
assert settings.show_nodes is False
|
|
assert settings.camera_view == "isometric"
|
|
assert settings.image_format == "PNG"
|
|
|
|
# Test custom values
|
|
custom_settings = VisualizationSettings(
|
|
width=1920,
|
|
height=1080,
|
|
background_color="black",
|
|
camera_view="front"
|
|
)
|
|
|
|
assert custom_settings.width == 1920
|
|
assert custom_settings.height == 1080
|
|
assert custom_settings.background_color == "black"
|
|
assert custom_settings.camera_view == "front"
|
|
|
|
|
|
def test_simulation_mesh_image_export():
|
|
"""Test mesh image export in simulation mode"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
exporter = VisualizationExporter(output_dir=temp_dir)
|
|
|
|
# Test with default settings
|
|
result = exporter.export_mesh_image()
|
|
|
|
assert isinstance(result, VisualizationResult)
|
|
assert result.success is True
|
|
assert result.image_path is not None
|
|
assert Path(result.image_path).exists()
|
|
assert result.file_size > 0
|
|
assert result.export_time > 0
|
|
assert len(result.warnings) > 0
|
|
assert "Simulated visualization export" in result.warnings[0]
|
|
|
|
print(f"✓ Simulation mesh export test passed:")
|
|
print(f" - Image path: {result.image_path}")
|
|
print(f" - File size: {result.file_size} bytes")
|
|
print(f" - Export time: {result.export_time:.2f} seconds")
|
|
|
|
|
|
def test_custom_visualization_settings():
|
|
"""Test mesh export with custom settings"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
exporter = VisualizationExporter(output_dir=temp_dir)
|
|
|
|
custom_settings = VisualizationSettings(
|
|
width=800,
|
|
height=600,
|
|
background_color="black",
|
|
camera_view="front",
|
|
image_format="JPG"
|
|
)
|
|
|
|
result = exporter.export_mesh_image(
|
|
filename="custom_mesh.jpg",
|
|
settings=custom_settings
|
|
)
|
|
|
|
assert result.success is True
|
|
assert result.image_path.endswith("custom_mesh.jpg")
|
|
assert result.image_size == (800, 600)
|
|
assert Path(result.image_path).exists()
|
|
|
|
print(f"✓ Custom settings test passed:")
|
|
print(f" - Custom filename: {Path(result.image_path).name}")
|
|
print(f" - Custom resolution: {result.image_size}")
|
|
|
|
|
|
def test_quality_visualization_export():
|
|
"""Test mesh quality visualization export"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
exporter = VisualizationExporter(output_dir=temp_dir)
|
|
|
|
# Test different quality metrics
|
|
quality_metrics = ["element_quality", "aspect_ratio", "skewness"]
|
|
|
|
for metric in quality_metrics:
|
|
result = exporter.export_quality_visualization(quality_metric=metric)
|
|
|
|
assert result.success is True
|
|
assert result.image_path is not None
|
|
assert Path(result.image_path).exists()
|
|
assert metric in result.image_path
|
|
assert len(result.warnings) > 0
|
|
|
|
print(f"✓ Quality visualization test passed for {metric}")
|
|
|
|
|
|
def test_available_views_and_formats():
|
|
"""Test available views and formats"""
|
|
exporter = VisualizationExporter()
|
|
|
|
views = exporter.get_available_views()
|
|
formats = exporter.get_available_formats()
|
|
|
|
assert isinstance(views, list)
|
|
assert len(views) > 0
|
|
assert "isometric" in views
|
|
assert "front" in views
|
|
assert "side" in views
|
|
assert "top" in views
|
|
|
|
assert isinstance(formats, list)
|
|
assert len(formats) > 0
|
|
assert "PNG" in formats
|
|
assert "JPG" in formats
|
|
|
|
print(f"✓ Available views: {views}")
|
|
print(f"✓ Available formats: {formats}")
|
|
|
|
|
|
def test_export_summary():
|
|
"""Test export summary functionality"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
exporter = VisualizationExporter(output_dir=temp_dir)
|
|
|
|
summary = exporter.get_export_summary()
|
|
|
|
assert isinstance(summary, dict)
|
|
assert summary['exporter_type'] == 'VisualizationExporter'
|
|
assert summary['simulation_mode'] is True
|
|
assert summary['output_directory'] == temp_dir
|
|
assert 'available_views' in summary
|
|
assert 'available_formats' in summary
|
|
assert 'supported_features' in summary
|
|
assert 'created_at' in summary
|
|
|
|
# Check supported features
|
|
features = summary['supported_features']
|
|
assert 'mesh_visualization' in features
|
|
assert 'quality_visualization' in features
|
|
assert 'custom_camera_views' in features
|
|
|
|
print(f"✓ Export summary test passed:")
|
|
print(f" - Features: {len(features)} supported")
|
|
print(f" - Views: {len(summary['available_views'])} available")
|
|
print(f" - Formats: {len(summary['available_formats'])} supported")
|
|
|
|
|
|
def test_cleanup_temp_files():
|
|
"""Test temporary file cleanup"""
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
exporter = VisualizationExporter(output_dir=temp_dir)
|
|
|
|
# Create some temporary files
|
|
temp_files = [
|
|
Path(temp_dir) / "temp_mesh.png",
|
|
Path(temp_dir) / "temp_quality.jpg",
|
|
Path(temp_dir) / "visualization_temp.png"
|
|
]
|
|
|
|
for temp_file in temp_files:
|
|
temp_file.write_text("temporary content")
|
|
|
|
# Verify files exist
|
|
for temp_file in temp_files:
|
|
assert temp_file.exists()
|
|
|
|
# Clean up
|
|
cleaned_count = exporter.cleanup_temp_files()
|
|
|
|
assert cleaned_count == len(temp_files)
|
|
|
|
# Verify files are gone
|
|
for temp_file in temp_files:
|
|
assert not temp_file.exists()
|
|
|
|
print(f"✓ Cleanup test passed: {cleaned_count} files cleaned")
|
|
|
|
|
|
def test_error_handling():
|
|
"""Test error handling in visualization export"""
|
|
# Test with invalid output directory
|
|
try:
|
|
exporter = VisualizationExporter(output_dir="/invalid/path/that/does/not/exist")
|
|
# Should still work due to mkdir(parents=True, exist_ok=True)
|
|
assert exporter.output_dir.exists()
|
|
print("✓ Error handling test passed: Invalid path handled gracefully")
|
|
except Exception as e:
|
|
print(f"✓ Error handling test passed: Exception caught as expected: {e}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
print("Testing Visualization Exporter...")
|
|
|
|
test_visualization_exporter_initialization()
|
|
print("✓ Initialization test passed")
|
|
|
|
test_visualization_settings()
|
|
print("✓ Settings test passed")
|
|
|
|
test_simulation_mesh_image_export()
|
|
print("✓ Simulation export test passed")
|
|
|
|
test_custom_visualization_settings()
|
|
print("✓ Custom settings test passed")
|
|
|
|
test_quality_visualization_export()
|
|
print("✓ Quality visualization test passed")
|
|
|
|
test_available_views_and_formats()
|
|
print("✓ Views and formats test passed")
|
|
|
|
test_export_summary()
|
|
print("✓ Export summary test passed")
|
|
|
|
test_cleanup_temp_files()
|
|
print("✓ Cleanup test passed")
|
|
|
|
test_error_handling()
|
|
print("✓ Error handling test passed")
|
|
|
|
print("\n🎉 All visualization exporter tests passed!") |