Module django_query::mock
source · [−]Expand description
Create Django-style endpoints using wiremock.
This module provides Endpoint which can function as an endpoint in wiremock (it implements Respond). It will dissect any URLs given to it, pulling out:
- pagination requests (
limit
,offset
) which it handles itself. - sort requests (
ordering
) which it uses OrderingSet to parse. - filtering requests which it uses OperatorSet to parse.
Finally, it uses IntoRow to generate the response table as JSON, in a similar way to Django.
Example
use django_query::{IntoRow, Filterable, Sortable, mock::Endpoint};
use std::sync::Arc;
use wiremock::{Mock, MockServer, matchers, http::Url};
#[derive(IntoRow, Filterable, Sortable)]
struct Foo {
#[django(sort, op(in, lt, gt))]
a: i32
}
let foos = (0..20i32).into_iter().map(|a| Foo { a }).collect::<Vec<_>>();
tokio_test::block_on( async {
let server = MockServer::start().await;
Mock::given(matchers::method("GET"))
.respond_with(Endpoint::new(Arc::new(foos), Some(&server.uri())))
.mount(&server)
.await;
let u = format!("{}?limit=1&offset=5&a__lt=10&ordering=-a", server.uri());
let body: serde_json::Value = reqwest::get(&u)
.await
.expect("error getting response")
.json()
.await
.expect("error parsing response");
let prev = format!("{}/?limit=1&offset=4&a__lt=10&ordering=-a", server.uri());
let next = format!("{}/?limit=1&offset=6&a__lt=10&ordering=-a", server.uri());
assert_eq!(body, serde_json::json!{
{
"count": 1,
"next": next,
"prev": prev,
"results": [
{ "a": 4 }
]
}
});
});
Structs
A Django-style Wiremock endpoint for a collection of suitable objects.
A Django nested route.
Use a regex to match a query path, mutating both the path and query string.
Enums
Traits
Something which can provide a collection of objects on demand.