diff --git a/crates/api-grub/src/config.rs b/crates/api-grub/src/config.rs index cfe96b7..9a30354 100644 --- a/crates/api-grub/src/config.rs +++ b/crates/api-grub/src/config.rs @@ -77,4 +77,54 @@ async fn save_new_config(config: &String) -> Result<()> { async fn init_unix_listener() -> Result { let _ = fs::remove_file(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::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()) + } } \ No newline at end of file