Files
komp_ac/server/tests/adresar/get_adresar_count_test.rs
2025-02-25 10:37:09 +01:00

237 lines
7.1 KiB
Rust

// tests/adresar/get_adresar_count_test.rs
use rstest::{fixture, rstest};
use server::adresar::handlers::get_adresar_count;
use common::proto::multieko2::common::Empty;
use crate::common::setup_test_db;
use sqlx::PgPool;
use tonic;
use std::sync::Arc;
use tokio::sync::Mutex;
// Use a global mutex to synchronize test execution
// This prevents tests from interfering with each other
lazy_static::lazy_static! {
static ref TEST_MUTEX: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
}
#[fixture]
async fn pool() -> PgPool {
// Just connect to the test database without truncating anything
setup_test_db().await
}
#[fixture]
async fn closed_pool(#[future] pool: PgPool) -> PgPool {
let pool = pool.await;
pool.close().await;
pool
}
#[rstest]
#[tokio::test]
async fn test_count_increments_after_adding_record(#[future] pool: PgPool) {
let pool = pool.await;
// Lock to prevent concurrent test execution
let _guard = TEST_MUTEX.lock().await;
// Get initial count of active records
let initial_response = get_adresar_count(&pool, Empty {}).await.unwrap();
let initial_count = initial_response.count;
// Insert new active record with unique name
let unique_name = format!("Test Company {}", chrono::Utc::now().timestamp_millis());
sqlx::query!(
"INSERT INTO adresar (firma, deleted) VALUES ($1, FALSE)",
unique_name
)
.execute(&pool)
.await
.unwrap();
// Verify count increased by 1
let updated_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(updated_response.count, initial_count + 1);
}
#[rstest]
#[tokio::test]
async fn test_add_multiple_records_and_check_count(#[future] pool: PgPool) {
let pool = pool.await;
// Lock to prevent concurrent test execution
let _guard = TEST_MUTEX.lock().await;
// Get initial count
let initial_response = get_adresar_count(&pool, Empty {}).await.unwrap();
let initial_count = initial_response.count;
// Add 5 active records
let timestamp = chrono::Utc::now().timestamp_millis();
for i in 0..5 {
let unique_name = format!("Batch Company {} #{}", timestamp, i);
sqlx::query!(
"INSERT INTO adresar (firma, deleted) VALUES ($1, FALSE)",
unique_name
)
.execute(&pool)
.await
.unwrap();
}
// Verify count increased by 5
let updated_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(updated_response.count, initial_count + 5);
}
#[rstest]
#[tokio::test]
async fn test_deleted_records_not_counted(#[future] pool: PgPool) {
let pool = pool.await;
// Lock to prevent concurrent test execution
let _guard = TEST_MUTEX.lock().await;
// Get initial count
let initial_response = get_adresar_count(&pool, Empty {}).await.unwrap();
let initial_count = initial_response.count;
// Generate unique identifier for test
let timestamp = chrono::Utc::now().timestamp_millis();
// Insert 5 test records (2 active, 3 deleted)
for i in 0..5 {
let is_deleted = i >= 2; // First 2 active, next 3 deleted
let unique_name = format!("Deletion Test {} #{}", timestamp, i);
sqlx::query!(
"INSERT INTO adresar (firma, deleted) VALUES ($1, $2)",
unique_name,
is_deleted
)
.execute(&pool)
.await
.unwrap();
}
// Verify count only increased by 2 (the non-deleted records)
let updated_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(updated_response.count, initial_count + 2);
}
#[rstest]
#[tokio::test]
async fn test_mark_existing_record_as_deleted(#[future] pool: PgPool) {
let pool = pool.await;
// Lock to prevent concurrent test execution
let _guard = TEST_MUTEX.lock().await;
// Get initial count
let initial_response = get_adresar_count(&pool, Empty {}).await.unwrap();
let initial_count = initial_response.count;
// Insert one record
let unique_name = format!("Delete Me {}", chrono::Utc::now().timestamp_millis());
let record_id = sqlx::query_scalar!(
"INSERT INTO adresar (firma, deleted) VALUES ($1, FALSE) RETURNING id",
unique_name
)
.fetch_one(&pool)
.await
.unwrap();
// Verify count increased by 1
let after_insert_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(after_insert_response.count, initial_count + 1);
// Mark record as deleted
sqlx::query!(
"UPDATE adresar SET deleted = TRUE WHERE id = $1",
record_id
)
.execute(&pool)
.await
.unwrap();
// Verify count decreased back to initial
let after_delete_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(after_delete_response.count, initial_count);
}
#[rstest]
#[tokio::test]
async fn test_edge_case_empty_table(#[future] pool: PgPool) {
let pool = pool.await;
// Get current count (whatever it is)
let current_response = get_adresar_count(&pool, Empty {}).await.unwrap();
let current_count = current_response.count;
// We're not going to empty the table (could violate FK constraints)
// But we can at least verify that the count is non-negative
assert!(current_count >= 0);
}
#[rstest]
#[tokio::test]
async fn test_database_error(#[future] closed_pool: PgPool) {
let closed_pool = closed_pool.await;
let result = get_adresar_count(&closed_pool, Empty {}).await;
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal);
}
#[rstest]
#[tokio::test]
async fn test_toggle_deleted_status(#[future] pool: PgPool) {
let pool = pool.await;
// Lock to prevent concurrent test execution
let _guard = TEST_MUTEX.lock().await;
// Get initial count
let initial_response = get_adresar_count(&pool, Empty {}).await.unwrap();
let initial_count = initial_response.count;
// Create a new record that will be toggled
let unique_name = format!("Toggle Me {}", chrono::Utc::now().timestamp_millis());
let record_id = sqlx::query_scalar!(
"INSERT INTO adresar (firma, deleted) VALUES ($1, FALSE) RETURNING id",
unique_name
)
.fetch_one(&pool)
.await
.unwrap();
// Verify count increased by 1
let after_insert_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(after_insert_response.count, initial_count + 1);
// Mark as deleted
sqlx::query!(
"UPDATE adresar SET deleted = TRUE WHERE id = $1",
record_id
)
.execute(&pool)
.await
.unwrap();
// Verify count back to initial
let after_delete_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(after_delete_response.count, initial_count);
// Mark as NOT deleted
sqlx::query!(
"UPDATE adresar SET deleted = FALSE WHERE id = $1",
record_id
)
.execute(&pool)
.await
.unwrap();
// Verify count increased again
let after_undelete_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(after_undelete_response.count, initial_count + 1);
}