|
| 1 | +use aggregator_core::{Ask, Bid, Exchange, PriceLevelUpdate}; |
| 2 | +use chrono::Utc; |
| 3 | +use uuid::Uuid; |
| 4 | + |
| 5 | +/// Common test utilities for exchange connector tests |
| 6 | +
|
| 7 | +pub fn create_test_price_level_update(exchange: Exchange, symbol: &str) -> PriceLevelUpdate { |
| 8 | + PriceLevelUpdate { |
| 9 | + id: Uuid::new_v4(), |
| 10 | + symbol: symbol.to_string(), |
| 11 | + exchange, |
| 12 | + bids: vec![ |
| 13 | + Bid { |
| 14 | + price: 50000.0, |
| 15 | + quantity: 1.0, |
| 16 | + exchange, |
| 17 | + timestamp: Utc::now(), |
| 18 | + }, |
| 19 | + Bid { |
| 20 | + price: 49999.0, |
| 21 | + quantity: 2.0, |
| 22 | + exchange, |
| 23 | + timestamp: Utc::now(), |
| 24 | + }, |
| 25 | + ], |
| 26 | + asks: vec![ |
| 27 | + Ask { |
| 28 | + price: 50001.0, |
| 29 | + quantity: 1.5, |
| 30 | + exchange, |
| 31 | + timestamp: Utc::now(), |
| 32 | + }, |
| 33 | + Ask { |
| 34 | + price: 50002.0, |
| 35 | + quantity: 0.5, |
| 36 | + exchange, |
| 37 | + timestamp: Utc::now(), |
| 38 | + }, |
| 39 | + ], |
| 40 | + timestamp: Utc::now(), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +pub fn assert_price_level_update_valid(update: &PriceLevelUpdate) { |
| 45 | + assert!(!update.symbol.is_empty()); |
| 46 | + assert!(!update.bids.is_empty() || !update.asks.is_empty()); |
| 47 | + |
| 48 | + // Validate bids are in descending order (highest price first) |
| 49 | + for window in update.bids.windows(2) { |
| 50 | + assert!( |
| 51 | + window[0].price >= window[1].price, |
| 52 | + "Bids should be in descending price order" |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + // Validate asks are in ascending order (lowest price first) |
| 57 | + for window in update.asks.windows(2) { |
| 58 | + assert!( |
| 59 | + window[0].price <= window[1].price, |
| 60 | + "Asks should be in ascending price order" |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + // Validate all prices and quantities are positive |
| 65 | + for bid in &update.bids { |
| 66 | + assert!(bid.price > 0.0, "Bid price must be positive"); |
| 67 | + assert!(bid.quantity >= 0.0, "Bid quantity must be non-negative"); |
| 68 | + } |
| 69 | + |
| 70 | + for ask in &update.asks { |
| 71 | + assert!(ask.price > 0.0, "Ask price must be positive"); |
| 72 | + assert!(ask.quantity >= 0.0, "Ask quantity must be non-negative"); |
| 73 | + } |
| 74 | +} |
0 commit comments