Commit cba9c3a1 authored by Ben Boeckel's avatar Ben Boeckel
Browse files

api/paged: add pagination tests

parent 9071e079
......@@ -305,7 +305,7 @@ mod tests {
use crate::api::endpoint_prelude::*;
use crate::api::paged::LinkHeader;
use crate::api::{self, ApiError, LinkHeaderParseError, Pagination, Query};
use crate::test::client::{ExpectedUrl, SingleTestClient};
use crate::test::client::{ExpectedUrl, PagedTestClient, SingleTestClient};
#[test]
fn test_link_header_no_brackets() {
......@@ -518,4 +518,138 @@ mod tests {
panic!("unexpected error: {}", err);
}
}
#[test]
fn test_pagination_limit() {
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(&client)
.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()
.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(&client).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()
.endpoint("paged_dummy")
.paginated(true)
.build()
.unwrap();
let client = PagedTestClient::new_raw(
endpoint,
(0..=255).map(|value| {
DummyResult {
value,
}
}),
);
let query = Dummy {
with_keyset: true,
};
let res: Vec<DummyResult> = api::paged(query, Pagination::Limit(25))
.query(&client)
.unwrap();
assert_eq!(res.len(), 25);
for (i, value) in res.iter().enumerate() {
assert_eq!(value.value, i as u8);
}
}
#[test]
fn test_keyset_pagination_all() {
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: true,
};
let res: Vec<DummyResult> = api::paged(query, Pagination::All).query(&client).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_missing_header() {
let endpoint = ExpectedUrl::builder()
.endpoint("paged_dummy")
.add_query_params(&[("pagination", "keyset"), ("per_page", "100")])
.build()
.unwrap();
let data: Vec<_> = (0..=255)
.map(|value| {
DummyResult {
value,
}
})
.collect();
let client = SingleTestClient::new_raw(endpoint, serde_json::to_vec(&data).unwrap());
let query = Dummy {
with_keyset: true,
};
let res: Vec<DummyResult> = api::paged(query, Pagination::Limit(300))
.query(&client)
.unwrap();
assert_eq!(res.len(), 256);
for (i, value) in res.iter().enumerate() {
assert_eq!(value.value, i as u8);
}
}
}
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