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

Commit

Permalink
add some tests to namespace handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SimranMakhija7 committed Apr 12, 2024
1 parent 954b5d2 commit 763e1f8
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dto/namespace_data.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct NamespaceData {
pub name: String,
pub properties: Value,
Expand Down
235 changes: 235 additions & 0 deletions src/handlers/namespace_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ use axum::{
use serde_json::Value;
use std::sync::Arc;

/*
TODO:
if a namespace or table already exists, you might want to return a StatusCode::CONFLICT
instead of StatusCode::INTERNAL_SERVER_ERROR. Similarly, if a namespace or table is not found,
you might want to return a StatusCode::NOT_FOUND.
*/
pub async fn list_namespaces(
State(repo): State<Arc<NamespaceRepository>>,
) -> Result<Json<Vec<String>>, (StatusCode, String)> {
Expand Down Expand Up @@ -74,3 +80,232 @@ pub async fn set_namespace_properties(
.map(|_| StatusCode::OK)
.map_err(|e| (StatusCode::INTERNAL_SERVER_ERROR, format!("Error: {}", e)))
}

// todo: check commented tests
#[cfg(test)]
mod tests {
use super::*;
use crate::database::database::Database;
use axum::http::StatusCode;
use serde_json::json;
use std::sync::{Arc, Mutex};
use tempfile::tempdir;

#[tokio::test]
async fn test_list_namespaces() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let data1 = list_namespaces(State(repo)).await.unwrap();
let data2: Json<Vec<String>> = Json(vec![]);
assert!(*data1 == *data2);
}

#[tokio::test]
async fn test_create_namespace() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "namespace".to_string(),
properties: json!({}),
});
assert_eq!(
create_namespace(State(repo), new_namespace.clone())
.await
.unwrap()
.name,
new_namespace.name
);
}

#[tokio::test]
async fn test_load_namespace_metadata() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "namespace".to_string(),
properties: json!({}),
});
let _ = create_namespace(State(repo.clone()), new_namespace.clone())
.await
.unwrap();

assert_eq!(
load_namespace_metadata(State(repo), Path("namespace".to_string()))
.await
.unwrap()
.name,
new_namespace.name
);
}

#[tokio::test]
async fn test_namespace_exists() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "namespace".to_string(),
properties: json!({}),
});
let _ = create_namespace(State(repo.clone()), new_namespace)
.await
.unwrap();
assert_eq!(
namespace_exists(State(repo), Path("namespace".to_string()))
.await
.unwrap(),
StatusCode::FOUND
);
}

#[tokio::test]
async fn test_drop_namespace() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "namespace".to_string(),
properties: json!({}),
});
let _ = create_namespace(State(repo.clone()), new_namespace)
.await
.unwrap();
assert_eq!(
drop_namespace(State(repo), Path("namespace".to_string()))
.await
.unwrap(),
StatusCode::NO_CONTENT
);
}

#[tokio::test]
async fn test_set_namespace_properties() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "namespace".to_string(),
properties: json!({}),
});
let _ = create_namespace(State(repo.clone()), new_namespace)
.await
.unwrap();
assert_eq!(
set_namespace_properties(
State(repo),
Path("namespace".to_string()),
Json(json!({"property": "value"}))
)
.await
.unwrap(),
StatusCode::OK
);
}

// Negative cases
#[tokio::test]
async fn test_load_namespace_metadata_not_found() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
assert_eq!(
load_namespace_metadata(State(repo), Path("nonexistent".to_string()))
.await
.unwrap_err()
.0,
StatusCode::NOT_FOUND
);
}

#[tokio::test]
async fn test_namespace_exists_not_found() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
assert_eq!(
namespace_exists(State(repo), Path("nonexistent".to_string()))
.await
.unwrap(),
StatusCode::NOT_FOUND
);
}

/*
#[tokio::test]
async fn test_drop_namespace_not_found() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
assert_eq!(
drop_namespace(State(repo), Path("nonexistent".to_string()))
.await
.unwrap_err()
.0,
StatusCode::INTERNAL_SERVER_ERROR
);
}
*/

#[tokio::test]
async fn test_set_namespace_properties_not_found() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
assert_eq!(
set_namespace_properties(
State(repo),
Path("nonexistent".to_string()),
Json(json!({"property": "value"}))
)
.await
.unwrap_err()
.0,
StatusCode::INTERNAL_SERVER_ERROR
);
}

/*
// Corner cases
#[tokio::test]
async fn test_create_namespace_empty_name() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "".to_string(),
properties: json!({}),
});
assert_eq!(
create_namespace(State(repo), new_namespace)
.await
.unwrap_err()
.0,
StatusCode::INTERNAL_SERVER_ERROR
);
}
#[tokio::test]
async fn test_create_namespace_already_exists() {
let repo = Arc::new(NamespaceRepository::new(Arc::new(Mutex::new(
Database::open(tempdir().unwrap().path()).unwrap(),
))));
let new_namespace = Json(NamespaceData {
name: "namespace".to_string(),
properties: json!({}),
});
let _ = create_namespace(State(repo.clone()), new_namespace.clone())
.await
.unwrap();
assert_eq!(
create_namespace(State(repo), new_namespace)
.await
.unwrap_err()
.0,
StatusCode::INTERNAL_SERVER_ERROR
);
}
*/
}

0 comments on commit 763e1f8

Please sign in to comment.