Crates.io | citerne |
lib.rs | citerne |
version | 0.1.0 |
source | src |
created_at | 2022-09-22 22:34:04.678561 |
updated_at | 2022-09-22 22:34:04.678561 |
description | A wrapper around diesel and testcontainers providing a single proc macro to run tests on a containerized database. |
homepage | |
repository | https://github.com/oknozor/citerne |
max_upload_size | |
id | 672020 |
size | 12,452 |
Citerne is a wrapper around diesel and testcontainers providing a single proc macro to run tests on a containerized database.
# Cargo.toml
citerne = {version = "0.1", features = ["postgres"] }
Embedding diesel migrations:
You must provide a diesel migration directory (relative to your crate root directory). Citerne will set up a database container and execute all migration in the directory.
#[derive(Insertable)]
#[diesel(table_name = dummy)]
struct NewDummy { value: String }
#[database_container_test(migrations = ["./migrations"])]
fn simple_migrations(conn: &mut PgConnection) -> CiterneResult<()> {
let value = NewDummy { value: "one".to_string() };
let sql_result = diesel::insert_into(dummy::table)
.values(value)
.execute(conn)?;
assert_eq!(sql_result, 1);
Ok(())
}
Custom migration files:
You can directly embed a single sql migration file to populate your containerized database with fixture data.
Note that the migrations
execution is ordered, you always need to provide diesel migrations first.
#[database_container_test(migrations = ["./migrations", "./tests/dummy.sql"])]
fn migration_with_data_script(conn: &mut PgConnection) -> CiterneResult<()> {
use crate::schema::dummy::value;
use crate::test::dummy::dsl::dummy;
let dummies: Vec<String> = dummy.select(value).load::<String>(conn)?;
assert_eq!(dummies, vec!["yeah".to_string(), "yo".to_string()]);
Ok(())
}
Raw sql script:
Alternatively if you need your fixture data to appear in the test
you can use the sql
attribute. Note that you can combine both approach.
#[database_container_test(
migrations = ["./migrations"]
sql = r#"
INSERT INTO dummy (value) VALUES ('yeah');
INSERT INTO dummy (value) VALUES ('yo');
"#
)]
fn migration_with_raw_sql_script(conn: &mut PgConnection) -> CiterneResult<()> {
use crate::schema::dummy::value;
use crate::test::dummy::dsl::dummy;
let dummies: Vec<String> = dummy.select(value).load::<String>(conn)?;
assert_eq!(dummies, vec!["yeah".to_string(), "yo".to_string()]);
Ok(())
}
All the code in this repository is released under the MIT License, for more information take a look at the LICENSE file.