From 62036b7760fceae41c33a32586c91c8d897feb9d Mon Sep 17 00:00:00 2001 From: Reuben Wong Date: Fri, 20 Oct 2023 21:24:28 +0800 Subject: [PATCH] complete splits endpoint implementation --- src/types.rs | 36 +++++++++++++-------------- tests/splits.rs | 65 +++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 20 deletions(-) diff --git a/src/types.rs b/src/types.rs index b360226..ec096dc 100644 --- a/src/types.rs +++ b/src/types.rs @@ -20,36 +20,36 @@ pub struct PaginationInfo { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct EodDataItem { /// Exact date/time the given data was collected in ISO-8601 format. - date: DateTime, + pub date: DateTime, /// Stock ticker symbol of the current data object. - symbol: String, + pub symbol: String, /// Exchange MIC identification associated with the current data object. - exchange: String, + pub exchange: String, /// Split factor used to adjust prices when a company splits, reverse splits or pays a /// distribution. - split_factor: f64, + pub split_factor: f64, /// Distribution of earnings to shareholders. - dividend: f64, + pub dividend: f64, /// Raw opening price of the given stock ticker. - open: f64, + pub open: f64, /// Raw high price of the given stock ticker. - high: f64, + pub high: f64, /// Raw low price of the given stock ticker. - low: f64, + pub low: f64, /// Raw closing price of the given stock ticker. - close: f64, + pub close: f64, /// Raw volume of the given stock ticker. - volume: f64, + pub volume: f64, /// Adjusted opening price of the given stock ticker. - adj_open: f64, + pub adj_open: f64, /// Adjusted high price of the given stock ticker. - adj_high: f64, + pub adj_high: f64, /// Adjusted low price of the given stock ticker. - adj_low: f64, + pub adj_low: f64, /// Adjusted closing price of the given stock ticker. - adj_close: f64, + pub adj_close: f64, /// Adjusted volume of the given stock ticker. - adj_volume: f64, + pub adj_volume: f64, } /// Rust representation of the JSON response from `eod` marketstack endpoint. @@ -65,11 +65,11 @@ pub struct EodData { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct SplitsDataItem { /// Exact date/time the given data was collected in ISO-8601 format. - date: NaiveDate, + pub date: NaiveDate, /// Split factor for that symbol on the date. - split_factor: f64, + pub split_factor: f64, /// Stock ticker symbol of the current data object. - symbol: String, + pub symbol: String, } /// Rust representation of the JSON response from `splits` marketstack endpoint. diff --git a/tests/splits.rs b/tests/splits.rs index cc2255e..add8494 100644 --- a/tests/splits.rs +++ b/tests/splits.rs @@ -1,5 +1,66 @@ -use marketstack::api::common::SortOrder; +use chrono::NaiveDate; + use marketstack::api::{splits, AsyncQuery, Query}; -use marketstack::{AsyncMarketstack, EodData, Marketstack}; +use marketstack::{AsyncMarketstack, Marketstack, SplitsData}; mod setup; + +#[test] +#[ignore] +fn test_splits() { + let api_key = setup::setup_key(); + let client = Marketstack::new_insecure("api.marketstack.com", api_key).unwrap(); + + let endpoint = splits::Splits::builder() + .symbol("AAPL") + .limit(3) + .unwrap() + .build() + .unwrap(); + let splits_result: SplitsData = endpoint.query(&client).unwrap(); + + assert_eq!(splits_result.pagination.limit, 3); + assert_eq!(splits_result.pagination.offset, 0); + + assert_eq!(splits_result.data.len(), 3); +} + +#[tokio::test] +#[ignore] +async fn test_async_splits() { + let api_key = setup::setup_key(); + let client = AsyncMarketstack::new_insecure("api.marketstack.com", api_key) + .await + .unwrap(); + + let endpoint = splits::Splits::builder() + .limit(3) + .unwrap() + .symbol("AAPL") + .build() + .unwrap(); + let eod_result: SplitsData = endpoint.query_async(&client).await.unwrap(); + + assert_eq!(eod_result.pagination.limit, 3); + assert_eq!(eod_result.pagination.offset, 0); + + assert_eq!(eod_result.data.len(), 3); +} + +#[test] +#[ignore] +fn test_splits_date() { + let api_key = setup::setup_key(); + let client = Marketstack::new_insecure("api.marketstack.com", api_key).unwrap(); + + let endpoint = splits::Splits::builder() + .symbol("AAPL") + .date_from(NaiveDate::from_ymd_opt(2020, 8, 29).unwrap()) + .date_to(NaiveDate::from_ymd_opt(2020, 9, 2).unwrap()) + .build() + .unwrap(); + let splits_result: SplitsData = endpoint.query(&client).unwrap(); + + assert_eq!(splits_result.data.len(), 1); + assert_eq!(splits_result.data[0].split_factor, 4.0); +}