forked from diesel-rs/diesel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_helpers.rs
More file actions
58 lines (46 loc) · 1.82 KB
/
Copy pathtest_helpers.rs
File metadata and controls
58 lines (46 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use diesel::prelude::*;
cfg_if! {
if #[cfg(feature = "sqlite")] {
use diesel::sqlite::SqliteConnection;
pub type TestConnection = SqliteConnection;
pub fn connection() -> TestConnection {
SqliteConnection::establish(":memory:").unwrap()
}
} else if #[cfg(feature = "postgres")] {
extern crate dotenv;
use self::dotenv::dotenv;
use std::env;
use diesel::pg::PgConnection;
pub type TestConnection = PgConnection;
pub fn connection() -> TestConnection {
dotenv().ok();
let database_url = env::var("PG_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
let conn = PgConnection::establish(&database_url).unwrap();
conn.begin_test_transaction().unwrap();
conn
}
} else if #[cfg(feature = "mysql")] {
extern crate dotenv;
use self::dotenv::dotenv;
use std::env;
use diesel::mysql::MysqlConnection;
pub type TestConnection = MysqlConnection;
pub fn connection() -> TestConnection {
let conn = connection_no_transaction();
conn.begin_test_transaction().unwrap();
conn
}
pub fn connection_no_transaction() -> TestConnection {
dotenv().ok();
let database_url = env::var("MYSQL_UNIT_TEST_DATABASE_URL")
.or_else(|_| env::var("DATABASE_URL"))
.expect("DATABASE_URL must be set in order to run tests");
MysqlConnection::establish(&database_url).unwrap()
}
} else {
// FIXME: https://github.com/rust-lang/rfcs/pull/1695
// compile_error!("At least one backend must be enabled to run tests");
}
}