mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
feature(test): add tests
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// auto-imports
|
||||
mod error;
|
||||
use error::ScootalooError;
|
||||
use crate::error::ScootalooError;
|
||||
|
||||
mod config;
|
||||
use config::Config;
|
||||
|
@@ -61,7 +61,7 @@ fn main() {
|
||||
},
|
||||
("init", Some(sub_m)) => {
|
||||
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH));
|
||||
init_db(&config).unwrap();
|
||||
init_db(&config.scootaloo).unwrap();
|
||||
return;
|
||||
},
|
||||
_ => (),
|
||||
|
@@ -1,6 +1,5 @@
|
||||
// auto imports
|
||||
use crate::config::MastodonConfig;
|
||||
use crate::twitter::decode_urls;
|
||||
|
||||
// std
|
||||
use std::{
|
||||
@@ -16,7 +15,7 @@ use htmlescape::decode_html;
|
||||
// egg-mode
|
||||
use egg_mode::{
|
||||
tweet::Tweet,
|
||||
entities::MentionEntity,
|
||||
entities::{UrlEntity, MentionEntity},
|
||||
};
|
||||
|
||||
// elefren
|
||||
@@ -39,6 +38,20 @@ fn twitter_mentions(ums: &Vec<MentionEntity>) -> HashMap<String, String> {
|
||||
decoded_mentions
|
||||
}
|
||||
|
||||
/// Decodes urls from UrlEntities
|
||||
fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
|
||||
let mut decoded_urls = HashMap::new();
|
||||
|
||||
for url in urls {
|
||||
if url.expanded_url.is_some() {
|
||||
// unwrap is safe here as we just verified that there is something inside expanded_url
|
||||
decoded_urls.insert(String::from(&url.url), String::from(url.expanded_url.as_deref().unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
decoded_urls
|
||||
}
|
||||
|
||||
/// Gets Mastodon Data
|
||||
pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon {
|
||||
let data = Data {
|
||||
@@ -104,3 +117,52 @@ pub fn register(host: &str) {
|
||||
println!("Please insert the following block at the end of your configuration file:\n[mastodon]\n{}", toml);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_twitter_mentions() {
|
||||
let mention_entity = MentionEntity {
|
||||
id: 12345,
|
||||
range: (1, 3),
|
||||
name: String::from("Ta Mere l0l"),
|
||||
screen_name: String::from("tamerelol"),
|
||||
};
|
||||
|
||||
let twitter_ums = vec![mention_entity];
|
||||
|
||||
let mut expected_mentions = HashMap::new();
|
||||
expected_mentions.insert(String::from("@tamerelol"), String::from("@tamerelol@twitter.com"));
|
||||
|
||||
let decoded_mentions = twitter_mentions(&twitter_ums);
|
||||
|
||||
assert_eq!(expected_mentions, decoded_mentions);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_decode_urls() {
|
||||
let url_entity1 = UrlEntity {
|
||||
display_url: String::from("tamerelol"),
|
||||
expanded_url: Some(String::from("https://www.nintendojo.fr/dojobar")),
|
||||
range: (1, 3),
|
||||
url: String::from("https://t.me/tamerelol"),
|
||||
};
|
||||
|
||||
let url_entity2 = UrlEntity {
|
||||
display_url: String::from("tamerelol"),
|
||||
expanded_url: None,
|
||||
range: (1, 3),
|
||||
url: String::from("https://t.me/tamerelol"),
|
||||
};
|
||||
|
||||
let twitter_urls = vec![url_entity1, url_entity2];
|
||||
|
||||
let mut expected_urls = HashMap::new();
|
||||
expected_urls.insert(String::from("https://t.me/tamerelol"), String::from("https://www.nintendojo.fr/dojobar"));
|
||||
|
||||
let decoded_urls = decode_urls(&twitter_urls);
|
||||
|
||||
assert_eq!(expected_urls, decoded_urls);
|
||||
}
|
||||
}
|
||||
|
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();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -4,16 +4,13 @@ use crate::config::TwitterConfig;
|
||||
use crate::util::cache_media;
|
||||
|
||||
// std
|
||||
use std::{
|
||||
error::Error,
|
||||
collections::HashMap,
|
||||
};
|
||||
use std::error::Error;
|
||||
|
||||
// egg-mode
|
||||
use egg_mode::{
|
||||
Token,
|
||||
KeyPair,
|
||||
entities::{UrlEntity, MediaEntity, MediaType},
|
||||
entities::{MediaEntity, MediaType},
|
||||
user::UserID,
|
||||
tweet::{
|
||||
Tweet,
|
||||
@@ -43,20 +40,6 @@ pub async fn get_user_timeline(config: &TwitterConfig, token: Token, lid: Option
|
||||
Ok(feed.to_vec())
|
||||
}
|
||||
|
||||
/// Decodes urls from UrlEntities
|
||||
pub fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
|
||||
let mut decoded_urls = HashMap::new();
|
||||
|
||||
for url in urls {
|
||||
if url.expanded_url.is_some() {
|
||||
// unwrap is safe here as we just verified that there is something inside expanded_url
|
||||
decoded_urls.insert(String::from(&url.url), String::from(url.expanded_url.as_deref().unwrap()));
|
||||
}
|
||||
}
|
||||
|
||||
decoded_urls
|
||||
}
|
||||
|
||||
/// Retrieves a single media from a tweet and store it in a temporary file
|
||||
pub async fn get_tweet_media(m: &MediaEntity, t: &str) -> Result<String, Box<dyn Error>> {
|
||||
match m.media_type {
|
||||
|
Reference in New Issue
Block a user