not working test yet

This commit is contained in:
filipriec
2025-02-25 09:49:12 +01:00
parent 04c7efe296
commit d296da0b31

View File

@@ -1,16 +1,23 @@
// tests/adresar/get_adresar_count_test.rs // tests/adresar/get_adresar_count_test.rs
use rstest::{fixture, rstest}; use rstest::{fixture, rstest};
use server::adresar::handlers::get_adresar_count; use server::adresar::handlers::get_adresar_count;
use common::proto::multieko2::common::{CountResponse, Empty}; use common::proto::multieko2::common::Empty;
use crate::common::{setup_test_db, clear_adresar_table}; use crate::common::setup_test_db;
use sqlx::PgPool; use sqlx::PgPool;
use tonic; use tonic;
#[fixture] #[fixture]
async fn pool() -> PgPool { async fn pool() -> PgPool {
let pool = setup_test_db().await; let pool = setup_test_db().await;
clear_adresar_table(&pool).await; // Clear the table before returning the pool
// 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 pool
} }
@@ -21,84 +28,107 @@ async fn closed_pool(#[future] pool: PgPool) -> PgPool {
pool pool
} }
#[fixture] #[rstest]
async fn mixed_records(#[future] pool: PgPool) -> PgPool { #[tokio::test]
async fn test_count_increments_after_adding_record(#[future] pool: PgPool) {
let pool = pool.await; let pool = pool.await;
clear_adresar_table(&pool).await; // Clear the table before inserting test data
// Insert test data // 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!( sqlx::query!(
r#" "INSERT INTO adresar (firma, deleted) VALUES ($1, FALSE)",
INSERT INTO adresar (firma, deleted) unique_name
VALUES
('Company A', false),
('Company B', false),
('Deleted Company', true),
('Company C', false),
('Another Deleted', true)
"#
) )
.execute(&pool) .execute(&pool)
.await .await
.unwrap(); .unwrap();
pool // Verify count increased by 1
let updated_response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(updated_response.count, initial_count + 1);
} }
#[rstest] #[rstest]
#[tokio::test] #[tokio::test]
async fn test_count_empty_database(#[future] pool: PgPool) { async fn test_deleted_records_not_counted(#[future] pool: PgPool) {
let pool = pool.await; let pool = pool.await;
let response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(response.count, 0);
}
#[rstest] // Get initial count
#[tokio::test] let initial_count = sqlx::query_scalar!("SELECT COUNT(*) FROM adresar WHERE deleted = FALSE")
async fn test_count_mixed_records(#[future] mixed_records: PgPool) { .fetch_one(&pool)
let pool = mixed_records.await; .await
let response = get_adresar_count(&pool, Empty {}).await.unwrap(); .unwrap()
assert_eq!(response.count, 3); // Only non-deleted records are counted .unwrap_or(0);
}
#[rstest] // Generate unique test identifier
#[tokio::test] let test_id = std::time::SystemTime::now()
async fn test_count_after_deletion(#[future] mixed_records: PgPool) { .duration_since(std::time::UNIX_EPOCH)
let pool = mixed_records.await; .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);
// Delete one active record
sqlx::query!( sqlx::query!(
"UPDATE adresar SET deleted = true WHERE firma = 'Company B'" "INSERT INTO adresar (id, firma, deleted) VALUES ($1, $2, FALSE)",
max_id + 1,
format!("Active 1 {}", test_id)
) )
.execute(&pool) .execute(&pool)
.await .await
.unwrap(); .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!( sqlx::query!(
r#" "INSERT INTO adresar (id, firma, deleted) VALUES ($1, $2, TRUE)",
INSERT INTO adresar (firma, deleted) max_id + 2,
VALUES format!("Deleted {}", test_id)
('Temp Company 1', true),
('Temp Company 2', true)
"#
) )
.execute(&pool) .execute(&pool)
.await .await
.unwrap(); .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(); let response = get_adresar_count(&pool, Empty {}).await.unwrap();
assert_eq!(response.count, 0); // No non-deleted records 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] #[rstest]
@@ -109,26 +139,3 @@ async fn test_database_error(#[future] closed_pool: PgPool) {
assert!(result.is_err()); assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::Internal); 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
}