Created ml/src/types/ohlcv.rs as the single source of truth for OHLCVBar (DateTime<Utc> timestamp, f64 OHLCV fields). Replaced all 13 duplicate definitions across features/, regime/, real_data_loader, and evaluation/ with imports from crate::types::OHLCVBar. Key changes: - New: ml/src/types/mod.rs + ohlcv.rs with canonical OHLCVBar (derives: Debug, Clone, Copy, PartialEq, Serialize, Deserialize + Default) - Renamed: evaluation::metrics::OHLCVBar → OHLCVBarF32 (genuinely different type: f32 fields, i64 timestamp for compact backtesting) - Eliminated all import aliases (ExtractionOHLCVBar, RegimeOHLCVBar, PriceOHLCVBar, VolumeOHLCVBar) in dbn_sequence_loader.rs and pipeline.rs - Renamed regime::orchestrator::Bar → OHLCVBar (same fields, just aliased) - Updated 39 files total (13 definitions removed, imports normalized) 1883 lib tests passing, compilation clean. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
415 lines
13 KiB
Rust
415 lines
13 KiB
Rust
//! # Feature Cache Tests (TDD)
|
||
//!
|
||
//! Test suite for pre-computed feature caching system.
|
||
//! Following TDD: Tests written FIRST, implementation comes after.
|
||
//!
|
||
//! ## Test Coverage
|
||
//!
|
||
//! 1. Feature extraction to 256-dim vectors
|
||
//! 2. Parquet serialization/deserialization
|
||
//! 3. MinIO storage integration
|
||
//! 4. Cache invalidation on data changes
|
||
//! 5. Performance benchmarks (10x improvement target)
|
||
|
||
use anyhow::Result;
|
||
use chrono::Utc;
|
||
use ml::real_data_loader::RealDataLoader;
|
||
use ml::types::OHLCVBar;
|
||
use std::path::PathBuf;
|
||
use tempfile::TempDir;
|
||
|
||
// ============================================================================
|
||
// TEST 1: Feature Extraction (256-dim vectors)
|
||
// ============================================================================
|
||
|
||
#[tokio::test]
|
||
async fn test_extract_256_dim_features() -> Result<()> {
|
||
// Load real data
|
||
let mut loader = RealDataLoader::new_from_workspace()?;
|
||
let bars = loader.load_symbol_data("ZN.FUT").await?;
|
||
assert!(bars.len() > 100, "Need >100 bars for testing");
|
||
|
||
// Extract features using feature cache service (NOT IMPLEMENTED YET)
|
||
// This should FAIL until we implement FeatureCacheService
|
||
let result = extract_ml_features(&bars);
|
||
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - extract_ml_features not implemented yet"
|
||
);
|
||
|
||
println!("✅ Test 1: Feature extraction test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_feature_dimensions() -> Result<()> {
|
||
// This test will validate feature dimensions once implemented
|
||
// Expected: 256-dim feature vector per bar
|
||
// - 5 OHLCV features
|
||
// - 10 technical indicators
|
||
// - 241 additional engineered features (price patterns, volume patterns, etc.)
|
||
|
||
let bars = create_mock_bars(100);
|
||
let result = extract_ml_features(&bars);
|
||
|
||
// Should fail until implemented
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - extract_ml_features not implemented"
|
||
);
|
||
|
||
println!("✅ Test 2: Feature dimensions test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
// ============================================================================
|
||
// TEST 2: Parquet Serialization/Deserialization
|
||
// ============================================================================
|
||
|
||
#[tokio::test]
|
||
async fn test_parquet_write_read() -> Result<()> {
|
||
// Create temp directory for Parquet files
|
||
let temp_dir = TempDir::new()?;
|
||
let parquet_path = temp_dir.path().join("features.parquet");
|
||
|
||
// Create mock feature data (256-dim vectors)
|
||
let features = create_mock_feature_matrix(100); // 100 bars × 256 features
|
||
|
||
// Write to Parquet (NOT IMPLEMENTED YET)
|
||
let result = write_features_to_parquet(&features, &parquet_path);
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - write_features_to_parquet not implemented"
|
||
);
|
||
|
||
println!("✅ Test 3: Parquet write test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_parquet_read_features() -> Result<()> {
|
||
let temp_dir = TempDir::new()?;
|
||
let parquet_path = temp_dir.path().join("features.parquet");
|
||
|
||
// This test will validate reading Parquet files once implemented
|
||
let result = read_features_from_parquet(&parquet_path);
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - read_features_from_parquet not implemented"
|
||
);
|
||
|
||
println!("✅ Test 4: Parquet read test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_parquet_roundtrip() -> Result<()> {
|
||
// Test that features survive serialization/deserialization
|
||
let temp_dir = TempDir::new()?;
|
||
let parquet_path = temp_dir.path().join("features_roundtrip.parquet");
|
||
|
||
let original_features = create_mock_feature_matrix(50);
|
||
|
||
// Write and read back (NOT IMPLEMENTED YET)
|
||
let write_result = write_features_to_parquet(&original_features, &parquet_path);
|
||
assert!(write_result.is_err(), "Should fail - not implemented");
|
||
|
||
println!("✅ Test 5: Parquet roundtrip test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
// ============================================================================
|
||
// TEST 3: MinIO Storage Integration
|
||
// ============================================================================
|
||
|
||
#[tokio::test]
|
||
async fn test_minio_upload() -> Result<()> {
|
||
// Test uploading feature cache to MinIO
|
||
// Note: Requires MinIO running locally or in Docker
|
||
|
||
let temp_dir = TempDir::new()?;
|
||
let parquet_path = temp_dir.path().join("features_minio.parquet");
|
||
|
||
let features = create_mock_feature_matrix(100);
|
||
|
||
// Upload to MinIO (NOT IMPLEMENTED YET)
|
||
let result =
|
||
upload_features_to_minio(&features, "test-bucket", "ZN.FUT/features.parquet").await;
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - upload_features_to_minio not implemented"
|
||
);
|
||
|
||
println!("✅ Test 6: MinIO upload test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_minio_download() -> Result<()> {
|
||
// Test downloading feature cache from MinIO
|
||
|
||
let result = download_features_from_minio("test-bucket", "ZN.FUT/features.parquet").await;
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - download_features_from_minio not implemented"
|
||
);
|
||
|
||
println!("✅ Test 7: MinIO download test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_minio_list_cached_symbols() -> Result<()> {
|
||
// Test listing all cached symbols in MinIO
|
||
|
||
let result = list_cached_symbols("test-bucket").await;
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - list_cached_symbols not implemented"
|
||
);
|
||
|
||
println!("✅ Test 8: MinIO list test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
// ============================================================================
|
||
// TEST 4: Cache Invalidation
|
||
// ============================================================================
|
||
|
||
#[tokio::test]
|
||
async fn test_cache_invalidation_on_data_change() -> Result<()> {
|
||
// Test that cache is invalidated when raw data changes
|
||
|
||
let cache_service = create_feature_cache_service().await;
|
||
|
||
// Initial cache
|
||
let bars_v1 = create_mock_bars(100);
|
||
let result1 = cache_service
|
||
.get_or_compute_features("ZN.FUT", &bars_v1)
|
||
.await;
|
||
assert!(
|
||
result1.is_err(),
|
||
"Should fail - FeatureCacheService not implemented"
|
||
);
|
||
|
||
println!("✅ Test 9: Cache invalidation test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cache_hit_vs_miss() -> Result<()> {
|
||
// Test cache hit/miss detection
|
||
|
||
let cache_service = create_feature_cache_service().await;
|
||
|
||
let result = cache_service.is_cached("ZN.FUT").await;
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - FeatureCacheService not implemented"
|
||
);
|
||
|
||
println!("✅ Test 10: Cache hit/miss test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_cache_metadata() -> Result<()> {
|
||
// Test cache metadata (timestamp, bar count, version)
|
||
|
||
let cache_service = create_feature_cache_service().await;
|
||
|
||
let result = cache_service.get_cache_metadata("ZN.FUT").await;
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - FeatureCacheService not implemented"
|
||
);
|
||
|
||
println!("✅ Test 11: Cache metadata test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
// ============================================================================
|
||
// TEST 5: Performance Benchmarks
|
||
// ============================================================================
|
||
|
||
#[tokio::test]
|
||
async fn test_cache_performance_improvement() -> Result<()> {
|
||
// Test that cached features load 10x faster than re-computing
|
||
// Target: <100ms cache load vs ~1000ms re-computation
|
||
|
||
let mut loader = RealDataLoader::new_from_workspace()?;
|
||
let bars = loader.load_symbol_data("ZN.FUT").await?;
|
||
|
||
// Baseline: Re-compute features (should be ~1000ms)
|
||
let start = std::time::Instant::now();
|
||
let _features = extract_ml_features(&bars);
|
||
let compute_time = start.elapsed();
|
||
|
||
// Cached: Load from cache (should be <100ms)
|
||
let cache_service = create_feature_cache_service().await;
|
||
let start = std::time::Instant::now();
|
||
let result = cache_service.get_or_compute_features("ZN.FUT", &bars).await;
|
||
let cache_time = start.elapsed();
|
||
|
||
assert!(result.is_err(), "Should fail - not implemented yet");
|
||
|
||
println!("✅ Test 12: Performance benchmark test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
println!(" Baseline compute time: {:?}", compute_time);
|
||
println!(" Target cache time: <100ms (10x improvement)");
|
||
|
||
Ok(())
|
||
}
|
||
|
||
#[tokio::test]
|
||
async fn test_batch_cache_loading() -> Result<()> {
|
||
// Test loading multiple cached symbols in parallel
|
||
|
||
let cache_service = create_feature_cache_service().await;
|
||
|
||
let symbols = vec!["ZN.FUT", "6E.FUT", "ES.FUT"];
|
||
let result = cache_service.load_batch_cached(symbols).await;
|
||
|
||
assert!(
|
||
result.is_err(),
|
||
"Should fail - load_batch_cached not implemented"
|
||
);
|
||
|
||
println!("✅ Test 13: Batch cache loading test written (WILL FAIL UNTIL IMPLEMENTED)");
|
||
Ok(())
|
||
}
|
||
|
||
// ============================================================================
|
||
// Helper Functions (NOT IMPLEMENTED - Will be in feature_cache module)
|
||
// ============================================================================
|
||
|
||
/// Extract 256-dim ML features from OHLCV bars
|
||
/// NOT IMPLEMENTED YET - This is what we need to build
|
||
fn extract_ml_features(_bars: &[OHLCVBar]) -> Result<Vec<Vec<f32>>> {
|
||
Err(anyhow::anyhow!("extract_ml_features not implemented yet"))
|
||
}
|
||
|
||
/// Write features to Parquet file
|
||
/// NOT IMPLEMENTED YET
|
||
fn write_features_to_parquet(_features: &[Vec<f32>], _path: &PathBuf) -> Result<()> {
|
||
Err(anyhow::anyhow!(
|
||
"write_features_to_parquet not implemented yet"
|
||
))
|
||
}
|
||
|
||
/// Read features from Parquet file
|
||
/// NOT IMPLEMENTED YET
|
||
fn read_features_from_parquet(_path: &PathBuf) -> Result<Vec<Vec<f32>>> {
|
||
Err(anyhow::anyhow!(
|
||
"read_features_from_parquet not implemented yet"
|
||
))
|
||
}
|
||
|
||
/// Upload features to MinIO
|
||
/// NOT IMPLEMENTED YET
|
||
async fn upload_features_to_minio(_features: &[Vec<f32>], _bucket: &str, _key: &str) -> Result<()> {
|
||
Err(anyhow::anyhow!(
|
||
"upload_features_to_minio not implemented yet"
|
||
))
|
||
}
|
||
|
||
/// Download features from MinIO
|
||
/// NOT IMPLEMENTED YET
|
||
async fn download_features_from_minio(_bucket: &str, _key: &str) -> Result<Vec<Vec<f32>>> {
|
||
Err(anyhow::anyhow!(
|
||
"download_features_from_minio not implemented yet"
|
||
))
|
||
}
|
||
|
||
/// List cached symbols in MinIO bucket
|
||
/// NOT IMPLEMENTED YET
|
||
async fn list_cached_symbols(_bucket: &str) -> Result<Vec<String>> {
|
||
Err(anyhow::anyhow!("list_cached_symbols not implemented yet"))
|
||
}
|
||
|
||
/// Create mock OHLCV bars for testing
|
||
fn create_mock_bars(count: usize) -> Vec<OHLCVBar> {
|
||
let mut bars = Vec::with_capacity(count);
|
||
let base_price = 100.0;
|
||
let base_time = chrono::Utc::now();
|
||
|
||
for i in 0..count {
|
||
bars.push(OHLCVBar {
|
||
timestamp: base_time + chrono::Duration::minutes(i as i64),
|
||
open: base_price + (i as f64 * 0.1),
|
||
high: base_price + (i as f64 * 0.15),
|
||
low: base_price + (i as f64 * 0.05),
|
||
close: base_price + (i as f64 * 0.12),
|
||
volume: 1000.0 + (i as f64 * 10.0),
|
||
});
|
||
}
|
||
|
||
bars
|
||
}
|
||
|
||
/// Create mock 256-dim feature matrix for testing
|
||
fn create_mock_feature_matrix(num_bars: usize) -> Vec<Vec<f32>> {
|
||
let mut features = Vec::with_capacity(num_bars);
|
||
|
||
for i in 0..num_bars {
|
||
let mut feature_vec = Vec::with_capacity(256);
|
||
for j in 0..256 {
|
||
feature_vec.push((i + j) as f32 * 0.01);
|
||
}
|
||
features.push(feature_vec);
|
||
}
|
||
|
||
features
|
||
}
|
||
|
||
/// Create feature cache service (NOT IMPLEMENTED YET)
|
||
async fn create_feature_cache_service() -> FeatureCacheService {
|
||
FeatureCacheService::new()
|
||
}
|
||
|
||
// ============================================================================
|
||
// Placeholder Types (Will be in feature_cache module)
|
||
// ============================================================================
|
||
|
||
/// Feature cache service (NOT IMPLEMENTED YET)
|
||
#[allow(dead_code)]
|
||
struct FeatureCacheService {
|
||
// Will be implemented in ml/src/feature_cache/cache.rs
|
||
}
|
||
|
||
impl FeatureCacheService {
|
||
fn new() -> Self {
|
||
Self {}
|
||
}
|
||
|
||
async fn get_or_compute_features(
|
||
&self,
|
||
_symbol: &str,
|
||
_bars: &[OHLCVBar],
|
||
) -> Result<Vec<Vec<f32>>> {
|
||
Err(anyhow::anyhow!("FeatureCacheService not implemented yet"))
|
||
}
|
||
|
||
async fn is_cached(&self, _symbol: &str) -> Result<bool> {
|
||
Err(anyhow::anyhow!("FeatureCacheService not implemented yet"))
|
||
}
|
||
|
||
async fn get_cache_metadata(&self, _symbol: &str) -> Result<CacheMetadata> {
|
||
Err(anyhow::anyhow!("FeatureCacheService not implemented yet"))
|
||
}
|
||
|
||
async fn load_batch_cached(&self, _symbols: Vec<&str>) -> Result<Vec<Vec<Vec<f32>>>> {
|
||
Err(anyhow::anyhow!("FeatureCacheService not implemented yet"))
|
||
}
|
||
}
|
||
|
||
/// Cache metadata (NOT IMPLEMENTED YET)
|
||
#[allow(dead_code)]
|
||
struct CacheMetadata {
|
||
symbol: String,
|
||
bar_count: usize,
|
||
feature_dim: usize,
|
||
created_at: chrono::DateTime<Utc>,
|
||
data_hash: String,
|
||
}
|