market-data-source 0.3.0

High-performance synthetic market data generator with financial precision. Generate unlimited OHLC candles, tick data, and realistic trading scenarios for backtesting and research.
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
#![allow(unused)]
//! Comprehensive integration tests for all export functionality
//!
//! This file contains end-to-end integration tests that verify the complete
//! export pipeline across all supported formats, including round-trip tests,
//! performance benchmarks, and error handling scenarios.

use market_data_source::{
    MarketDataGenerator, ConfigBuilder, TrendDirection,
    types::{OHLC, Tick},
};
use std::fs;
use tempfile::{tempdir, TempDir};
use std::time::Instant;

/// Test fixture for export integration tests
struct ExportTestFixture {
    generator: MarketDataGenerator,
    temp_dir: TempDir,
    ohlc_data: Vec<OHLC>,
    tick_data: Vec<Tick>,
}

impl ExportTestFixture {
    fn new() -> Self {
        let config = ConfigBuilder::new()
            .starting_price_f64(100.0)
            .volatility_f64(0.02)
            .trend_f64(TrendDirection::Bullish, 0.001)
            .seed(12345)  // Fixed seed for reproducible tests
            .build()
            .unwrap();
        
        let mut generator = MarketDataGenerator::with_config(config).unwrap();
        let temp_dir = tempdir().unwrap();
        
        // Pre-generate test data
        let ohlc_data = generator.generate_series(100);
        let tick_data = generator.generate_ticks(50);
        
        Self {
            generator,
            temp_dir,
            ohlc_data,
            tick_data,
        }
    }
    
    fn get_path(&self, filename: &str) -> std::path::PathBuf {
        self.temp_dir.path().join(filename)
    }
}

#[cfg(feature = "csv_export")]
mod csv_integration_tests {
    use super::*;
    use market_data_source::export::{to_csv_ohlc, to_csv_ticks};
    
    #[test]
    fn test_csv_export_integration() {
        let fixture = ExportTestFixture::new();
        
        // Export OHLC data
        let ohlc_path = fixture.get_path("integration_ohlc.csv");
        let result = to_csv_ohlc(&fixture.ohlc_data, &ohlc_path);
        assert!(result.is_ok(), "CSV OHLC export failed: {result:?}");
        
        // Verify file exists and has correct content
        assert!(ohlc_path.exists());
        let content = fs::read_to_string(&ohlc_path).unwrap();
        let lines: Vec<&str> = content.lines().collect();
        
        // Should have header + data rows
        assert_eq!(lines.len(), fixture.ohlc_data.len() + 1);
        assert!(lines[0].contains("timestamp,open,high,low,close,volume"));
        
        // Export tick data
        let tick_path = fixture.get_path("integration_ticks.csv");
        let result = to_csv_ticks(&fixture.tick_data, &tick_path);
        assert!(result.is_ok(), "CSV tick export failed: {result:?}");
        
        // Verify tick file
        assert!(tick_path.exists());
        let tick_content = fs::read_to_string(&tick_path).unwrap();
        let tick_lines: Vec<&str> = tick_content.lines().collect();
        assert_eq!(tick_lines.len(), fixture.tick_data.len() + 1);
    }
    
    #[test]
    fn test_csv_round_trip() {
        let fixture = ExportTestFixture::new();
        
        // Export original data
        let csv_path = fixture.get_path("roundtrip.csv");
        to_csv_ohlc(&fixture.ohlc_data, &csv_path).unwrap();
        
        // Read back and parse CSV
        use csv::Reader;
        let mut reader = Reader::from_path(&csv_path).unwrap();
        
        let mut parsed_data = Vec::new();
        for record in reader.deserialize() {
            let ohlc: OHLC = record.unwrap();
            parsed_data.push(ohlc);
        }
        
        // Verify data integrity
        assert_eq!(parsed_data.len(), fixture.ohlc_data.len());
        
        // Compare key fields (allowing for float precision differences)
        for (original, parsed) in fixture.ohlc_data.iter().zip(parsed_data.iter()) {
            use rust_decimal::prelude::*;
            assert!((original.open - parsed.open).abs() < Decimal::from_str("0.001").unwrap());
            assert!((original.high - parsed.high).abs() < Decimal::from_str("0.001").unwrap());
            assert!((original.low - parsed.low).abs() < Decimal::from_str("0.001").unwrap());
            assert!((original.close - parsed.close).abs() < Decimal::from_str("0.001").unwrap());
            assert_eq!(original.volume, parsed.volume);
        }
    }
}

#[cfg(feature = "json_export")]
mod json_integration_tests {
    use super::*;
    use market_data_source::export::{to_json_ohlc, to_jsonl_ohlc};
    
    
    #[test]
    fn test_json_export_integration() {
        let fixture = ExportTestFixture::new();
        
        // Test standard JSON export
        let json_path = fixture.get_path("integration.json");
        let result = to_json_ohlc(&fixture.ohlc_data, &json_path);
        assert!(result.is_ok(), "JSON export failed: {result:?}");
        
        // Verify file exists and is valid JSON
        assert!(json_path.exists());
        let content = fs::read_to_string(&json_path).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
        
        // Should be an array with correct length
        assert!(parsed.is_array());
        assert_eq!(parsed.as_array().unwrap().len(), fixture.ohlc_data.len());
        
        // Test JSON Lines export
        let jsonl_path = fixture.get_path("integration.jsonl");
        let result = to_jsonl_ohlc(&fixture.ohlc_data, &jsonl_path);
        assert!(result.is_ok(), "JSONL export failed: {result:?}");
        
        // Verify JSONL format
        let jsonl_content = fs::read_to_string(&jsonl_path).unwrap();
        let jsonl_lines: Vec<&str> = jsonl_content.lines().collect();
        assert_eq!(jsonl_lines.len(), fixture.ohlc_data.len());
        
        // Each line should be valid JSON
        for line in jsonl_lines {
            let parsed: serde_json::Value = serde_json::from_str(line).unwrap();
            assert!(parsed.is_object());
        }
    }
    
    #[test]
    fn test_json_round_trip() {
        let fixture = ExportTestFixture::new();
        
        // Export to JSON
        let json_path = fixture.get_path("json_roundtrip.json");
        to_json_ohlc(&fixture.ohlc_data, &json_path).unwrap();
        
        // Read back and parse
        let content = fs::read_to_string(&json_path).unwrap();
        let parsed_data: Vec<OHLC> = serde_json::from_str(&content).unwrap();
        
        // Verify data integrity
        assert_eq!(parsed_data.len(), fixture.ohlc_data.len());
        
        // JSON should preserve values with reasonable precision
        for (original, parsed) in fixture.ohlc_data.iter().zip(parsed_data.iter()) {
            use rust_decimal::prelude::*;
            assert!((original.open - parsed.open).abs() < Decimal::from_str("0.0000000001").unwrap());
            assert!((original.high - parsed.high).abs() < Decimal::from_str("0.0000000001").unwrap());
            assert!((original.low - parsed.low).abs() < Decimal::from_str("0.0000000001").unwrap());
            assert!((original.close - parsed.close).abs() < Decimal::from_str("0.0000000001").unwrap());
            assert_eq!(original.volume, parsed.volume);
            assert_eq!(original.timestamp, parsed.timestamp);
        }
    }
}

#[cfg(feature = "png_export")]
mod png_integration_tests {
    use super::*;
    use market_data_source::export::{to_png_ohlc, to_png_ticks};
    
    #[test]
    fn test_png_export_integration() {
        let fixture = ExportTestFixture::new();
        
        // Test OHLC chart export
        let ohlc_chart_path = fixture.get_path("integration_candlestick.png");
        let result = to_png_ohlc(&fixture.ohlc_data, &ohlc_chart_path);
        assert!(result.is_ok(), "PNG OHLC export failed: {result:?}");
        
        // Verify PNG file
        assert!(ohlc_chart_path.exists());
        let file_size = fs::metadata(&ohlc_chart_path).unwrap().len();
        assert!(file_size > 1000, "PNG file too small: {file_size} bytes");
        
        // Verify PNG magic bytes
        let content = fs::read(&ohlc_chart_path).unwrap();
        assert!(content.len() >= 8);
        assert_eq!(&content[0..8], &[0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
        
        // Test tick chart export
        let tick_chart_path = fixture.get_path("integration_line.png");
        let result = to_png_ticks(&fixture.tick_data, &tick_chart_path);
        assert!(result.is_ok(), "PNG tick export failed: {result:?}");
        
        // Verify tick chart
        assert!(tick_chart_path.exists());
        let tick_size = fs::metadata(&tick_chart_path).unwrap().len();
        assert!(tick_size > 1000, "Tick chart too small: {tick_size} bytes");
    }
    
    #[test]
    fn test_png_custom_options() {
        use market_data_source::export::{ChartBuilder, to_png_ohlc_with_builder};
        
        let fixture = ExportTestFixture::new();
        
        // Create custom chart builder
        let builder = ChartBuilder::new()
            .title("Integration Test Chart")
            .width(800)
            .height(600)
            .show_volume(true);
        
        let custom_path = fixture.get_path("custom_chart.png");
        let result = to_png_ohlc_with_builder(&fixture.ohlc_data, &custom_path, builder);
        assert!(result.is_ok(), "Custom PNG export failed: {result:?}");
        
        // Verify custom chart
        assert!(custom_path.exists());
        let file_size = fs::metadata(&custom_path).unwrap().len();
        assert!(file_size > 2000, "Custom chart too small: {file_size} bytes");
    }
}

#[cfg(feature = "couchdb")]
mod couchdb_integration_tests {
    use super::*;
    use market_data_source::export::{to_couchdb_ohlc, to_couchdb_ticks};
    
    #[test]
    fn test_couchdb_export_integration() {
        let fixture = ExportTestFixture::new();
        
        // Note: These tests will fail if CouchDB is not running
        // In CI, we should either mock this or make it conditional
        let server_url = "http://localhost:5984";
        let test_db = "integration_test";
        
        // Test OHLC export (may fail if server not available)
        let result = to_couchdb_ohlc(&fixture.ohlc_data[..5], server_url, test_db);
        match result {
            Ok(_) => {
                // CouchDB is available and export succeeded
                println!("CouchDB export successful");
            }
            Err(e) => {
                // Expected if CouchDB is not running
                println!("CouchDB export failed (expected): {e}");
                // Don't fail the test, just log it
            }
        }
        
        // Similar for tick data
        let result = to_couchdb_ticks(&fixture.tick_data[..3], server_url, "tick_test");
        match result {
            Ok(_) => println!("CouchDB tick export successful"),
            Err(e) => println!("CouchDB tick export failed (expected): {e}"),
        }
    }
}

/// Performance benchmark tests for large datasets
mod performance_tests {
    use super::*;
    
    #[test]
    fn test_large_dataset_performance() {
        let config = ConfigBuilder::new()
            .starting_price_f64(100.0)
            .seed(99999)
            .build()
            .unwrap();
        
        let mut generator = MarketDataGenerator::with_config(config).unwrap();
        let temp_dir = tempdir().unwrap();
        
        // Generate large dataset
        let large_data = generator.generate_series(5000);
        println!("Generated {} records for performance test", large_data.len());
        
        // Benchmark CSV export
        #[cfg(feature = "csv_export")]
        {
            use market_data_source::export::to_csv_ohlc;
            
            let csv_path = temp_dir.path().join("perf_test.csv");
            let start = Instant::now();
            
            let result = to_csv_ohlc(&large_data, &csv_path);
            let duration = start.elapsed();
            
            assert!(result.is_ok(), "Large CSV export failed");
            assert!(csv_path.exists());
            
            let records_per_sec = large_data.len() as f64 / duration.as_secs_f64();
            println!("CSV Performance: {records_per_sec:.0} records/sec ({duration:?})");
            
            // Should handle at least 1000 records per second
            assert!(records_per_sec > 1000.0, "CSV export too slow: {records_per_sec:.0} rec/sec");
        }
        
        // Benchmark JSON export
        #[cfg(feature = "json_export")]
        {
            use market_data_source::export::to_json_ohlc;
            
            let json_path = temp_dir.path().join("perf_test.json");
            let start = Instant::now();
            
            let result = to_json_ohlc(&large_data, &json_path);
            let duration = start.elapsed();
            
            assert!(result.is_ok(), "Large JSON export failed");
            assert!(json_path.exists());
            
            let records_per_sec = large_data.len() as f64 / duration.as_secs_f64();
            println!("JSON Performance: {records_per_sec:.0} records/sec ({duration:?})");
            
            // JSON might be slower due to more formatting
            assert!(records_per_sec > 500.0, "JSON export too slow: {records_per_sec:.0} rec/sec");
        }
        
        // Benchmark PNG export (smaller dataset)
        #[cfg(feature = "png_export")]
        {
            use market_data_source::export::to_png_ohlc;
            
            let chart_data = &large_data[..1000];  // Charts don't need all data
            let png_path = temp_dir.path().join("perf_chart.png");
            let start = Instant::now();
            
            let result = to_png_ohlc(chart_data, &png_path);
            let duration = start.elapsed();
            
            assert!(result.is_ok(), "Large PNG export failed");
            assert!(png_path.exists());
            
            println!("PNG Chart Performance: {} records in {:?}", chart_data.len(), duration);
            
            // Charts should complete within reasonable time
            assert!(duration.as_secs() < 30, "PNG export took too long: {duration:?}");
        }
    }
}

/// Error handling and edge case tests
mod error_handling_tests {
    use super::*;
    
    #[test]
    fn test_empty_data_export() {
        let temp_dir = tempdir().unwrap();
        let empty_ohlc: Vec<OHLC> = vec![];
        let _empty_ticks: Vec<Tick> = vec![];
        
        // CSV should handle empty data gracefully
        #[cfg(feature = "csv_export")]
        {
            use market_data_source::export::to_csv_ohlc;
            
            let csv_path = temp_dir.path().join("empty.csv");
            let result = to_csv_ohlc(&empty_ohlc, &csv_path);
            
            match result {
                Ok(_) => {
                    // Should create file with just headers
                    assert!(csv_path.exists());
                    let content = fs::read_to_string(&csv_path).unwrap();
                    let lines: Vec<&str> = content.lines().collect();
                    assert_eq!(lines.len(), 1); // Just the header
                }
                Err(e) => {
                    println!("Empty CSV export handling: {e}");
                    // Some exporters might reject empty data, which is acceptable
                }
            }
        }
        
        // JSON should handle empty arrays
        #[cfg(feature = "json_export")]
        {
            use market_data_source::export::to_json_ohlc;
            
            let json_path = temp_dir.path().join("empty.json");
            let result = to_json_ohlc(&empty_ohlc, &json_path);
            
            match result {
                Ok(_) => {
                    assert!(json_path.exists());
                    let content = fs::read_to_string(&json_path).unwrap();
                    let parsed: serde_json::Value = serde_json::from_str(&content).unwrap();
                    assert!(parsed.is_array());
                    assert_eq!(parsed.as_array().unwrap().len(), 0);
                }
                Err(e) => {
                    println!("Empty JSON export handling: {e}");
                }
            }
        }
    }
    
    #[test]
    fn test_invalid_path_handling() {
        let fixture = ExportTestFixture::new();
        
        // Test with invalid/read-only path
        let invalid_path = if cfg!(windows) {
            "C:\\invalid\\path\\file.csv"
        } else {
            "/root/invalid/path/file.csv"
        };
        
        #[cfg(feature = "csv_export")]
        {
            use market_data_source::export::to_csv_ohlc;
            let result = to_csv_ohlc(&fixture.ohlc_data, invalid_path);
            assert!(result.is_err(), "Should fail with invalid path");
        }
        
        #[cfg(feature = "json_export")]
        {
            use market_data_source::export::to_json_ohlc;
            let result = to_json_ohlc(&fixture.ohlc_data, invalid_path);
            assert!(result.is_err(), "Should fail with invalid path");
        }
    }
}

/// Integration test runner that provides a summary
#[test]
#[cfg(any(feature = "csv_export", feature = "json_export", feature = "png_export", feature = "couchdb"))]
fn test_export_integration_summary() {
    println!("Running Export Integration Test Summary");
    println!("=====================================");
    
    let mut enabled_features: Vec<&str> = vec![];
    let mut disabled_features: Vec<&str> = vec![];
    
    #[cfg(feature = "csv_export")]
    enabled_features.push("csv_export");
    #[cfg(not(feature = "csv_export"))]
    disabled_features.push("csv_export");
    
    #[cfg(feature = "json_export")]
    enabled_features.push("json_export");
    #[cfg(not(feature = "json_export"))]
    disabled_features.push("json_export");
    
    #[cfg(feature = "png_export")]
    enabled_features.push("png_export");
    #[cfg(not(feature = "png_export"))]
    disabled_features.push("png_export");
    
    #[cfg(feature = "couchdb")]
    enabled_features.push("couchdb");
    #[cfg(not(feature = "couchdb"))]
    disabled_features.push("couchdb");
    
    println!("Enabled features: {enabled_features:?}");
    println!("Disabled features: {disabled_features:?}");
    
    // Basic test to ensure at least one format works
    assert!(!enabled_features.is_empty(), "At least one export feature should be enabled for integration tests");
    
    println!("✅ Export integration tests completed successfully");
}

#[test]
#[cfg(not(any(feature = "csv_export", feature = "json_export", feature = "png_export", feature = "couchdb")))]
fn test_no_export_features() {
    println!("Running with no export features enabled");
    println!("This is valid for the base library configuration");
}