partially-fixed the tests and benchmarks.

This commit is contained in:
Jordan Petridis
2017-11-20 01:08:34 +02:00
parent 5ad52fa4b2
commit e07683c046
5 changed files with 24 additions and 47 deletions
+5 -11
View File
@@ -74,27 +74,21 @@ lazy_static! {
#[cfg(test)]
struct TempDB(tempdir::TempDir, PathBuf, utils::Pool);
#[cfg(test)]
extern crate rand;
#[cfg(test)]
extern crate tempdir;
#[cfg(test)]
use rand::Rng;
#[cfg(test)]
/// Create and return a Temporary DB.
/// Will be destroed once the returned variable(s) is dropped.
fn get_temp_db() -> TempDB {
let mut rng = rand::thread_rng();
let tmp_dir = tempdir::TempDir::new("hammond_unit_test").unwrap();
let db_path = tmp_dir
.path()
.join("test.db");
let db_path = tmp_dir.path().join("test.db");
let pool = utils::init_pool(db_path.to_str().unwrap());
let db = pool.clone().get().unwrap();
utils::run_migration_on(&*db).unwrap();
{
let db = pool.clone().get().unwrap();
utils::run_migration_on(&*db).unwrap();
}
TempDB(tmp_dir, db_path, pool)
}
+10 -13
View File
@@ -1,5 +1,4 @@
use rayon::prelude::*;
use diesel::prelude::*;
use chrono::prelude::*;
use r2d2;
@@ -12,24 +11,28 @@ use models::Episode;
use std::path::Path;
use std::fs;
use std::sync::Arc;
use std::time::Duration;
use DB_PATH;
use POOL;
embed_migrations!("migrations/");
pub type Pool = r2d2::Pool<ConnectionManager<SqliteConnection>>;
pub type Pool = Arc<r2d2::Pool<ConnectionManager<SqliteConnection>>>;
pub fn init() -> Result<()> {
let conn = establish_connection();
run_migration_on(&conn)
let con = POOL.clone().get().unwrap();
run_migration_on(&*con)
}
pub fn init_pool(db_path: &str) -> Pool {
let config = r2d2::Config::default();
let config = r2d2::Config::builder()
.connection_timeout(Duration::from_secs(60))
.build();
let manager = ConnectionManager::<SqliteConnection>::new(db_path);
let pool = r2d2::Pool::new(config, manager).expect("Failed to create pool.");
info!("Database pool initialized.");
pool
Arc::new(pool)
}
pub fn run_migration_on(connection: &SqliteConnection) -> Result<()> {
@@ -39,12 +42,6 @@ pub fn run_migration_on(connection: &SqliteConnection) -> Result<()> {
Ok(())
}
pub fn establish_connection() -> SqliteConnection {
let database_url = DB_PATH.to_str().unwrap();
SqliteConnection::establish(database_url)
.expect(&format!("Error connecting to {}", database_url))
}
// TODO: Write unit test.
fn download_checker() -> Result<()> {
let episodes = dbqueries::get_downloaded_episodes()?;