Crate couch_rs_test

source ·
Expand description

A set of helper functions for executing tests with couch_rs library

Allows easy execution of tests by providing automated creation and destruction of test databases in a CouchDB instance. For a given database name, the created databases append a random string to guarantee uniqueness of multiple tests in parallel in the same CouchDB instance.

use serde_json::json;
use couch_rs_test::{TestRepo, TestRepoConfig};
 
async fn new_database(
    config_uri: &str,
    config_username: &str,
    config_password: &str,
    config_dbname: &str,
) -> TestRepo {
 
    let repo: TestRepo = match TestRepo::new(
        TestRepoConfig::new(
            config_uri,
            config_username,
            config_password,
            config_dbname,
        )
    ).await {
        Ok(r) => r,
        // if db creation fails, test will fail, so just panic
        Err(e) => panic!("Failed to create test database: {}", e), 
    };
 
    // write test data into the newly created database
    let data = &mut vec!{
        json!{{"some": "data"}},
        json!{{"some": "other-data"}}
    };
 
    match repo.with_data(data).await {
        Ok(cnt) => log::info!("Added {} entries to test database {}", cnt, repo.db.name()),
        Err(e) => panic!("Failed to set up database: {}", e),
    };
    repo
}

Structs

A wrapper for a struct that encapsulates functionality of an application’s data layer.
Configuration for TestRepo.