Add some cache tests

This commit is contained in:
Mateusz Słodkowicz 2024-03-18 14:54:39 +01:00
parent 011f3ab497
commit bb91b603e3
Signed by: materus
GPG Key ID: 28D140BCA60B4FD1
2 changed files with 72 additions and 0 deletions

View File

@ -11,6 +11,8 @@ mod cache;
mod config; mod config;
mod exchange; mod exchange;
mod requests; mod requests;
#[cfg(test)]
mod tests;
#[derive(Parser)] #[derive(Parser)]
#[command(about, long_about = None, arg_required_else_help = true)] #[command(about, long_about = None, arg_required_else_help = true)]

70
src/tests.rs Normal file
View File

@ -0,0 +1,70 @@
use crate::{cache::get_api_key, *};
use std::sync::Once;
static INIT: Once = Once::new();
fn setup_test() {
INIT.call_once(|| {
let mut path: std::path::PathBuf = std::path::PathBuf::new();
path.push(std::env::temp_dir());
path.push("testCurrencyCache.db");
std::env::set_var(config::CACHE_LOCATION_ENV_NAME, &path);
if path.exists() {
std::fs::remove_file(path).expect("Something went wrong when removing test cache");
}
cache::create_cache().expect("Something went wrong when creating test cache");
cache::set_api_key("testKey".to_string())
.expect("Something went wrong when setting api key");
cache::add_code(["PLN".to_string(), "Polish zloty".to_string()])
.expect("Something went wrong when adding code");
let mut rates: std::collections::HashMap<String, serde_json::Value> =
std::collections::HashMap::new();
rates.insert("USD".to_string(), serde_json::json!(0.2546));
rates.insert("EUR".to_string(), serde_json::json!(0.2325));
cache::add_rates(99710201602, &"PLN".to_string(), &rates).expect("Error seting rates");
});
}
#[test]
fn test_cache_get_api_key() {
setup_test();
assert_eq!(
get_api_key().expect("Something went wrong when getting api key"),
"testKey"
);
}
#[test]
fn test_cache_check_code() {
setup_test();
assert!(cache::check_code(&"PLN".to_string()).expect("Something went wrong when getting code"));
}
#[test]
fn test_cache_get_rates() {
setup_test();
assert_eq!(
cache::get_rate(&"PLN".to_string(), &"USD".to_string()).expect("Error getting rates"),
"0.2546"
);
assert_eq!(
cache::get_rate(&"PLN".to_string(), &"EUR".to_string()).expect("Error getting rates"),
"0.2325"
);
}
#[test]
fn test_cache_check_exchange() {
setup_test();
assert!(
cache::check_exchange(&"PLN".to_string(), &"USD".to_string()).expect("Error while checking exchange")
);
assert!(
cache::check_exchange(&"PLN".to_string(), &"EUR".to_string()).expect("Error while checking exchange")
);
}