diff --git a/server/tests/adresar/get_adresar_count_test.rs b/server/tests/adresar/get_adresar_count_test.rs new file mode 100644 index 0000000..878c319 --- /dev/null +++ b/server/tests/adresar/get_adresar_count_test.rs @@ -0,0 +1,134 @@ +// 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); +} + +#[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); +} + +#[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); +} + +#[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); +} diff --git a/server/tests/adresar/mod.rs b/server/tests/adresar/mod.rs index 69c3f50..efa50cb 100644 --- a/server/tests/adresar/mod.rs +++ b/server/tests/adresar/mod.rs @@ -3,3 +3,4 @@ pub mod post_adresar_test; pub mod put_adresar_test; pub mod get_adresar_test; +pub mod get_adresar_count_test; diff --git a/server/tests/common/mod.rs b/server/tests/common/mod.rs index bf198d7..81dfbfc 100644 --- a/server/tests/common/mod.rs +++ b/server/tests/common/mod.rs @@ -28,3 +28,24 @@ pub async fn setup_test_db() -> PgPool { pool } + +// Add this to tests/common/mod.rs +pub async fn clear_all_tables(pool: &PgPool) { + // Clear tables in the correct order to respect foreign key constraints + clear_uctovnictvo_table(pool).await; + clear_adresar_table(pool).await; +} + +pub async fn clear_uctovnictvo_table(pool: &PgPool) { + sqlx::query!("DELETE FROM uctovnictvo") + .execute(pool) + .await + .expect("Failed to clear uctovnictvo table"); +} + +pub async fn clear_adresar_table(pool: &PgPool) { + sqlx::query!("DELETE FROM adresar") + .execute(pool) + .await + .expect("Failed to clear adresar table"); +}