I'm writing my first Diesel application, and I really want to find a better way to run tests. I'm currently doing this so it can use a different database:
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url_env_var_name = if cfg!(test) {
"DATABASE_URL"
} else {
"TEST_DATABASE_URL"
};
let database_url = env::var(database_url_env_var_name)
.expect(&format!("{} must be set in order to connect to the database", database_url_env_var_name));
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
but I suspect there may be a better way. If so, I'd love to hear about it.
In addition, I want to enforce some postconditions on my tests (somewhat like meta testing, testing my test suite). Specifically, I want to ensure that there are no entries in any table in the database once the test suite is finished running.
How can I do this? Is it possible?