File size: 10,049 Bytes
811c5bc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
"""
Pytest configuration and fixtures for BackgroundFX Pro tests.
"""
import pytest
import numpy as np
import torch
import cv2
import tempfile
import shutil
from pathlib import Path
from unittest.mock import Mock, MagicMock
import os
import sys
# Add parent directory to path for imports
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# ============================================================================
# Configuration
# ============================================================================
@pytest.fixture(scope="session")
def test_config():
"""Test configuration."""
return {
'device': 'cpu', # Use CPU for testing
'test_data_dir': Path(__file__).parent / 'data',
'temp_dir': tempfile.mkdtemp(prefix='bgfx_test_'),
'max_test_duration': 30, # seconds
'use_gpu': torch.cuda.is_available()
}
@pytest.fixture(scope="session", autouse=True)
def cleanup(test_config):
"""Cleanup after all tests."""
yield
# Clean up temp directory
if os.path.exists(test_config['temp_dir']):
shutil.rmtree(test_config['temp_dir'])
# ============================================================================
# Image and Video Fixtures
# ============================================================================
@pytest.fixture
def sample_image():
"""Create a sample image for testing."""
# Create 512x512 RGB image with a person-like shape
image = np.zeros((512, 512, 3), dtype=np.uint8)
# Add background
image[:, :] = [100, 150, 200] # Blue background
# Add person-like shape (simple rectangle for testing)
cv2.rectangle(image, (150, 100), (350, 450), (50, 100, 50), -1)
# Add some texture
noise = np.random.randint(0, 20, (512, 512, 3), dtype=np.uint8)
image = cv2.add(image, noise)
return image
@pytest.fixture
def sample_mask():
"""Create a sample mask for testing."""
mask = np.zeros((512, 512), dtype=np.uint8)
# Create person mask
cv2.rectangle(mask, (150, 100), (350, 450), 255, -1)
# Add some edge refinement
mask = cv2.GaussianBlur(mask, (5, 5), 2)
return mask
@pytest.fixture
def sample_background():
"""Create a sample background image."""
background = np.zeros((512, 512, 3), dtype=np.uint8)
# Create gradient background
for i in range(512):
background[i, :] = [
int(255 * (i / 512)), # Red gradient
100, # Fixed green
int(255 * (1 - i / 512)) # Blue inverse gradient
]
return background
@pytest.fixture
def sample_video(test_config):
"""Create a sample video file for testing."""
video_path = Path(test_config['temp_dir']) / 'test_video.mp4'
# Create video writer
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(str(video_path), fourcc, 30.0, (512, 512))
# Write 30 frames (1 second at 30fps)
for i in range(30):
frame = np.zeros((512, 512, 3), dtype=np.uint8)
# Animate a moving rectangle
x = 100 + i * 5
cv2.rectangle(frame, (x, 200), (x + 100, 400), (0, 255, 0), -1)
out.write(frame)
out.release()
return str(video_path)
# ============================================================================
# Model Fixtures
# ============================================================================
@pytest.fixture
def mock_model():
"""Create a mock ML model for testing."""
model = MagicMock()
model.eval = MagicMock(return_value=None)
model.to = MagicMock(return_value=model)
# Mock forward pass
def forward(x):
batch_size = x.shape[0] if hasattr(x, 'shape') else 1
return torch.randn(batch_size, 1, 512, 512)
model.__call__ = MagicMock(side_effect=forward)
model.forward = MagicMock(side_effect=forward)
return model
@pytest.fixture
def mock_sam2_predictor():
"""Create a mock SAM2 predictor."""
predictor = MagicMock()
def predict(image):
h, w = image.shape[:2] if len(image.shape) > 2 else (512, 512)
return np.random.randint(0, 2, (h, w), dtype=np.uint8) * 255
predictor.predict = MagicMock(side_effect=predict)
predictor.set_image = MagicMock(return_value=None)
return predictor
@pytest.fixture
def mock_matanyone_model():
"""Create a mock MatAnyone model."""
model = MagicMock()
def refine(image, mask):
return cv2.GaussianBlur(mask, (5, 5), 2)
model.refine = MagicMock(side_effect=refine)
return model
# ============================================================================
# Pipeline and Processing Fixtures
# ============================================================================
@pytest.fixture
def pipeline_config():
"""Create pipeline configuration for testing."""
from api.pipeline import PipelineConfig
return PipelineConfig(
use_gpu=False, # CPU for testing
quality_preset='medium',
enable_cache=False, # Disable cache for testing
batch_size=1,
max_workers=2
)
@pytest.fixture
def mock_pipeline(pipeline_config):
"""Create a mock processing pipeline."""
from api.pipeline import ProcessingPipeline
# Mock the pipeline to avoid loading real models
with pytest.MonkeyPatch().context() as m:
m.setattr('api.pipeline.ModelFactory.load_model',
lambda self, *args, **kwargs: Mock())
pipeline = ProcessingPipeline(pipeline_config)
return pipeline
# ============================================================================
# API and Server Fixtures
# ============================================================================
@pytest.fixture
def api_client():
"""Create a test client for the API."""
from fastapi.testclient import TestClient
from api.api_server import app
return TestClient(app)
@pytest.fixture
def mock_job_manager():
"""Create a mock job manager."""
manager = MagicMock()
manager.create_job = MagicMock(return_value='test-job-123')
manager.get_job = MagicMock(return_value={'status': 'processing'})
manager.update_job = MagicMock(return_value=None)
return manager
# ============================================================================
# File System Fixtures
# ============================================================================
@pytest.fixture
def temp_dir(test_config):
"""Create a temporary directory for test files."""
temp_path = Path(test_config['temp_dir']) / 'test_run'
temp_path.mkdir(parents=True, exist_ok=True)
yield temp_path
# Cleanup
if temp_path.exists():
shutil.rmtree(temp_path)
@pytest.fixture
def sample_files(temp_dir, sample_image):
"""Create sample files in temp directory."""
files = {}
# Save sample image
image_path = temp_dir / 'sample.jpg'
cv2.imwrite(str(image_path), sample_image)
files['image'] = image_path
# Create multiple images for batch testing
for i in range(3):
path = temp_dir / f'image_{i}.jpg'
cv2.imwrite(str(path), sample_image)
files[f'image_{i}'] = path
return files
# ============================================================================
# Model Registry Fixtures
# ============================================================================
@pytest.fixture
def mock_registry():
"""Create a mock model registry."""
from models.registry import ModelRegistry, ModelInfo, ModelTask, ModelFramework
registry = ModelRegistry(models_dir=Path(tempfile.mkdtemp()))
# Add test model
test_model = ModelInfo(
model_id='test-model',
name='Test Model',
version='1.0',
task=ModelTask.SEGMENTATION,
framework=ModelFramework.PYTORCH,
url='http://example.com/model.pth',
filename='test_model.pth',
file_size=1000000
)
registry.register_model(test_model)
return registry
# ============================================================================
# WebSocket Fixtures
# ============================================================================
@pytest.fixture
def mock_websocket():
"""Create a mock WebSocket connection."""
ws = MagicMock()
ws.accept = MagicMock(return_value=None)
ws.send_json = MagicMock(return_value=None)
ws.receive_text = MagicMock(return_value='{"type": "ping", "data": {}}')
return ws
# ============================================================================
# Utility Fixtures
# ============================================================================
@pytest.fixture
def mock_progress_callback():
"""Create a mock progress callback."""
callback = MagicMock()
return callback
@pytest.fixture
def device():
"""Get device for testing."""
return 'cuda' if torch.cuda.is_available() else 'cpu'
@pytest.fixture
def performance_timer():
"""Timer for performance testing."""
import time
class Timer:
def __init__(self):
self.start_time = None
self.elapsed = 0
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, *args):
self.elapsed = time.time() - self.start_time
return Timer
# ============================================================================
# Markers
# ============================================================================
def pytest_configure(config):
"""Register custom markers."""
config.addinivalue_line(
"markers", "slow: marks tests as slow (deselect with '-m \"not slow\"')"
)
config.addinivalue_line(
"markers", "gpu: marks tests that require GPU"
)
config.addinivalue_line(
"markers", "integration: marks integration tests"
)
config.addinivalue_line(
"markers", "unit: marks unit tests"
) |