Commit d3fd1108 authored by boxdot's avatar boxdot Committed by Ben Boeckel
Browse files

Add async tests to Endpoint, Ignore, Paged, Raw.

parent 7294cae9
......@@ -47,3 +47,4 @@ url = "^2.1"
[dev-dependencies]
itertools = { version = "~0.8" }
tokio = { version = "1.4.0", features = ["macros", "rt-multi-thread"] }
......@@ -100,7 +100,7 @@ mod tests {
use serde_json::json;
use crate::api::endpoint_prelude::*;
use crate::api::{ApiError, Query};
use crate::api::{ApiError, AsyncQuery, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
struct Dummy;
......@@ -294,4 +294,18 @@ mod tests {
let res: DummyResult = Dummy.query(&client).unwrap();
assert_eq!(res.value, 0);
}
#[tokio::test]
async fn test_good_deserialization_async() {
let endpoint = ExpectedUrl::builder().endpoint("dummy").build().unwrap();
let client = SingleTestClient::new_json(
endpoint,
&json!({
"value": 0,
}),
);
let res: DummyResult = Dummy.query_async(&client).await.unwrap();
assert_eq!(res.value, 0);
}
}
......@@ -85,7 +85,7 @@ mod tests {
use serde_json::json;
use crate::api::endpoint_prelude::*;
use crate::api::{self, ApiError, Query};
use crate::api::{self, ApiError, AsyncQuery, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
struct Dummy;
......@@ -113,6 +113,14 @@ mod tests {
api::ignore(Dummy).query(&client).unwrap()
}
#[tokio::test]
async fn test_gitlab_non_json_response_async() {
let endpoint = ExpectedUrl::builder().endpoint("dummy").build().unwrap();
let client = SingleTestClient::new_raw(endpoint, "not json");
api::ignore(Dummy).query_async(&client).await.unwrap()
}
#[test]
fn test_gitlab_error_bad_json() {
let endpoint = ExpectedUrl::builder()
......
......@@ -270,7 +270,7 @@ impl<E, T, C> AsyncQuery<Vec<T>, C> for Paged<E>
where
E: Endpoint + Sync,
E: Pageable,
T: DeserializeOwned + Send,
T: DeserializeOwned + Send + 'static,
C: AsyncClient + Sync,
{
async fn query_async(&self, client: &C) -> Result<Vec<T>, ApiError<C::Error>> {
......@@ -401,7 +401,7 @@ mod tests {
use crate::api::endpoint_prelude::*;
use crate::api::paged::LinkHeader;
use crate::api::{self, ApiError, LinkHeaderParseError, Pagination, Query};
use crate::api::{self, ApiError, AsyncQuery, LinkHeaderParseError, Pagination, Query};
use crate::test::client::{ExpectedUrl, PagedTestClient, SingleTestClient};
#[test]
......@@ -642,6 +642,35 @@ mod tests {
}
}
#[tokio::test]
async fn test_pagination_limit_async() {
let endpoint = ExpectedUrl::builder()
.endpoint("paged_dummy")
.paginated(true)
.build()
.unwrap();
let client = PagedTestClient::new_raw(
endpoint,
(0..=255).map(|value| {
DummyResult {
value,
}
}),
);
let query = Dummy {
with_keyset: false,
};
let res: Vec<DummyResult> = api::paged(query, Pagination::Limit(25))
.query_async(&client)
.await
.unwrap();
assert_eq!(res.len(), 25);
for (i, value) in res.iter().enumerate() {
assert_eq!(value.value, i as u8);
}
}
#[test]
fn test_pagination_all() {
let endpoint = ExpectedUrl::builder()
......@@ -666,6 +695,33 @@ mod tests {
}
}
#[tokio::test]
async fn test_pagination_all_async() {
let endpoint = ExpectedUrl::builder()
.endpoint("paged_dummy")
.paginated(true)
.build()
.unwrap();
let client = PagedTestClient::new_raw(
endpoint,
(0..=255).map(|value| {
DummyResult {
value,
}
}),
);
let query = Dummy::default();
let res: Vec<DummyResult> = api::paged(query, Pagination::All)
.query_async(&client)
.await
.unwrap();
assert_eq!(res.len(), 256);
for (i, value) in res.iter().enumerate() {
assert_eq!(value.value, i as u8);
}
}
#[test]
fn test_keyset_pagination_limit() {
let endpoint = ExpectedUrl::builder()
......
......@@ -85,7 +85,7 @@ mod tests {
use serde_json::json;
use crate::api::endpoint_prelude::*;
use crate::api::{self, ApiError, Query};
use crate::api::{self, ApiError, AsyncQuery, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
struct Dummy;
......@@ -114,6 +114,15 @@ mod tests {
itertools::assert_equal(data, "not json".bytes());
}
#[tokio::test]
async fn test_gitlab_non_json_response_async() {
let endpoint = ExpectedUrl::builder().endpoint("dummy").build().unwrap();
let client = SingleTestClient::new_raw(endpoint, "not json");
let data = api::raw(Dummy).query_async(&client).await.unwrap();
itertools::assert_equal(data, "not json".bytes());
}
#[test]
fn test_gitlab_error_bad_json() {
let endpoint = ExpectedUrl::builder()
......
......@@ -9,6 +9,7 @@ use std::cmp;
use std::collections::HashMap;
use std::ops::Range;
use async_trait::async_trait;
use bytes::Bytes;
use derive_builder::Builder;
use http::request::Builder as RequestBuilder;
......@@ -17,7 +18,7 @@ use serde::ser::Serialize;
use thiserror::Error;
use url::Url;
use crate::api::{ApiError, Client};
use crate::api::{ApiError, AsyncClient, Client};
#[derive(Debug, Builder)]
pub struct ExpectedUrl {
......@@ -201,6 +202,23 @@ impl Client for SingleTestClient {
}
}
#[async_trait]
impl AsyncClient for SingleTestClient {
type Error = TestClientError;
fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>> {
<Self as Client>::rest_endpoint(self, endpoint)
}
async fn rest_async(
&self,
request: RequestBuilder,
body: Vec<u8>,
) -> Result<Response<Bytes>, ApiError<Self::Error>> {
<Self as Client>::rest(self, request, body)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Page {
ByNumber { number: usize, size: usize },
......@@ -362,3 +380,23 @@ where
.map(Into::into))
}
}
#[async_trait]
impl<T> AsyncClient for PagedTestClient<T>
where
T: Serialize + Send + Sync,
{
type Error = TestClientError;
fn rest_endpoint(&self, endpoint: &str) -> Result<Url, ApiError<Self::Error>> {
<Self as Client>::rest_endpoint(self, endpoint)
}
async fn rest_async(
&self,
request: RequestBuilder,
body: Vec<u8>,
) -> Result<Response<Bytes>, ApiError<Self::Error>> {
<Self as Client>::rest(self, request, body)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment