Spaces:
Sleeping
Sleeping
| """Integration tests configuration and fixtures.""" | |
| import pytest | |
| import sqlite3 | |
| import tempfile | |
| import os | |
| from pathlib import Path | |
| from unittest.mock import patch | |
| def test_database(): | |
| """Create a test database for integration tests.""" | |
| # Create temporary database file | |
| db_fd, db_path = tempfile.mkstemp(suffix='.db') | |
| os.close(db_fd) | |
| # Initialize database | |
| conn = sqlite3.connect(db_path) | |
| cursor = conn.cursor() | |
| # Create tables (simplified schema) | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS logs ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| timestamp TEXT NOT NULL, | |
| level TEXT NOT NULL, | |
| message TEXT NOT NULL, | |
| module TEXT, | |
| function TEXT, | |
| line_number INTEGER | |
| ) | |
| """) | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS market_data ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| symbol TEXT NOT NULL, | |
| timestamp TEXT NOT NULL, | |
| price REAL NOT NULL, | |
| volume INTEGER, | |
| high REAL, | |
| low REAL, | |
| open_price REAL, | |
| close_price REAL | |
| ) | |
| """) | |
| cursor.execute(""" | |
| CREATE TABLE IF NOT EXISTS analysis_results ( | |
| id INTEGER PRIMARY KEY AUTOINCREMENT, | |
| timestamp TEXT NOT NULL, | |
| symbol TEXT NOT NULL, | |
| analysis_type TEXT NOT NULL, | |
| result TEXT NOT NULL, | |
| confidence REAL | |
| ) | |
| """) | |
| conn.commit() | |
| conn.close() | |
| yield db_path | |
| # Cleanup | |
| try: | |
| os.unlink(db_path) | |
| except OSError: | |
| pass | |
| def database_connection(test_database): | |
| """Provide database connection for tests.""" | |
| conn = sqlite3.connect(test_database) | |
| conn.row_factory = sqlite3.Row | |
| yield conn | |
| conn.close() | |
| def populated_database(database_connection): | |
| """Database with sample data.""" | |
| cursor = database_connection.cursor() | |
| # Insert sample log data | |
| cursor.execute(""" | |
| INSERT INTO logs (timestamp, level, message, module, function, line_number) | |
| VALUES (?, ?, ?, ?, ?, ?) | |
| """, ('2024-01-01 10:00:00', 'INFO', 'Application started', 'app', 'main', 1)) | |
| cursor.execute(""" | |
| INSERT INTO logs (timestamp, level, message, module, function, line_number) | |
| VALUES (?, ?, ?, ?, ?, ?) | |
| """, ('2024-01-01 10:01:00', 'ERROR', 'Import error occurred', 'market_analysis', 'analyze', 45)) | |
| # Insert sample market data | |
| cursor.execute(""" | |
| INSERT INTO market_data (symbol, timestamp, price, volume, high, low, open_price, close_price) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, ?) | |
| """, ('AAPL', '2024-01-01 10:00:00', 150.0, 1000000, 155.0, 145.0, 148.0, 150.0)) | |
| cursor.execute(""" | |
| INSERT INTO market_data (symbol, timestamp, price, volume, high, low, open_price, close_price) | |
| VALUES (?, ?, ?, ?, ?, ?, ?, ?) | |
| """, ('GOOGL', '2024-01-01 10:00:00', 2800.0, 500000, 2850.0, 2750.0, 2780.0, 2800.0)) | |
| # Insert sample analysis results | |
| cursor.execute(""" | |
| INSERT INTO analysis_results (timestamp, symbol, analysis_type, result, confidence) | |
| VALUES (?, ?, ?, ?, ?) | |
| """, ('2024-01-01 10:00:00', 'AAPL', 'sentiment', 'positive', 0.85)) | |
| database_connection.commit() | |
| return database_connection | |
| def mock_external_apis(): | |
| """Mock external API calls for integration tests.""" | |
| with patch('yfinance.download') as mock_yf, \ | |
| patch('requests.get') as mock_requests: | |
| # Mock yfinance response | |
| import pandas as pd | |
| mock_data = pd.DataFrame({ | |
| 'Open': [148.0, 150.0, 152.0], | |
| 'High': [155.0, 157.0, 159.0], | |
| 'Low': [145.0, 147.0, 149.0], | |
| 'Close': [150.0, 152.0, 154.0], | |
| 'Volume': [1000000, 1100000, 1200000] | |
| }) | |
| mock_yf.return_value = mock_data | |
| # Mock requests response | |
| mock_response = type('MockResponse', (), { | |
| 'status_code': 200, | |
| 'json': lambda: {'data': 'mocked'}, | |
| 'text': 'mocked response' | |
| })() | |
| mock_requests.return_value = mock_response | |
| yield mock_yf, mock_requests | |
| def integration_config(test_database): | |
| """Configuration for integration tests.""" | |
| return { | |
| 'DATABASE_URL': test_database, | |
| 'LOG_LEVEL': 'DEBUG', | |
| 'CACHE_SIZE': 50, | |
| 'API_TIMEOUT': 10, | |
| 'TESTING': True | |
| } | |
| def temp_cache_dir(): | |
| """Temporary cache directory for tests.""" | |
| import tempfile | |
| import shutil | |
| cache_dir = tempfile.mkdtemp(prefix='test_cache_') | |
| yield Path(cache_dir) | |
| shutil.rmtree(cache_dir, ignore_errors=True) | |
| def setup_integration_environment(monkeypatch, test_database, temp_cache_dir): | |
| """Setup environment for integration tests.""" | |
| monkeypatch.setenv('DATABASE_URL', test_database) | |
| monkeypatch.setenv('CACHE_DIR', str(temp_cache_dir)) | |
| monkeypatch.setenv('TESTING', 'true') | |
| monkeypatch.setenv('LOG_LEVEL', 'DEBUG') |