Skip to content

Commit f1585cd

Browse files
committed
added initial unit test implementation
1 parent 819ba2b commit f1585cd

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

src/lib/db.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,17 @@ macro_rules! try_return { ($e:expr) => (match $e { Ok(x) => x, Err(e) => {warn!(
2121
macro_rules! try_option { ($e:expr) => (match $e { Some(x) => x, None => return None }) }
2222

2323
/// Connect to DBMS, retry on failure.
24-
pub fn db_connect(opts: MyOpts, sleep_time: u32) -> MyPool {
24+
/// `is_test` is only `true` for tests
25+
pub fn db_connect(opts: MyOpts, sleep_time: u32, is_test: bool) -> MyPool {
2526
loop {
2627
match pool::MyPool::new(opts.clone()) {
2728
Ok(conn) => {return conn;},
2829
Err(err) => error!("Unable to establish a connection: {}",err),
2930
};
3031
sleep_ms(sleep_time);
32+
if is_test {
33+
unreachable!("couldn't connect to db")
34+
}
3135
}
3236
}
3337

src/main.rs

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ enum Thing { String(String), Bool(bool), None }
6060
fn main() {
6161
logger::initialize();
6262

63-
let pool = db::db_connect(db::mysql_options(), SLEEP_MS);
63+
let pool = db::db_connect(db::mysql_options(), SLEEP_MS, false);
6464
debug!("cleaning db");
6565
db::clear_query_states(&pool);
6666

@@ -374,3 +374,99 @@ fn handle_playlist<'a>(downl_db: & mut DownloadDB<'a>, converter: &Converter, fi
374374

375375
Ok(Thing::Bool(warnings))
376376
}
377+
378+
#[cfg(test)]
379+
mod test {
380+
extern crate mysql;
381+
use mysql::error::MyResult;
382+
use mysql::error::MyError;
383+
use mysql::conn::MyOpts;
384+
use mysql::conn::pool::MyPool;
385+
386+
use super::handle_download;
387+
use lib;
388+
use lib::l_expect;
389+
use lib::config;
390+
use std::env;
391+
use lib::db::db_connect;
392+
393+
lazy_static! {
394+
pub static ref CONFIG: config::Config = {
395+
config::init_config()
396+
};
397+
}
398+
399+
macro_rules! println_stderr(
400+
($($arg:tt)*) => (
401+
match writeln!(&mut ::std::io::stderr(), $($arg)* ) {
402+
Ok(_) => {},
403+
Err(x) => panic!("Unable to write to stderr: {}", x),
404+
}
405+
)
406+
);
407+
408+
#[test]
409+
fn handle_db() {
410+
assert_eq!(env::var("db_test"),Ok("true".to_string()));
411+
lib::logger::initialize();
412+
let pool = connect_db();
413+
setup_db(&pool);
414+
415+
fn connect_db() -> MyPool {
416+
let myopts = MyOpts {
417+
tcp_addr: Some(env::var("db_ip").unwrap()),
418+
tcp_port: env::var("db_port").unwrap().parse::<u16>().unwrap(),
419+
user: Some(env::var("db_user").unwrap()),
420+
pass: Some(env::var("db_password").unwrap()),
421+
db_name: Some(env::var("db_db").unwrap()),
422+
..Default::default() // set others to default
423+
};
424+
println!("{:?}",myopts);
425+
lib::db::db_connect(myopts, super::SLEEP_MS, true)
426+
}
427+
fn setup_db(pool: &MyPool) -> Result<(),MyError> {
428+
let setup = include_str!("../install.sql").to_string();
429+
let lines = setup.lines();
430+
let mut table_sql = String::new();
431+
let mut in_table = false;
432+
for line in lines {
433+
if in_table {
434+
table_sql = table_sql +"\n"+ line;
435+
if line.contains(";") {
436+
in_table = false;
437+
info!("Table:\n{}",table_sql);
438+
l_expect(pool.prep_exec(&table_sql,()),"unable to create db!");
439+
table_sql.clear();
440+
}
441+
}
442+
if line.starts_with("CREATE TABLE") {
443+
table_sql = table_sql +"\n"+ line;
444+
in_table = true;
445+
}
446+
}
447+
448+
// create fake entries to monitor progress regressions leading to wrong updates
449+
let mut query_stmt = l_expect(pool.prepare("insert into `queries` (qid, url, type, quality, uid, created) VALUES (?,?,?,?,0,NOW())"),"prepare error");
450+
let mut querydetails_stmt = l_expect(pool.prepare("insert into `querydetails` (qid,code,progress,status) VALUES (?,?,?,?)"),"prepare error");
451+
let index_start = 10;
452+
let mut index = index_start;
453+
for i in 1..index_start {
454+
l_expect(query_stmt.execute((i,"",0,0)),"stmt exec");
455+
l_expect(querydetails_stmt.execute((i,-5,-5,"fake")), "stmt exec");
456+
}
457+
458+
l_expect(query_stmt.execute((index,"https://www.youtube.com/watch?v=aqz-KE-bpKQ",0,133)),"stmt exec");
459+
index += 1;
460+
l_expect(query_stmt.execute((index,"https://www.youtube.com/watch?v=aqz-KE-bpKQ",0,-1)),"stmt exec");
461+
index += 1;
462+
l_expect(query_stmt.execute((index,"https://www.youtube.com/watch?v=aqz-KE-bpKQ",0,-2)),"stmt exec");
463+
index += 1;
464+
const code_waiting: i16 = -1;
465+
for i in index_start..index {
466+
l_expect(querydetails_stmt.execute((i,code_waiting,0,"waiting")),"stmt exec");
467+
}
468+
469+
Ok(())
470+
}
471+
}
472+
}

0 commit comments

Comments
 (0)