deleted test working, crashing other test, needs fix

This commit is contained in:
filipriec
2025-02-25 12:01:01 +01:00
parent f81aabff78
commit 2abf0ab772
3 changed files with 119 additions and 1 deletions

View File

@@ -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
)

View File

@@ -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);
}

View File

@@ -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;