| 1 |
#![allow(missing_docs)] // test only module |
| 2 |
extern crate dotenvy; |
| 3 |
|
| 4 |
use crate::prelude::*; |
| 5 |
|
| 6 |
cfg_if! { |
| 7 |
if #[cfg(feature = "sqlite")] { |
| 8 |
pub type TestConnection = SqliteConnection; |
| 9 |
|
| 10 |
pub fn connection() -> TestConnection { |
| 11 |
SqliteConnection::establish(":memory:").unwrap() |
| 12 |
} |
| 13 |
|
| 14 |
pub fn database_url() -> String { |
| 15 |
String::from(":memory:") |
| 16 |
} |
| 17 |
} else if #[cfg(feature = "postgres")] { |
| 18 |
pub type TestConnection = PgConnection; |
| 19 |
|
| 20 |
pub fn connection() -> TestConnection { |
| 21 |
pg_connection() |
| 22 |
} |
| 23 |
|
| 24 |
pub fn database_url() -> String { |
| 25 |
pg_database_url() |
| 26 |
} |
| 27 |
} else if #[cfg(feature = "mysql")] { |
| 28 |
pub type TestConnection = MysqlConnection; |
| 29 |
|
| 30 |
pub fn connection() -> TestConnection { |
| 31 |
let mut conn = connection_no_transaction(); |
| 32 |
conn.begin_test_transaction().unwrap(); |
| 33 |
conn |
| 34 |
} |
| 35 |
|
| 36 |
pub fn connection_no_transaction() -> TestConnection { |
| 37 |
MysqlConnection::establish(&database_url()).unwrap() |
| 38 |
} |
| 39 |
|
| 40 |
pub fn database_url() -> String { |
| 41 |
dotenvy::var("MYSQL_UNIT_TEST_DATABASE_URL") |
| 42 |
.or_else(|_| dotenvy::var("DATABASE_URL")) |
| 43 |
.expect("DATABASE_URL must be set in order to run tests") |
| 44 |
} |
| 45 |
} else { |
| 46 |
compile_error!( |
| 47 |
"At least one backend must be used to test this crate.\n \ |
| 48 |
Pass argument `--features \"<backend>\"` with one or more of the following backends, \ |
| 49 |
'mysql', 'postgres', or 'sqlite'. \n\n \ |
| 50 |
ex. cargo test --features \"mysql postgres sqlite\"\n" |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
#[cfg(feature = "postgres")] |
| 56 |
pub fn pg_connection() -> PgConnection { |
| 57 |
let mut conn = pg_connection_no_transaction(); |
| 58 |
conn.begin_test_transaction().unwrap(); |
| 59 |
conn |
| 60 |
} |
| 61 |
|
| 62 |
#[cfg(feature = "postgres")] |
| 63 |
pub fn pg_connection_no_transaction() -> PgConnection { |
| 64 |
PgConnection::establish(&pg_database_url()).unwrap() |
| 65 |
} |
| 66 |
|
| 67 |
#[cfg(feature = "postgres")] |
| 68 |
pub fn pg_database_url() -> String { |
| 69 |
dotenvy::var("PG_DATABASE_URL") |
| 70 |
.or_else(|_| dotenvy::var("DATABASE_URL")) |
| 71 |
.expect("DATABASE_URL must be set in order to run tests") |
| 72 |
} |