mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
refactor: replace scootaloo_config with &str in init_db()
This commit is contained in:
12
README.md
12
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, "");
|
||||
```
|
||||
|
@@ -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;
|
||||
},
|
||||
_ => (),
|
||||
|
95
src/state.rs
95
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<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 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");
|
||||
|
Reference in New Issue
Block a user