-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Duyet Le
committed
Jan 31, 2022
1 parent
b3ec226
commit afad35d
Showing
9 changed files
with
840 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use crate::diesel::Connection; | ||
use crate::diesel::RunQueryDsl; | ||
use diesel::pg::PgConnection; | ||
use diesel::r2d2::{ConnectionManager, Pool}; | ||
use diesel::sql_query; | ||
use diesel_migrations::embed_migrations; | ||
use std::env; | ||
|
||
use crate::DBPool; | ||
|
||
pub struct TestContext { | ||
conn: PgConnection, | ||
base_url: String, | ||
db_name: String, | ||
} | ||
|
||
embed_migrations!("migrations/"); | ||
|
||
impl TestContext { | ||
pub fn new(db_name: &str) -> Self { | ||
let base_url = | ||
env::var("TEST_DATABASE_URL").unwrap_or_else(|_| "postgres://localhost".to_string()); | ||
println!( | ||
"Creating test database {} in {}, please set TEST_DATABASE_URL to change this", | ||
db_name, base_url | ||
); | ||
|
||
let database_url = format!("{}/postgres", base_url); | ||
let conn = PgConnection::establish(&database_url).expect("Could not connect to database"); | ||
|
||
// Create database | ||
sql_query(format!("CREATE DATABASE {}", db_name).as_str()) | ||
.execute(&conn) | ||
.expect("Failed to create database"); | ||
|
||
// Migation | ||
let conn_migrations = PgConnection::establish(&format!("{}/{}", base_url, db_name)) | ||
.unwrap_or_else(|_| panic!("Could not connect to database {}", db_name)); | ||
embedded_migrations::run(&conn_migrations).expect("Failed to run migrations"); | ||
|
||
Self { | ||
conn, | ||
base_url, | ||
db_name: db_name.to_string(), | ||
} | ||
} | ||
|
||
pub fn get_conn(&self) -> PgConnection { | ||
let database_url = format!("{}/{}", self.base_url, self.db_name); | ||
|
||
PgConnection::establish(&database_url).expect("Could not connect to database") | ||
} | ||
|
||
pub fn get_pool(&self) -> DBPool { | ||
let database_url = format!("{}/{}", self.base_url, self.db_name); | ||
let manager = ConnectionManager::<PgConnection>::new(database_url); | ||
|
||
Pool::builder() | ||
.build(manager) | ||
.expect("Failed to create connection pool") | ||
} | ||
} | ||
|
||
impl Drop for TestContext { | ||
fn drop(&mut self) { | ||
println!("Dropping test database {}", self.db_name); | ||
|
||
// Postgres will refuse to delete a database | ||
// if there is any connected user | ||
sql_query(format!( | ||
"SELECT pg_terminate_backend(pid) | ||
FROM pg_stat_activity | ||
WHERE datname = '{}';", | ||
self.db_name | ||
)) | ||
.execute(&self.conn) | ||
.unwrap(); | ||
|
||
// Drop the database | ||
sql_query(format!("DROP DATABASE {}", self.db_name).as_str()) | ||
.execute(&self.conn) | ||
.unwrap_or_else(|_| panic!("Couldn't drop database {}", self.db_name)); | ||
} | ||
} | ||
|
||
pub use actix_web::body::{Body, ResponseBody}; | ||
|
||
pub trait BodyTest { | ||
fn as_str(&self) -> &str; | ||
} | ||
|
||
/// Get the body from ResponseBody::Body | ||
impl BodyTest for ResponseBody<Body> { | ||
fn as_str(&self) -> &str { | ||
match self { | ||
ResponseBody::Body(ref b) => match b { | ||
Body::Bytes(ref by) => std::str::from_utf8(by).unwrap(), | ||
_ => panic!(), | ||
}, | ||
ResponseBody::Other(ref b) => match b { | ||
Body::Bytes(ref by) => std::str::from_utf8(by).unwrap(), | ||
_ => panic!(), | ||
}, | ||
} | ||
} | ||
} |
Oops, something went wrong.