Skip to content

Commit

Permalink
add tests for async client
Browse files Browse the repository at this point in the history
  • Loading branch information
reubenwong97 committed Oct 18, 2023
1 parent 343ec73 commit a47755f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 deletions.
23 changes: 23 additions & 0 deletions src/marketstack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,27 @@ impl AsyncMarketstack {
};
call().map_err(api::ApiError::client).await
}

/// Create a new AyncMarketstack API representation.
///
/// The `token` should be a valid [personal access token](https://marketstack.com/documentation).
/// Errors out if `token` is invalid.
pub async fn new<H, T>(host: H, token: T) -> MarketstackResult<Self>
where
H: AsRef<str>,
T: Into<String>,
{
Self::new_impl("https", host.as_ref(), Auth::Token(token.into())).await
}

/// Create a new non-SSL AsyncMarketstack API representation.
///
/// A `token` will still be required for insecure access.
pub async fn new_insecure<H, T>(host: H, token: T) -> MarketstackResult<Self>
where
H: AsRef<str>,
T: Into<String>,
{
Self::new_impl("http", host.as_ref(), Auth::Token(token.into())).await
}
}
18 changes: 0 additions & 18 deletions src/test/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ impl ExpectedUrlBuilder {
.extend(pairs.iter().cloned().map(|(k, v)| (k.into(), v.into())));
self
}

pub fn body_str(&mut self, body: &str) -> &mut Self {
self.body = Some(body.bytes().collect());
self
}
}

impl ExpectedUrl {
Expand Down Expand Up @@ -249,19 +244,6 @@ pub struct PagedTestClient<T> {
const KEYSET_QUERY_PARAM: &str = "__test_keyset";
const DEFAULT_PAGE_SIZE: usize = 20;

impl<T> PagedTestClient<T> {
pub fn new_raw<I>(expected: ExpectedUrl, data: I) -> Self
where
I: IntoIterator<Item = T>,
{
Self {
expected,
data: data.into_iter().collect(),
auth: Auth::Token("".into()),
}
}
}

impl<T> RestClient for PagedTestClient<T> {
type Error = TestClientError;

Expand Down
41 changes: 39 additions & 2 deletions tests/eod_test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use marketstack::api::{eod, Query};
use marketstack::{EodData, Marketstack};
use marketstack::api::{eod, AsyncQuery, Query};
use marketstack::{AsyncMarketstack, EodData, Marketstack};

mod setup;

Expand Down Expand Up @@ -35,3 +35,40 @@ fn test_eod_paged() {
assert_eq!(eod_result.pagination.limit, 5);
assert_eq!(eod_result.data.len(), 5);
}

#[tokio::test]
#[ignore]
async fn test_async_eod() {
let api_key = setup::setup_key();
let client = AsyncMarketstack::new_insecure("api.marketstack.com", api_key)
.await
.unwrap();

let endpoint = eod::Eod::builder().symbol("AAPL").build().unwrap();
let eod_result: EodData = endpoint.query_async(&client).await.unwrap();

assert_eq!(eod_result.pagination.limit, 100);
assert_eq!(eod_result.pagination.offset, 0);

assert_eq!(eod_result.data.len(), 100);
}

#[tokio::test]
#[ignore]
async fn test_async_eod_paged() {
let api_key = setup::setup_key();
let client = AsyncMarketstack::new_insecure("api.marketstack.com", api_key)
.await
.unwrap();

let endpoint = eod::Eod::builder()
.symbol("AAPL")
.limit(5)
.unwrap()
.build()
.unwrap();
let eod_result: EodData = endpoint.query_async(&client).await.unwrap();

assert_eq!(eod_result.pagination.limit, 5);
assert_eq!(eod_result.data.len(), 5);
}

0 comments on commit a47755f

Please sign in to comment.