Compare commits

...

4 Commits

Author SHA1 Message Date
prplV de727de200 pb update 2025-01-17 13:38:47 +03:00
prplV 16cb6a5de3 net unitests 2025-01-17 13:37:41 +03:00
prplV ced107c5b3 config unitests 2025-01-17 12:46:53 +03:00
prplV 8acf1230d2 logger unit test 2025-01-17 12:46:43 +03:00
4 changed files with 126 additions and 4 deletions

View File

@ -9,7 +9,7 @@
| Crate (submodule) | Progress | | Crate (submodule) | Progress |
|---|---| |---|---|
|`api-grub` | ✅✅✅✅🔲🔲🔲🔲🔲🔲 | |`api-grub` | ✅✅✅✅✅✅✅🔲🔲🔲 |
|`config-delivery` | 🔲🔲🔲🔲🔲🔲🔲🔲🔲🔲 | |`config-delivery` | 🔲🔲🔲🔲🔲🔲🔲🔲🔲🔲 |
|`integrs-structs` | ✅✅✅✅✅✅🔲🔲🔲🔲 | |`integrs-structs` | ✅✅✅✅✅✅🔲🔲🔲🔲 |
|`preproc` | 🔲🔲🔲🔲🔲🔲🔲🔲🔲🔲 | |`preproc` | 🔲🔲🔲🔲🔲🔲🔲🔲🔲🔲 |

View File

@ -77,4 +77,54 @@ async fn save_new_config(config: &String) -> Result<()> {
async fn init_unix_listener() -> Result<UnixListener> { async fn init_unix_listener() -> Result<UnixListener> {
let _ = fs::remove_file(SOCKET_PATH); let _ = fs::remove_file(SOCKET_PATH);
Ok(UnixListener::bind(SOCKET_PATH)?) Ok(UnixListener::bind(SOCKET_PATH)?)
}
#[cfg(test)]
mod config_unittests {
use super::*;
use tokio::test;
#[test]
async fn check_init_unix_listener() {
let res = init_unix_listener().await;
if res.is_ok() {
assert!(fs::remove_file(SOCKET_PATH).is_ok())
} else {
assert!(false)
}
}
#[test]
async fn check_save_new_config() {
use std::fs;
use integr_structs::api::ApiConfig;
use serde_json::to_string;
let test_config_path = "test_config_api.json";
// config gen
let config = to_string::<ApiConfig>(&ApiConfig::default());
assert!(config.is_ok());
let config = config.unwrap();
// config file gen and write
assert!(fs::File::create(test_config_path).is_ok());
assert!(fs::write(test_config_path, config).is_ok());
// config file reading and checking content
let file = fs::read_to_string(test_config_path);
assert!(file.is_ok());
let file = file.unwrap();
assert_ne!(file.len(), 0);
// deleting test config file
assert!(fs::remove_file(test_config_path).is_ok())
}
#[test]
async fn check_pull_local_config() {
use std::path::Path;
let local_config = Path::new(CONFIG_PATH);
assert_eq!(local_config.is_file() && local_config.exists(), pull_local_config().await.is_ok())
}
} }

View File

@ -23,4 +23,28 @@ pub async fn setup_logger() -> Result<()> {
info!("Logger configured"); info!("Logger configured");
Ok(()) Ok(())
}
#[cfg(test)]
mod logger_unittests {
use tokio::test;
use super::*;
#[test]
async fn check_logger_builder() {
Builder::new()
.format(move |buf, record| {
writeln!(
buf,
"|{}| {} [{}] - {}",
"api-grubber",
Local::now().format("%d-%m-%Y %H:%M:%S"),
record.level(),
record.args(),
)
})
.filter(None, LevelFilter::Info)
.target(env_logger::Target::Stdout)
.init();
}
} }

View File

@ -94,8 +94,56 @@ pub async fn init_api_grub_mechanism(config: ApiConfig, rx: &mut Receiver<ApiCon
// Ok(()) // Ok(())
} }
#[cfg(test)]
mod net_unittests {
use super::*;
use tokio::test;
use integr_structs::api::{ApiConfig, ApiEndpoint};
#[test]
async fn check_str_to_rest_method() {
assert_eq!(RestMethod::from_str("get").await, Method::GET);
assert_eq!(RestMethod::from_str("post").await, Method::POST);
assert_eq!(RestMethod::from_str("patch").await, Method::PATCH);
assert_eq!(RestMethod::from_str("put").await, Method::PUT);
assert_eq!(RestMethod::from_str("delete").await, Method::DELETE);
assert_eq!(RestMethod::from_str("invalid_method").await, Method::GET);
}
#[test]
async fn check_api_poll_change_config() {
let mut conf1 = ApiConfig::default();
let conf2 = ApiConfig { endpoints : vec![], delay : 10, };
let mut poll = ApiPoll::new(&mut conf1).await;
poll.change_config(conf2).await;
assert_eq!(poll.config.delay, 10)
}
#[test]
async fn check_api_poll_is_default() {
let mut conf1 = ApiConfig::default();
let poll = ApiPoll::new(&mut conf1).await;
assert!(poll.is_default().await)
}
// one-time exec func to send request, deserialize it and return object #[test]
#[allow(dead_code)] async fn check_api_grubbing_mechanism_on_public_one() {
async fn send_api_request() -> Result<()> {Ok(())} use log::{set_max_level, LevelFilter};
set_max_level(LevelFilter::Off);
let mut conf1 = ApiConfig {
endpoints : vec![
ApiEndpoint {
url : String::from("https://dummy-json.mock.beeceptor.com/countries"),
method: String::from("get"),
}],
delay : 10,
};
let conf2 = ApiConfig::default();
let mut poll = ApiPoll::new(&mut conf1).await;
assert!(poll.process_polling().await.is_ok());
poll.change_config(conf2).await;
assert!(poll.process_polling().await.is_err());
}
}