Spaces:
Sleeping
Sleeping
| """Global pytest configuration and fixtures.""" | |
| import os | |
| import sys | |
| import pytest | |
| import tempfile | |
| import shutil | |
| from pathlib import Path | |
| from unittest.mock import Mock, patch | |
| # Add src to Python path | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', 'src')) | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..')) | |
| def project_root(): | |
| """Return the project root directory.""" | |
| return Path(__file__).parent.parent | |
| def test_data_dir(project_root): | |
| """Return the test data directory.""" | |
| return project_root / "tests" / "data" | |
| def temp_dir(): | |
| """Create a temporary directory for tests.""" | |
| temp_path = tempfile.mkdtemp() | |
| yield Path(temp_path) | |
| shutil.rmtree(temp_path, ignore_errors=True) | |
| def mock_database(): | |
| """Mock database connection.""" | |
| with patch('sqlite3.connect') as mock_connect: | |
| mock_db = Mock() | |
| mock_cursor = Mock() | |
| mock_db.cursor.return_value = mock_cursor | |
| mock_connect.return_value = mock_db | |
| yield mock_db, mock_cursor | |
| def sample_market_data(): | |
| """Sample market data for testing.""" | |
| return { | |
| 'symbol': 'AAPL', | |
| 'price': 150.0, | |
| 'volume': 1000000, | |
| 'timestamp': '2024-01-01T10:00:00Z', | |
| 'high': 155.0, | |
| 'low': 145.0, | |
| 'open': 148.0, | |
| 'close': 150.0 | |
| } | |
| def mock_gradio_interface(): | |
| """Mock Gradio interface for UI tests.""" | |
| with patch('gradio.Interface') as mock_interface: | |
| mock_instance = Mock() | |
| mock_interface.return_value = mock_instance | |
| yield mock_instance | |
| def mock_transformers(): | |
| """Mock transformers library.""" | |
| with patch('transformers.pipeline') as mock_pipeline: | |
| mock_model = Mock() | |
| mock_model.return_value = [{'label': 'POSITIVE', 'score': 0.9}] | |
| mock_pipeline.return_value = mock_model | |
| yield mock_model | |
| def setup_test_environment(monkeypatch): | |
| """Setup test environment variables.""" | |
| monkeypatch.setenv('TESTING', 'true') | |
| monkeypatch.setenv('LOG_LEVEL', 'DEBUG') | |
| monkeypatch.setenv('DATABASE_URL', ':memory:') | |
| def capture_logs(caplog): | |
| """Capture logs during tests.""" | |
| import logging | |
| caplog.set_level(logging.DEBUG) | |
| return caplog | |
| def pytest_configure(config): | |
| """Configure pytest.""" | |
| # Create reports directory if it doesn't exist | |
| reports_dir = Path("reports") | |
| reports_dir.mkdir(exist_ok=True) | |
| # Create test data directory if it doesn't exist | |
| test_data_dir = Path("tests/data") | |
| test_data_dir.mkdir(parents=True, exist_ok=True) | |
| def pytest_collection_modifyitems(config, items): | |
| """Modify test collection.""" | |
| # Add markers based on test file location | |
| for item in items: | |
| if "unit" in str(item.fspath): | |
| item.add_marker(pytest.mark.unit) | |
| elif "integration" in str(item.fspath): | |
| item.add_marker(pytest.mark.integration) | |
| # Mark slow tests | |
| if "slow" in item.name or "performance" in item.name: | |
| item.add_marker(pytest.mark.slow) | |
| def pytest_runtest_setup(item): | |
| """Setup for each test.""" | |
| # Skip slow tests unless explicitly requested | |
| if "slow" in item.keywords and not item.config.getoption("--runslow", default=False): | |
| pytest.skip("need --runslow option to run") | |
| def pytest_addoption(parser): | |
| """Add custom command line options.""" | |
| parser.addoption( | |
| "--runslow", action="store_true", default=False, help="run slow tests" | |
| ) | |
| parser.addoption( | |
| "--runintegration", action="store_true", default=False, help="run integration tests" | |
| ) |