Files
ETPlot/tests/test_pytest_suite.py
T
2025-09-08 10:52:58 +02:00

211 lines
6.3 KiB
Python

"""
Pytest-based test suite for the gallery generator container.
Clean, focused tests using pytest conventions.
"""
import pytest
import sys
import tempfile
import shutil
from pathlib import Path
# Add project root to Python path for container testing
if Path('/src').exists():
sys.path.insert(0, '/src')
else:
sys.path.insert(0, str(Path(__file__).parent.parent))
@pytest.fixture
def temp_dir():
"""Create a temporary directory for tests."""
temp_path = tempfile.mkdtemp()
yield temp_path
shutil.rmtree(temp_path, ignore_errors=True)
@pytest.fixture
def mock_pdf_content():
"""Mock PDF content for testing."""
return (b'%PDF-1.4\n1 0 obj\n<<\n/Type /Catalog\n/Pages 2 0 R\n>>'
b'\nendobj\nxref\n0 3\n0000000000 65535 f \ntrailer\n<<\n'
b'/Size 3\n/Root 1 0 R\n>>\nstartxref\n9\n%%EOF')
class TestEnvironment:
"""Test container environment and dependencies."""
def test_python_version(self):
"""Test Python version is correct."""
assert sys.version_info.major == 3
assert sys.version_info.minor >= 10
def test_required_packages(self):
"""Test required packages are available."""
import jinja2
import yaml
import coverage
assert jinja2.__version__
assert yaml.__version__
assert coverage.__version__
class TestCoreModules:
"""Test core application modules."""
def test_generate_gallery_import(self):
"""Test main module imports correctly."""
try:
import generate_gallery
assert hasattr(generate_gallery, 'main')
except ImportError:
pytest.skip("generate_gallery not available in test environment")
def test_config_module(self):
"""Test config module functionality."""
try:
from orchestration.config import Config
assert hasattr(Config, 'from_yaml')
except ImportError:
pytest.skip("Config module not available")
def test_logger_module(self):
"""Test logger module functionality."""
try:
from orchestration.logger import create_logger
logger = create_logger('test')
assert logger.name == 'test'
except ImportError:
pytest.skip("Logger module not available")
class TestUtilityFunctions:
"""Test utility functions."""
def test_file_operations(self, temp_dir):
"""Test basic file operations."""
test_file = Path(temp_dir) / 'test.txt'
test_file.write_text('test content')
assert test_file.exists()
assert test_file.read_text() == 'test content'
def test_directory_operations(self, temp_dir):
"""Test directory operations."""
test_subdir = Path(temp_dir) / 'subdir'
test_subdir.mkdir()
assert test_subdir.is_dir()
class TestPDFProcessing:
"""Test PDF-related functionality."""
def test_mock_pdf_creation(self, temp_dir, mock_pdf_content):
"""Test creating mock PDF files."""
pdf_path = Path(temp_dir) / 'test.pdf'
pdf_path.write_bytes(mock_pdf_content)
assert pdf_path.exists()
assert pdf_path.stat().st_size > 0
def test_imagemagick_available(self):
"""Test ImageMagick is available in container."""
import subprocess
try:
result = subprocess.run(['convert', '-version'],
capture_output=True, text=True)
assert result.returncode == 0
assert 'ImageMagick' in result.stdout
except FileNotFoundError:
pytest.skip("ImageMagick not available")
class TestMetadataSystem:
"""Test metadata handling."""
def test_yaml_processing(self, temp_dir):
"""Test YAML metadata processing."""
import yaml
metadata = {
'title': 'Test Gallery',
'description': 'Test description',
'plots': ['plot1.pdf', 'plot2.pdf']
}
yaml_path = Path(temp_dir) / 'metadata.yaml'
with open(yaml_path, 'w') as f:
yaml.dump(metadata, f)
# Read back and verify
with open(yaml_path, 'r') as f:
loaded = yaml.safe_load(f)
assert loaded['title'] == 'Test Gallery'
assert len(loaded['plots']) == 2
def test_metadata_module(self):
"""Test metadata module if available."""
try:
from orchestration.metadata import load_metadata_file
# Test with minimal functionality
assert callable(load_metadata_file)
except ImportError:
pytest.skip("Metadata module not available")
class TestGalleryGeneration:
"""Test gallery generation workflow."""
def test_template_processing(self, temp_dir):
"""Test Jinja2 template processing."""
from jinja2 import Template
template_content = """
<html>
<title>{{ title }}</title>
<body>
{% for plot in plots %}
<img src="{{ plot }}" alt="Plot {{ loop.index }}">
{% endfor %}
</body>
</html>
"""
template = Template(template_content)
result = template.render(
title='Test Gallery',
plots=['plot1.png', 'plot2.png']
)
assert 'Test Gallery' in result
assert 'plot1.png' in result
assert 'plot2.png' in result
def test_gallery_workflow(self, temp_dir, mock_pdf_content):
"""Test complete gallery workflow simulation."""
# Create mock directory structure
input_dir = Path(temp_dir) / 'input'
output_dir = Path(temp_dir) / 'output'
input_dir.mkdir()
output_dir.mkdir()
# Create mock PDF
pdf_path = input_dir / 'test.pdf'
pdf_path.write_bytes(mock_pdf_content)
# Create metadata
import yaml
metadata = {'title': 'Test', 'description': 'Test gallery'}
meta_path = input_dir / 'metadata.yaml'
with open(meta_path, 'w') as f:
yaml.dump(metadata, f)
# Verify setup
assert pdf_path.exists()
assert meta_path.exists()
assert output_dir.exists()
if __name__ == '__main__':
pytest.main([__file__, '-v'])