Skip to content

Commit

Permalink
add splits types and corresponding unit tests for deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenwong97 committed Oct 20, 2023
1 parent fe8823a commit 45d7ffa
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "marketstack"
version = "0.0.3"
version = "0.0.4"
edition = "2021"
license = "MIT"
description = "Rust bindings for Marketstack REST API"
Expand Down
90 changes: 84 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
//! Contains Rust types of deserialized responses from Marketstack REST API.

/// Basic struct that acts as dummy.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BasicPublic {}
use chrono::{DateTime, NaiveDate, Utc};
use serde::{Deserialize, Serialize};

/// Pagination Information returned by Marketstack API.
#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -18,6 +16,7 @@ pub struct PaginationInfo {
pub total: u64,
}

/// Rust representation of single data item from Marketstack `eod` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EodDataItem {
/// Exact date/time the given data was collected in ISO-8601 format.
Expand Down Expand Up @@ -53,15 +52,40 @@ pub struct EodDataItem {
adj_volume: f64,
}

/// Rust representation of the JSON response from `eod` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct EodData {
/// Corresponds to pagination entry from JSON response from marketstack.
pub pagination: PaginationInfo,
/// Corresponds to data entry from JSON response from marketstack.
pub data: Vec<EodDataItem>,
}

/// Rust representation of single data item from Marketstack `splits` response.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SplitsDataItem {
/// Exact date/time the given data was collected in ISO-8601 format.
date: NaiveDate,
/// Split factor for that symbol on the date.
split_factor: f64,
/// Stock ticker symbol of the current data object.
symbol: String,
}

/// Rust representation of the JSON response from `splits` marketstack endpoint.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SplitsData {
/// Corresponds to pagination entry from JSON response from marketstack.
pub pagination: PaginationInfo,
/// Corresponds to data entry from JSON response from marketstack.
pub data: Vec<SplitsDataItem>,
}

#[cfg(test)]
mod tests {
use super::EodData;
use chrono::NaiveDate;

use super::{EodData, SplitsData};

#[test]
fn test_deserialize_eod() {
Expand Down Expand Up @@ -98,4 +122,58 @@ mod tests {
assert_eq!(eod_data.data[0].symbol, "AAPL");
assert_eq!(eod_data.pagination.limit, 100);
}

#[test]
fn test_deserialize_splits() {
let json_data = r#"{
"pagination": {
"limit": 100,
"offset": 0,
"count": 5,
"total": 5
},
"data": [
{
"date": "2020-08-31",
"split_factor": 4,
"symbol": "AAPL"
},
{
"date": "2014-06-09",
"split_factor": 7,
"symbol": "AAPL"
},
{
"date": "2005-02-28",
"split_factor": 2,
"symbol": "AAPL"
},
{
"date": "2000-06-21",
"split_factor": 2,
"symbol": "AAPL"
},
{
"date": "1987-06-16",
"split_factor": 2,
"symbol": "AAPL"
}
]
}"#;

let splits_data: SplitsData = serde_json::from_str(json_data).unwrap();
assert_eq!(splits_data.data[0].split_factor, 4.0);
assert_eq!(
splits_data.data[0].date,
NaiveDate::from_ymd_opt(2020, 8, 31).unwrap()
);
assert_eq!(splits_data.data[0].symbol, "AAPL");

assert_eq!(splits_data.data[4].split_factor, 2.0);
assert_eq!(
splits_data.data[4].date,
NaiveDate::from_ymd_opt(1987, 6, 16).unwrap()
);
assert_eq!(splits_data.data[4].symbol, "AAPL");
}
}
5 changes: 5 additions & 0 deletions tests/splits.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use marketstack::api::common::SortOrder;
use marketstack::api::{splits, AsyncQuery, Query};
use marketstack::{AsyncMarketstack, EodData, Marketstack};

mod setup;

0 comments on commit 45d7ffa

Please sign in to comment.