|
1 | 1 | #[cfg(test)] |
2 | 2 | mod tests { |
| 3 | + use chrono::{Duration, Utc}; |
3 | 4 | use spice_rs::new_spice_client; |
4 | 5 | use std::env; |
5 | 6 | use std::ops::{Add, Sub}; |
6 | 7 | use std::path::Path; |
7 | | - use chrono::{Duration, Utc}; |
8 | 8 |
|
9 | 9 | #[tokio::test] |
10 | 10 | async fn test_readme() { |
11 | 11 | dotenv::from_path(Path::new(".env.local")).ok(); |
12 | 12 | let api_key = env::var("API_KEY").expect("API_KEY not found"); |
13 | | - |
14 | | - match new_spice_client(api_key).await{ |
15 | | - Ok(mut client) => { |
16 | | - let data = client.query("SELECT * FROM eth.recent_blocks LIMIT 10;".to_string()).await; |
17 | | - if data.is_err() { |
18 | | - assert!(false, "failed to query: {:#?}", data.expect_err("")) |
19 | | - } |
20 | | - let supported_pairs = client.prices.get_supported_pairs().await; |
21 | | - if supported_pairs.is_err() { |
22 | | - assert!(false, "failed to get supported pairs: {:#?}", supported_pairs.expect_err("")) |
23 | | - } |
24 | | - let price_data = client.prices.get_prices(&["BTC-USDC"]).await; |
25 | | - if price_data.is_err() { |
26 | | - assert!(false, "failed to get prices: {:#?}", price_data.expect_err("")) |
27 | | - } |
28 | | - let historical_price_data = client.prices.get_historical_prices(&["BTC-USDC"], Option::None, Option::None, Option::None).await; |
29 | | - if historical_price_data.is_err() { |
30 | | - assert!(false, "failed to get prices: {:#?}", historical_price_data.expect_err("")) |
31 | | - } |
32 | | - let now = Utc::now(); |
33 | | - let start = now.sub(Duration::seconds(3600)); |
34 | 13 |
|
35 | | - let historical_price_data = client.prices.get_historical_prices(&["BTC-USDC"], Some(start),Some(now), Option::None).await; |
36 | | - if historical_price_data.is_err() { |
37 | | - assert!(false, "failed to get prices: {:#?}", historical_price_data.expect_err("")) |
38 | | - } |
39 | | - } |
40 | | - Err(e) => { |
41 | | - assert!(false, "Error: {:#?}", e); |
42 | | - } |
| 14 | + let mut client = new_spice_client(api_key).await; |
| 15 | + let data = client |
| 16 | + .query("SELECT * FROM eth.recent_blocks LIMIT 10;".to_string()) |
| 17 | + .await; |
| 18 | + if data.is_err() { |
| 19 | + assert!(false, "failed to query: {:#?}", data.expect_err("")) |
| 20 | + } |
| 21 | + let supported_pairs = client.prices.get_supported_pairs().await; |
| 22 | + if supported_pairs.is_err() { |
| 23 | + assert!( |
| 24 | + false, |
| 25 | + "failed to get supported pairs: {:#?}", |
| 26 | + supported_pairs.expect_err("") |
| 27 | + ) |
| 28 | + } |
| 29 | + let price_data = client.prices.get_prices(&["BTC-USDC"]).await; |
| 30 | + if price_data.is_err() { |
| 31 | + assert!( |
| 32 | + false, |
| 33 | + "failed to get prices: {:#?}", |
| 34 | + price_data.expect_err("") |
| 35 | + ) |
| 36 | + } |
| 37 | + let historical_price_data = client |
| 38 | + .prices |
| 39 | + .get_historical_prices(&["BTC-USDC"], Option::None, Option::None, Option::None) |
| 40 | + .await; |
| 41 | + if historical_price_data.is_err() { |
| 42 | + assert!( |
| 43 | + false, |
| 44 | + "failed to get prices: {:#?}", |
| 45 | + historical_price_data.expect_err("") |
| 46 | + ) |
| 47 | + } |
| 48 | + let now = Utc::now(); |
| 49 | + let start = now.sub(Duration::seconds(3600)); |
| 50 | + |
| 51 | + let historical_price_data = client |
| 52 | + .prices |
| 53 | + .get_historical_prices(&["BTC-USDC"], Some(start), Some(now), Option::None) |
| 54 | + .await; |
| 55 | + if historical_price_data.is_err() { |
| 56 | + assert!( |
| 57 | + false, |
| 58 | + "failed to get prices: {:#?}", |
| 59 | + historical_price_data.expect_err("") |
| 60 | + ) |
43 | 61 | } |
44 | | - } |
| 62 | + } |
45 | 63 | } |
0 commit comments