mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 09:31:19 +02:00
feature(test): add tests
This commit is contained in:
69
src/state.rs
69
src/state.rs
@@ -1,5 +1,5 @@
|
||||
// auto-imports
|
||||
use crate::config::Config;
|
||||
use crate::config::ScootalooConfig;
|
||||
|
||||
// std
|
||||
use std::{
|
||||
@@ -10,6 +10,9 @@ use std::{
|
||||
// log
|
||||
use log::debug;
|
||||
|
||||
// rusqlite
|
||||
use rusqlite::{Connection, OpenFlags, params};
|
||||
|
||||
/// Reads last tweet id from a file
|
||||
pub fn read_state(s: &str) -> Option<u64> {
|
||||
let state = read_to_string(s);
|
||||
@@ -31,8 +34,68 @@ pub fn write_state(f: &str, s: u64) -> Result<(), std::io::Error> {
|
||||
* Main functions
|
||||
*********/
|
||||
/// Initiates the DB from path
|
||||
pub fn init_db(config: &Config) -> Result<(), Box<dyn Error>> {
|
||||
println!("config.scootaloo.db_path: {}", config.scootaloo.db_path);
|
||||
pub fn init_db(config: &ScootalooConfig) -> Result<(), Box<dyn Error>> {
|
||||
let conn = Connection::open(&config.db_path)?;
|
||||
|
||||
conn.execute(
|
||||
"CREATE TABLE IF NOT EXISTS tweet_to_toot (
|
||||
tweet_id INTEGER PRIMARY KEY,
|
||||
toot_id TEXT UNIQUE
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use std::{
|
||||
fs::remove_file,
|
||||
path::Path,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_init_db() {
|
||||
let scootaloo_config = ScootalooConfig {
|
||||
db_path: String::from("/tmp/test_init_db.sqlite"),
|
||||
cache_path: String::from("/tmp/scootaloo"),
|
||||
};
|
||||
|
||||
init_db(&scootaloo_config).unwrap();
|
||||
|
||||
// check that file exist
|
||||
assert!(Path::new(&scootaloo_config.db_path).exists());
|
||||
|
||||
// open said file
|
||||
let conn = Connection::open_with_flags(&scootaloo_config.db_path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
|
||||
|
||||
conn.execute(
|
||||
"SELECT * from tweet_to_toot;",
|
||||
[],
|
||||
).unwrap();
|
||||
|
||||
conn.close().unwrap();
|
||||
remove_file(&scootaloo_config.db_path).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_read_state() {
|
||||
let scootaloo_config = ScootalooConfig {
|
||||
db_path: String::from("/tmp/test_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 (?1, ?2)",
|
||||
params![123456789 as u64, String::from("987654321")],
|
||||
).unwrap();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user