trying to do get request unit test to work with vipe out database
This commit is contained in:
134
server/tests/adresar/get_adresar_count_test.rs
Normal file
134
server/tests/adresar/get_adresar_count_test.rs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
@@ -3,3 +3,4 @@
|
|||||||
pub mod post_adresar_test;
|
pub mod post_adresar_test;
|
||||||
pub mod put_adresar_test;
|
pub mod put_adresar_test;
|
||||||
pub mod get_adresar_test;
|
pub mod get_adresar_test;
|
||||||
|
pub mod get_adresar_count_test;
|
||||||
|
|||||||
@@ -28,3 +28,24 @@ pub async fn setup_test_db() -> PgPool {
|
|||||||
|
|
||||||
pool
|
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");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user