refactor: replace scootaloo_config with &str in init_db()

This commit is contained in:
VC
2022-04-24 10:02:45 +02:00
parent 13bb6d6f37
commit 26491f146f
3 changed files with 58 additions and 51 deletions

View File

@@ -54,7 +54,7 @@ fn main() {
},
("init", Some(sub_m)) => {
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH));
init_db(&config.scootaloo).unwrap();
init_db(&config.scootaloo.db_path).unwrap();
return;
},
_ => (),

View File

@@ -1,5 +1,3 @@
use crate::config::ScootalooConfig;
use std::error::Error;
use log::debug;
use rusqlite::{Connection, params, OptionalExtension};
@@ -45,9 +43,9 @@ pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Erro
}
/// Initiates the DB from path
pub fn init_db(config: &ScootalooConfig) -> Result<(), Box<dyn Error>> {
pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
debug!("Initializing DB for Scootaloo");
let conn = Connection::open(&config.db_path)?;
let conn = Connection::open(d)?;
conn.execute(
"CREATE TABLE IF NOT EXISTS tweet_to_toot (
@@ -70,36 +68,51 @@ mod tests {
#[test]
fn test_init_db() {
let scootaloo_config = ScootalooConfig {
db_path: String::from("/tmp/test_init_db.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
let d = "/tmp/test_init_db.sqlite";
init_db(&scootaloo_config).unwrap();
init_db(d).unwrap();
// check that file exist
assert!(Path::new(&scootaloo_config.db_path).exists());
assert!(Path::new(d).exists());
// open said file
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"SELECT * from tweet_to_toot;",
[],
).unwrap();
remove_file(scootaloo_config.db_path).unwrap();
remove_file(d).unwrap();
}
#[test]
fn test_init_init_db() {
// init_db fn should be idempotent so lets test that
let d = "/tmp/test_init_init_db.sqlite";
init_db(d).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot
VALUES
(100, 'A');",
[],
).unwrap();
init_db(d).unwrap();
remove_file(d).unwrap();
}
#[test]
fn test_write_state() {
let scootaloo_config = ScootalooConfig {
db_path: String::from("/tmp/test_write_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
let d = "/tmp/test_write_state.sqlite";
init_db(&scootaloo_config).unwrap();
init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
let conn = Connection::open(d).unwrap();
let t_in = TweetToToot {
tweet_id: 123456789,
@@ -120,19 +133,16 @@ mod tests {
assert_eq!(t_out.tweet_id, 123456789);
assert_eq!(t_out.toot_id, String::from("987654321"));
remove_file(&scootaloo_config.db_path).unwrap();
remove_file(d).unwrap();
}
#[test]
fn test_none_to_tweet_id_read_state() {
let scootaloo_config = ScootalooConfig {
db_path: String::from("/tmp/test_none_to_tweet_id_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
let d = "/tmp/test_none_to_tweet_id_read_state.sqlite";
init_db(&scootaloo_config).unwrap();
init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
@@ -144,7 +154,7 @@ mod tests {
let t_out = read_state(&conn, None).unwrap().unwrap();
remove_file(&scootaloo_config.db_path).unwrap();
remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 102);
assert_eq!(t_out.toot_id, "B");
@@ -152,32 +162,26 @@ mod tests {
#[test]
fn test_none_to_none_read_state() {
let scootaloo_config = ScootalooConfig {
db_path: String::from("/tmp/test_none_to_none_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
let d = "/tmp/test_none_to_none_read_state.sqlite";
init_db(&scootaloo_config).unwrap();
init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
let conn = Connection::open(d).unwrap();
let t_out = read_state(&conn, None).unwrap();
remove_file(&scootaloo_config.db_path).unwrap();
remove_file(d).unwrap();
assert!(t_out.is_none());
}
#[test]
fn test_tweet_id_to_none_read_state() {
let scootaloo_config = ScootalooConfig {
db_path: String::from("/tmp/test_tweet_id_to_none_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
let d = "/tmp/test_tweet_id_to_none_read_state.sqlite";
init_db(&scootaloo_config).unwrap();
init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
@@ -188,21 +192,18 @@ mod tests {
let t_out = read_state(&conn, Some(101)).unwrap();
remove_file(&scootaloo_config.db_path).unwrap();
remove_file(d).unwrap();
assert!(t_out.is_none());
}
#[test]
fn test_tweet_id_to_tweet_id_read_state() {
let scootaloo_config = ScootalooConfig {
db_path: String::from("/tmp/test_tweet_id_to_tweet_id_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
let d = "/tmp/test_tweet_id_to_tweet_id_read_state.sqlite";
init_db(&scootaloo_config).unwrap();
init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
@@ -213,7 +214,7 @@ mod tests {
let t_out = read_state(&conn, Some(100)).unwrap().unwrap();
remove_file(&scootaloo_config.db_path).unwrap();
remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 100);
assert_eq!(t_out.toot_id, "A");