Spaces:
Sleeping
Sleeping
File size: 5,238 Bytes
b9c68d4 | 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 | """Integration tests configuration and fixtures."""
import pytest
import sqlite3
import tempfile
import os
from pathlib import Path
from unittest.mock import patch
@pytest.fixture(scope="session")
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
@pytest.fixture
def database_connection(test_database):
"""Provide database connection for tests."""
conn = sqlite3.connect(test_database)
conn.row_factory = sqlite3.Row
yield conn
conn.close()
@pytest.fixture
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
@pytest.fixture
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
@pytest.fixture
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
}
@pytest.fixture
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)
@pytest.fixture(autouse=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') |