From 2abf0ab772c14f90aef2f89257dd37f8ff6f453f Mon Sep 17 00:00:00 2001 From: filipriec Date: Tue, 25 Feb 2025 12:01:01 +0100 Subject: [PATCH] deleted test working, crashing other test, needs fix --- server/src/adresar/handlers/delete_adresar.rs | 2 +- server/tests/adresar/delete_adresar_test.rs | 117 ++++++++++++++++++ server/tests/adresar/mod.rs | 1 + 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 server/tests/adresar/delete_adresar_test.rs diff --git a/server/src/adresar/handlers/delete_adresar.rs b/server/src/adresar/handlers/delete_adresar.rs index 7eeeaad..5b4894b 100644 --- a/server/src/adresar/handlers/delete_adresar.rs +++ b/server/src/adresar/handlers/delete_adresar.rs @@ -11,7 +11,7 @@ pub async fn delete_adresar( r#" UPDATE adresar SET deleted = true - WHERE id = $1 + WHERE id = $1 AND deleted = false "#, request.id ) diff --git a/server/tests/adresar/delete_adresar_test.rs b/server/tests/adresar/delete_adresar_test.rs new file mode 100644 index 0000000..0d0d2e4 --- /dev/null +++ b/server/tests/adresar/delete_adresar_test.rs @@ -0,0 +1,117 @@ +// tests/adresar/delete_adresar_test.rs +use rstest::{fixture, rstest}; +use server::adresar::handlers::delete_adresar; +use common::proto::multieko2::adresar::DeleteAdresarRequest; +use crate::common::setup_test_db; +use sqlx::PgPool; +use tonic; + +// Fixtures +#[fixture] +async fn pool() -> PgPool { + setup_test_db().await +} + +#[fixture] +async fn closed_pool(#[future] pool: PgPool) -> PgPool { + let pool = pool.await; + pool.close().await; + pool +} + +#[fixture] +async fn existing_record(#[future] pool: PgPool) -> (PgPool, i64) { + let pool = pool.await; + let record = sqlx::query!( + r#" + INSERT INTO adresar (firma, deleted) + VALUES ('Test Company', false) + RETURNING id + "# + ) + .fetch_one(&pool) + .await + .unwrap(); + + (pool, record.id) +} + +#[fixture] +async fn existing_deleted_record(#[future] pool: PgPool) -> (PgPool, i64) { + let pool = pool.await; + let record = sqlx::query!( + r#" + INSERT INTO adresar (firma, deleted) + VALUES ('Deleted Company', true) + RETURNING id + "# + ) + .fetch_one(&pool) + .await + .unwrap(); + + (pool, record.id) +} + +// Helper to check if the record is deleted +async fn assert_record_deleted(pool: &PgPool, id: i64) { + let db_record = sqlx::query!("SELECT deleted FROM adresar WHERE id = $1", id) + .fetch_one(pool) + .await + .unwrap(); + + assert!(db_record.deleted); +} + +// Tests +#[rstest] +#[tokio::test] +async fn test_delete_adresar_success( + #[future] existing_record: (PgPool, i64), +) { + let (pool, id) = existing_record.await; + let request = DeleteAdresarRequest { id }; + let response = delete_adresar(&pool, request).await.unwrap(); + + assert!(response.success); + assert_record_deleted(&pool, id).await; +} + +#[rstest] +#[tokio::test] +async fn test_delete_adresar_nonexistent_id( + #[future] pool: PgPool, +) { + let pool = pool.await; + let request = DeleteAdresarRequest { id: 9999 }; + let response = delete_adresar(&pool, request).await.unwrap(); + + // Deleting a non-existent record should return success: false + assert!(!response.success); +} + +#[rstest] +#[tokio::test] +async fn test_delete_adresar_already_deleted( + #[future] existing_deleted_record: (PgPool, i64), +) { + let (pool, id) = existing_deleted_record.await; + let request = DeleteAdresarRequest { id }; + let response = delete_adresar(&pool, request).await.unwrap(); + + // Deleting an already deleted record should return success: false + assert!(!response.success); +} + +#[rstest] +#[tokio::test] +async fn test_delete_adresar_database_error( + #[future] closed_pool: PgPool, +) { + let closed_pool = closed_pool.await; + let request = DeleteAdresarRequest { id: 1 }; + let result = delete_adresar(&closed_pool, request).await; + + assert!(result.is_err()); + assert_eq!(result.unwrap_err().code(), tonic::Code::Internal); +} diff --git a/server/tests/adresar/mod.rs b/server/tests/adresar/mod.rs index 072cf2a..4ee661b 100644 --- a/server/tests/adresar/mod.rs +++ b/server/tests/adresar/mod.rs @@ -5,3 +5,4 @@ pub mod put_adresar_test; pub mod get_adresar_test; pub mod get_adresar_count_test; pub mod get_adresar_by_position_test; +pub mod delete_adresar_test;