135 lines
3.5 KiB
Rust
135 lines
3.5 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::{CountResponse, Empty};
|
|
use crate::common::{setup_test_db, clear_adresar_table};
|
|
use sqlx::PgPool;
|
|
use tonic;
|
|
|
|
#[fixture]
|
|
async fn pool() -> PgPool {
|
|
let pool = setup_test_db().await;
|
|
clear_adresar_table(&pool).await; // Clear the table before returning the pool
|
|
pool
|
|
}
|
|
|
|
#[fixture]
|
|
async fn closed_pool(#[future] pool: PgPool) -> PgPool {
|
|
let pool = pool.await;
|
|
pool.close().await;
|
|
pool
|
|
}
|
|
|
|
#[fixture]
|
|
async fn mixed_records(#[future] pool: PgPool) -> PgPool {
|
|
let pool = pool.await;
|
|
clear_adresar_table(&pool).await; // Clear the table before inserting test data
|
|
|
|
// Insert test data
|
|
sqlx::query!(
|
|
r#"
|
|
INSERT INTO adresar (firma, deleted)
|
|
VALUES
|
|
('Company A', false),
|
|
('Company B', false),
|
|
('Deleted Company', true),
|
|
('Company C', false),
|
|
('Another Deleted', true)
|
|
"#
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
pool
|
|
}
|
|
|
|
#[rstest]
|
|
#[tokio::test]
|
|
async fn test_count_empty_database(#[future] pool: PgPool) {
|
|
let pool = pool.await;
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, 0);
|
|
}
|
|
|
|
#[rstest]
|
|
#[tokio::test]
|
|
async fn test_count_mixed_records(#[future] mixed_records: PgPool) {
|
|
let pool = mixed_records.await;
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, 3); // Only non-deleted records are counted
|
|
}
|
|
|
|
#[rstest]
|
|
#[tokio::test]
|
|
async fn test_count_after_deletion(#[future] mixed_records: PgPool) {
|
|
let pool = mixed_records.await;
|
|
|
|
// Delete one active record
|
|
sqlx::query!(
|
|
"UPDATE adresar SET deleted = true WHERE firma = 'Company B'"
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, 2); // One fewer non-deleted record
|
|
}
|
|
|
|
#[rstest]
|
|
#[tokio::test]
|
|
async fn test_count_all_deleted(#[future] pool: PgPool) {
|
|
let pool = pool.await;
|
|
clear_adresar_table(&pool).await; // Ensure the table is empty
|
|
|
|
// Insert only deleted records
|
|
sqlx::query!(
|
|
r#"
|
|
INSERT INTO adresar (firma, deleted)
|
|
VALUES
|
|
('Temp Company 1', true),
|
|
('Temp Company 2', true)
|
|
"#
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, 0); // No non-deleted records
|
|
}
|
|
|
|
#[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_count_after_insert(#[future] pool: PgPool) {
|
|
let pool = pool.await;
|
|
clear_adresar_table(&pool).await; // Ensure the table is empty
|
|
|
|
// Initial count
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, 0);
|
|
|
|
// Insert new record
|
|
sqlx::query!(
|
|
"INSERT INTO adresar (firma) VALUES ('New Company')"
|
|
)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
// Verify updated count
|
|
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
|
|
assert_eq!(response.count, 1); // One new non-deleted record
|
|
}
|