use docbox_database::{models::document_box::DocumentBox, utils::DatabaseErrorExt};
use crate::common::database::test_tenant_db;
mod common;
#[tokio::test]
async fn test_create_document_box() {
let (db, _db_container) = test_tenant_db().await;
let document_box = DocumentBox::create(&db, "test".to_string()).await.unwrap();
assert_eq!(document_box.scope.as_str(), "test");
}
#[tokio::test]
async fn test_create_document_box_duplicate_scope_failure() {
let (db, _db_container) = test_tenant_db().await;
let document_box = DocumentBox::create(&db, "test".to_string()).await.unwrap();
assert_eq!(document_box.scope.as_str(), "test");
let error = DocumentBox::create(&db, "test".to_string())
.await
.unwrap_err();
assert!(error.is_duplicate_record());
}
#[tokio::test]
async fn test_delete_document_box_known() {
let (db, _db_container) = test_tenant_db().await;
let document_box = DocumentBox::create(&db, "test".to_string()).await.unwrap();
let result = document_box.delete(&db).await.unwrap();
assert_eq!(result.rows_affected(), 1);
}
#[tokio::test]
async fn test_delete_document_box_deleted() {
let (db, _db_container) = test_tenant_db().await;
let document_box = DocumentBox::create(&db, "test".to_string()).await.unwrap();
let result = document_box.delete(&db).await.unwrap();
assert_eq!(result.rows_affected(), 1);
let result = document_box.delete(&db).await.unwrap();
assert_eq!(result.rows_affected(), 0);
}
#[tokio::test]
async fn test_delete_document_box_exact() {
let (db, _db_container) = test_tenant_db().await;
DocumentBox::create(&db, "test1".to_string()).await.unwrap();
DocumentBox::create(&db, "test2".to_string()).await.unwrap();
let document_box = DocumentBox::create(&db, "test".to_string()).await.unwrap();
let result = document_box.delete(&db).await.unwrap();
assert_eq!(result.rows_affected(), 1);
let result = document_box.delete(&db).await.unwrap();
assert_eq!(result.rows_affected(), 0);
}
#[tokio::test]
async fn test_document_box_find_by_scope() {
let (db, _db_container) = test_tenant_db().await;
let document_box = DocumentBox::create(&db, "test".to_string()).await.unwrap();
let result = DocumentBox::find_by_scope(&db, "test")
.await
.unwrap()
.expect("should find document box");
assert_eq!(result.scope, document_box.scope);
}
#[tokio::test]
async fn test_document_box_find_by_scope_unknown() {
let (db, _db_container) = test_tenant_db().await;
DocumentBox::create(&db, "test1".to_string()).await.unwrap();
DocumentBox::create(&db, "test2".to_string()).await.unwrap();
let result = DocumentBox::find_by_scope(&db, "test").await.unwrap();
assert!(result.is_none());
}
#[tokio::test]
async fn test_document_box_total() {
let (db, _db_container) = test_tenant_db().await;
let total = DocumentBox::total(&db).await.unwrap();
assert_eq!(total, 0);
let document_box = DocumentBox::create(&db, "test1".to_string()).await.unwrap();
let total = DocumentBox::total(&db).await.unwrap();
assert_eq!(total, 1);
DocumentBox::create(&db, "test2".to_string()).await.unwrap();
DocumentBox::create(&db, "test3".to_string()).await.unwrap();
let total = DocumentBox::total(&db).await.unwrap();
assert_eq!(total, 3);
_ = document_box.delete(&db).await.unwrap();
let total = DocumentBox::total(&db).await.unwrap();
assert_eq!(total, 2);
}
#[tokio::test]
async fn test_document_box_query() {
let (db, _db_container) = test_tenant_db().await;
let results = DocumentBox::query(&db, 0, 5).await.unwrap();
assert!(results.is_empty());
DocumentBox::create(&db, "test1".to_string()).await.unwrap();
DocumentBox::create(&db, "test2".to_string()).await.unwrap();
DocumentBox::create(&db, "test3".to_string()).await.unwrap();
let results = DocumentBox::query(&db, 0, 5).await.unwrap();
assert_eq!(results.len(), 3);
assert_eq!(results[0].scope, "test3");
assert_eq!(results[1].scope, "test2");
assert_eq!(results[2].scope, "test1");
let results = DocumentBox::query(&db, 0, 2).await.unwrap();
assert_eq!(results.len(), 2);
assert_eq!(results[0].scope, "test3");
assert_eq!(results[1].scope, "test2");
let results = DocumentBox::query(&db, 2, 2).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].scope, "test1");
let results = DocumentBox::query(&db, 3, 2).await.unwrap();
assert!(results.is_empty());
}
#[tokio::test]
async fn test_document_box_search_query() {
let (db, _db_container) = test_tenant_db().await;
let results = DocumentBox::search_query(&db, "test", 0, 5).await.unwrap();
assert!(results.is_empty());
DocumentBox::create(&db, "test1".to_string()).await.unwrap();
DocumentBox::create(&db, "test2".to_string()).await.unwrap();
DocumentBox::create(&db, "test3".to_string()).await.unwrap();
let results = DocumentBox::search_query(&db, "test3", 0, 5).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].scope, "test3");
let results = DocumentBox::search_query(&db, "test%", 0, 5).await.unwrap();
assert_eq!(results.len(), 3);
assert_eq!(results[0].scope, "test3");
assert_eq!(results[1].scope, "test2");
assert_eq!(results[2].scope, "test1");
let results = DocumentBox::search_query(&db, "test%", 1, 5).await.unwrap();
assert_eq!(results.len(), 2);
assert_eq!(results[0].scope, "test2");
assert_eq!(results[1].scope, "test1");
let results = DocumentBox::search_query(&db, "test%", 0, 2).await.unwrap();
assert_eq!(results.len(), 2);
assert_eq!(results[0].scope, "test3");
assert_eq!(results[1].scope, "test2");
let results = DocumentBox::search_query(&db, "test%", 2, 2).await.unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].scope, "test1");
let results = DocumentBox::search_query(&db, "1test:%", 0, 5)
.await
.unwrap();
assert!(results.is_empty());
}
#[tokio::test]
async fn test_document_box_search_total() {
let (db, _db_container) = test_tenant_db().await;
let results = DocumentBox::search_total(&db, "test").await.unwrap();
assert_eq!(results, 0);
DocumentBox::create(&db, "test1".to_string()).await.unwrap();
DocumentBox::create(&db, "test2".to_string()).await.unwrap();
DocumentBox::create(&db, "test3".to_string()).await.unwrap();
let results = DocumentBox::search_total(&db, "test3").await.unwrap();
assert_eq!(results, 1);
let results = DocumentBox::search_total(&db, "test%").await.unwrap();
assert_eq!(results, 3);
let results = DocumentBox::search_total(&db, "1test:%").await.unwrap();
assert_eq!(results, 0);
}