From ad2d0e80776e707a90c2f7de415abe6eaf30b062 Mon Sep 17 00:00:00 2001 From: SIMRAN MAKHIJA Date: Thu, 2 May 2024 02:09:56 -0400 Subject: [PATCH] add test --- src/handlers/namespace_handler.rs | 89 +++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/src/handlers/namespace_handler.rs b/src/handlers/namespace_handler.rs index aca213f..24fd90f 100644 --- a/src/handlers/namespace_handler.rs +++ b/src/handlers/namespace_handler.rs @@ -132,3 +132,92 @@ pub async fn set_namespace_properties( .map(|_| StatusCode::OK) .map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {}", e))) } + +#[cfg(test)] +mod tests { + use super::*; + use crate::database::database::Database; + use crate::dto::set_namespace_properties_req::SetNamespacePropertiesRequest; + use axum::http::StatusCode; + use std::sync::{Arc, Mutex}; + use tempfile::tempdir; + #[tokio::test] + async fn test_namespace_endpoints() { + let dir = tempdir().unwrap(); + let db = Database::open(dir.path()).unwrap(); + let db = Arc::new(Mutex::new(db)); + let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new( + Database::open(tempdir().unwrap().path()).unwrap(), + )))); + // Test create_namespace + let new_namespace = Json(NamespaceData { + name: NamespaceIdent(vec!["test".to_string()]), + properties: json!({"property1": "value1"}), + }); + assert_eq!( + create_namespace(State(repo.clone()), new_namespace.clone()) + .await + .unwrap().name, + new_namespace.name + ); + + // Test namespace_exists + assert_eq!( + namespace_exists(State(repo.clone()), Path("test".to_string())) + .await + .unwrap(), + StatusCode::NO_CONTENT + ); + + // Test load_namespace_metadata + assert_eq!( + load_namespace_metadata(State(repo.clone()), Path("test".to_string())) + .await + .unwrap().name, + new_namespace.name + ); + + // Test set_namespace_properties + let set_namespace_properties_request = Json(SetNamespacePropertiesRequest { + removals: vec!["property1".to_string()], + updates: serde_json::from_value(json!({"property2": "value2"})).unwrap(), + }); + assert_eq!( + set_namespace_properties( + State(repo.clone()), + Path("test".to_string()), + set_namespace_properties_request + ) + .await + .unwrap(), + StatusCode::OK + ); + + // Test load_namespace_metadata after set_namespace_properties + assert_eq!( + load_namespace_metadata(State(repo.clone()), Path("test".to_string())) + .await + .unwrap().name, + Json(NamespaceData { + name: NamespaceIdent(vec!["test".to_string()]), + properties: json!({"property2": "value2"}), + }).name + ); + + // Test drop_namespace + assert_eq!( + drop_namespace(State(repo.clone()), Path("test".to_string())) + .await + .unwrap(), + StatusCode::NO_CONTENT + ); + + // Test namespace_exists after drop_namespace + assert_eq!( + namespace_exists(State(repo.clone()), Path("test".to_string())) + .await + .unwrap(), + StatusCode::NOT_FOUND + ); + } +}