Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
SimranMakhija7 committed May 2, 2024
1 parent b15eaa4 commit ad2d0e8
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions src/handlers/namespace_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}

0 comments on commit ad2d0e8

Please sign in to comment.