#django #mocking #testing

macro django-query_derive

Macros for the django-query crate

5 releases

0.2.1 Jun 1, 2022
0.2.0 Jun 1, 2022
0.1.4 Apr 15, 2022
0.1.3 Apr 11, 2022
0.1.0 Apr 5, 2022

#11 in #django


Used in 3 crates (via django-query)

MIT/Apache

67KB
1.5K SLoC

django-query

A set of tools for creating Django-style mock endpoints. The basic usage is to annotate the type that you want to simulate having a database of, derive the necessary traits, and then create an Endpoint to serve a sortable, filterable database of them.

Example usage:

#[derive(Filterable, IntoRow, Sortable)]
struct Foo {
    #[django(sort, op(lt, gt))]
    a: i32,
    #[django(sort, op(icontains, iexact))]
    b: String,
}

#[tokio::test]
fn simple_test() {
    let server = MockServer::start().await;
    
    // create a Vec of struct Foo here, and optionally 
    // wrap it in an Arc. 
    
    // Now serve our mock database with wiremock
    Mock::given(matchers::method("GET"))
         .respond_with(Endpoint::new(foo_table, Some(&server.uri())));
    
    // And now we can make requests from our code like
    reqwest::get(&format!("{}?limit=5&offset=5&b__icontains=apple&ordering=-a"))
       .await
       .expect("error getting response")
       .json()
       .await
       .expect("error parsing response");
    // which will perform
    //  - pagination (5 responses, offset by 5)
    //  - filtering (only array members whose b member contains apple,
    //    case insensitively)
    //  - sorting (the results will be in decreasing order of the a
    //    member)
}

It integrates with persian-rug and clone-replace to provide mutable collections of interlinked objects.

License

This code is made available under either an Apache-2.0 or an MIT license.

Dependencies

~1.5MB
~37K SLoC