mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 17:34:37 +02:00
feature: make thread in Twitter thread in Mastodon
This commit is contained in:
174
src/state.rs
174
src/state.rs
@@ -1,13 +1,7 @@
|
||||
// auto-imports
|
||||
use crate::config::ScootalooConfig;
|
||||
|
||||
// std
|
||||
use std::error::Error;
|
||||
|
||||
// log
|
||||
use log::debug;
|
||||
|
||||
// rusqlite
|
||||
use rusqlite::{Connection, params, OptionalExtension};
|
||||
|
||||
/// Struct for each query line
|
||||
@@ -50,9 +44,6 @@ pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Erro
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*********
|
||||
* Main functions
|
||||
*********/
|
||||
/// Initiates the DB from path
|
||||
pub fn init_db(config: &ScootalooConfig) -> Result<(), Box<dyn Error>> {
|
||||
debug!("Initializing DB for Scootaloo");
|
||||
@@ -78,7 +69,7 @@ mod tests {
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_db() {
|
||||
fn test_init_db() {
|
||||
let scootaloo_config = ScootalooConfig {
|
||||
db_path: String::from("/tmp/test_init_db.sqlite"),
|
||||
cache_path: String::from("/tmp/scootaloo"),
|
||||
@@ -96,57 +87,136 @@ mod tests {
|
||||
[],
|
||||
).unwrap();
|
||||
|
||||
// write a state to DB
|
||||
let t = TweetToToot {
|
||||
remove_file(scootaloo_config.db_path).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"),
|
||||
};
|
||||
|
||||
init_db(&scootaloo_config).unwrap();
|
||||
|
||||
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
let t_in = TweetToToot {
|
||||
tweet_id: 123456789,
|
||||
toot_id: String::from("987654321"),
|
||||
};
|
||||
write_state(&conn, t).unwrap();
|
||||
|
||||
let mut stmt = conn.prepare("SELECT * FROM tweet_to_toot limit 1;").unwrap();
|
||||
let mut rows = stmt.query([]).unwrap();
|
||||
write_state(&conn, t_in).unwrap();
|
||||
|
||||
while let Some(row) = rows.next().unwrap() {
|
||||
assert_eq!(123456789 as u64, row.get::<_, u64>(0).unwrap());
|
||||
assert_eq!("987654321", row.get::<_, String>(1).unwrap());
|
||||
}
|
||||
let mut stmt = conn.prepare("SELECT * FROM tweet_to_toot;").unwrap();
|
||||
|
||||
// write several other states
|
||||
let (t1, t2) = (
|
||||
TweetToToot {
|
||||
tweet_id: 11111111,
|
||||
toot_id: String::from("tamerelol"),
|
||||
},
|
||||
TweetToToot {
|
||||
tweet_id: 1123456789,
|
||||
toot_id: String::from("tonperemdr"),
|
||||
});
|
||||
let t_out = stmt.query_row([], |row| {
|
||||
Ok(TweetToToot {
|
||||
tweet_id: row.get(0).unwrap(),
|
||||
toot_id: row.get(1).unwrap(),
|
||||
})
|
||||
}).unwrap();
|
||||
|
||||
write_state(&conn, t1).unwrap();
|
||||
write_state(&conn, t2).unwrap();
|
||||
|
||||
match read_state(&conn, None).unwrap() {
|
||||
Some(i) => {
|
||||
assert_eq!(1123456789, i.tweet_id);
|
||||
assert_eq!("tonperemdr", &i.toot_id);
|
||||
},
|
||||
None => panic!("This should not happen!"),
|
||||
}
|
||||
|
||||
match read_state(&conn, Some(11111111)).unwrap() {
|
||||
Some(i) => {
|
||||
assert_eq!(11111111, i.tweet_id);
|
||||
assert_eq!("tamerelol", &i.toot_id);
|
||||
},
|
||||
None => panic!("This should not happen!"),
|
||||
}
|
||||
|
||||
match read_state(&conn, Some(0000000)).unwrap() {
|
||||
Some(_) => panic!("This should not happen"),
|
||||
_ => (),
|
||||
}
|
||||
assert_eq!(t_out.tweet_id, 123456789);
|
||||
assert_eq!(t_out.toot_id, String::from("987654321"));
|
||||
|
||||
remove_file(&scootaloo_config.db_path).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"),
|
||||
};
|
||||
|
||||
init_db(&scootaloo_config).unwrap();
|
||||
|
||||
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
|
||||
VALUES
|
||||
(101, 'A'),
|
||||
(102, 'B');",
|
||||
[],
|
||||
).unwrap();
|
||||
|
||||
let t_out = read_state(&conn, None).unwrap().unwrap();
|
||||
|
||||
remove_file(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
assert_eq!(t_out.tweet_id, 102);
|
||||
assert_eq!(t_out.toot_id, "B");
|
||||
}
|
||||
|
||||
#[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"),
|
||||
};
|
||||
|
||||
init_db(&scootaloo_config).unwrap();
|
||||
|
||||
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
let t_out = read_state(&conn, None).unwrap();
|
||||
|
||||
remove_file(&scootaloo_config.db_path).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"),
|
||||
};
|
||||
|
||||
init_db(&scootaloo_config).unwrap();
|
||||
|
||||
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
|
||||
VALUES
|
||||
(100, 'A');",
|
||||
[],
|
||||
).unwrap();
|
||||
|
||||
let t_out = read_state(&conn, Some(101)).unwrap();
|
||||
|
||||
remove_file(&scootaloo_config.db_path).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"),
|
||||
};
|
||||
|
||||
init_db(&scootaloo_config).unwrap();
|
||||
|
||||
let conn = Connection::open(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"INSERT INTO tweet_to_toot (tweet_id, toot_id)
|
||||
VALUES
|
||||
(100, 'A');",
|
||||
[],
|
||||
).unwrap();
|
||||
|
||||
let t_out = read_state(&conn, Some(100)).unwrap().unwrap();
|
||||
|
||||
remove_file(&scootaloo_config.db_path).unwrap();
|
||||
|
||||
assert_eq!(t_out.tweet_id, 100);
|
||||
assert_eq!(t_out.toot_id, "A");
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user