142 lines
3.8 KiB
Rust
142 lines
3.8 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;
|
|
|
|
#[fixture]
|
|
async fn pool() -> PgPool {
|
|
let pool = setup_test_db().await;
|
|
|
|
// Ensure sequence is set correctly to avoid primary key conflicts
|
|
sqlx::query_scalar!(
|
|
"SELECT setval('adresar_id_seq', COALESCE((SELECT MAX(id) FROM adresar), 0) + 1, false)"
|
|
)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
pool
|
|
}
|
|
|
|
#[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;
|
|
|
|
// Get initial count of active records
|
|
let initial_count = sqlx::query_scalar!("SELECT COUNT(*) FROM adresar WHERE deleted = FALSE")
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap()
|
|
.unwrap_or(0);
|
|
|
|
// Verify initial count from handler
|
|
let initial_response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(initial_response.count, initial_count);
|
|
|
|
// Generate unique identifier for test data
|
|
let test_id = std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_millis();
|
|
|
|
// Insert new active record with unique name
|
|
let unique_name = format!("Test Company {}", test_id);
|
|
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_deleted_records_not_counted(#[future] pool: PgPool) {
|
|
let pool = pool.await;
|
|
|
|
// Get initial count
|
|
let initial_count = sqlx::query_scalar!("SELECT COUNT(*) FROM adresar WHERE deleted = FALSE")
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap()
|
|
.unwrap_or(0);
|
|
|
|
// Generate unique test identifier
|
|
let test_id = std::time::SystemTime::now()
|
|
.duration_since(std::time::UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_millis();
|
|
|
|
// Insert test records (2 active, 1 deleted)
|
|
let max_id = sqlx::query_scalar!("SELECT COALESCE(MAX(id), 0) FROM adresar")
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap()
|
|
.unwrap_or(0);
|
|
|
|
sqlx::query!(
|
|
"INSERT INTO adresar (id, firma, deleted) VALUES ($1, $2, FALSE)",
|
|
max_id + 1,
|
|
format!("Active 1 {}", test_id)
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
sqlx::query!(
|
|
"INSERT INTO adresar (id, firma, deleted) VALUES ($1, $2, TRUE)",
|
|
max_id + 2,
|
|
format!("Deleted {}", test_id)
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
sqlx::query!(
|
|
"INSERT INTO adresar (id, firma, deleted) VALUES ($1, $2, FALSE)",
|
|
max_id + 3,
|
|
format!("Active 2 {}", test_id)
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
// Verify count only includes active records
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, initial_count + 2);
|
|
|
|
// Reset sequence to maintain test stability
|
|
sqlx::query_scalar!(
|
|
"SELECT setval('adresar_id_seq', $1, true)",
|
|
max_id + 4
|
|
)
|
|
.fetch_one(&pool)
|
|
.await
|
|
.unwrap();
|
|
}
|
|
|
|
#[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);
|
|
}
|