From 26491f146f6d366c49f548560ae2106cb25e3dc6 Mon Sep 17 00:00:00 2001 From: VC Date: Sun, 24 Apr 2022 10:02:45 +0200 Subject: [PATCH] refactor: replace scootaloo_config with &str in init_db() --- README.md | 12 +++++-- src/main.rs | 2 +- src/state.rs | 95 ++++++++++++++++++++++++++-------------------------- 3 files changed, 58 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 0a17168..2a92071 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ Then run the command with the `init` subcommand to initiate the DB: scootaloo init ``` +This subcommand is completely idempotent. + Then run the command with the `register` subcommand: ```sh scootaloo register --host https://m.nintendojo.fr @@ -76,9 +78,13 @@ SUBCOMMANDS: Scootaloo does not respect the spam limits imposed by Mastodon: it will make a 429 error if too much Tweets are converted to Toots in a short amount of time (and it will not recover from it). By default, it gets the last 200 tweets from the user timeline (which is a lot!). It is recommended to put a Tweet number into the DB file before copying an old account. -You can can insert it like this: +You can insert that Tweet number, by connecting to the DB you created: ```sh sqlite3 /var/lib/scootaloo/scootaloo.sqlite -INSERT INTO tweet_to_toot VALUES (1383782580412030982, ""); -.quit +``` + +And inserting the data: + +```sql +INSERT INTO tweet_to_toot VALUES (1383782580412030982, ""); ``` diff --git a/src/main.rs b/src/main.rs index 60d22ad..d31f57c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; }, _ => (), diff --git a/src/state.rs b/src/state.rs index 93c103d..8b9b569 100644 --- a/src/state.rs +++ b/src/state.rs @@ -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 Result<(), Box> { +pub fn init_db(d: &str) -> Result<(), Box> { 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 let’s 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");