Module tests

Source
Expand description

Testing utilities for working with database handlers in a mock environment

This module provides ergonomic APIs for working with database tests, allowing you to create seamless test interactions with databases.

§Examples

Using the direct API with setup_async and transaction methods:

use testkit_core::*;

#[tokio::test]
async fn test_database() {
    let backend = MockBackend::new();
    
    // Direct API with setup_async and transaction methods (no boxed_async needed)
    let ctx = with_boxed_database(backend)
        .setup_async(|conn| async {
            println!("Setting up database");
            Ok(())
        })
        .transaction(|conn| async {
            println!("Running transaction");
            Ok(())
        })
        .run()
        .await
        .expect("Test failed");
}

Using the db_test! macro for a clean entry point:

use testkit_core::*;

#[tokio::test]
async fn test_with_macro() {
    let backend = MockBackend::new();
     
    // Variable capture works seamlessly
    let table_name = "users".to_string();
    
    // Using db_test! macro as a more readable entry point
    let ctx = db_test!(backend)
        .setup_async(|conn| async move {
            println!("Creating table: {}", table_name);
            Ok(())
        })
        .transaction(|conn| async {
            println!("Running transaction");
            Ok(())
        })
        .run()
        .await
        .expect("Test failed");
}

For more examples, check the tests/ergonomic_api.rs file.

Modules§

mock