not working test yet
This commit is contained in:
@@ -1,16 +1,23 @@
|
||||
// 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 common::proto::multieko2::common::Empty;
|
||||
use crate::common::setup_test_db;
|
||||
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
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
@@ -21,84 +28,107 @@ async fn closed_pool(#[future] pool: PgPool) -> PgPool {
|
||||
pool
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
async fn mixed_records(#[future] pool: PgPool) -> PgPool {
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_count_increments_after_adding_record(#[future] pool: PgPool) {
|
||||
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!(
|
||||
r#"
|
||||
INSERT INTO adresar (firma, deleted)
|
||||
VALUES
|
||||
('Company A', false),
|
||||
('Company B', false),
|
||||
('Deleted Company', true),
|
||||
('Company C', false),
|
||||
('Another Deleted', true)
|
||||
"#
|
||||
"INSERT INTO adresar (firma, deleted) VALUES ($1, FALSE)",
|
||||
unique_name
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.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]
|
||||
#[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 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); // Only non-deleted records are counted
|
||||
}
|
||||
// Get initial count
|
||||
let initial_count = sqlx::query_scalar!("SELECT COUNT(*) FROM adresar WHERE deleted = FALSE")
|
||||
.fetch_one(&pool)
|
||||
.await
|
||||
.unwrap()
|
||||
.unwrap_or(0);
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_count_after_deletion(#[future] mixed_records: PgPool) {
|
||||
let pool = mixed_records.await;
|
||||
// Generate unique test identifier
|
||||
let test_id = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.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!(
|
||||
"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)
|
||||
.await
|
||||
.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!(
|
||||
r#"
|
||||
INSERT INTO adresar (firma, deleted)
|
||||
VALUES
|
||||
('Temp Company 1', true),
|
||||
('Temp Company 2', true)
|
||||
"#
|
||||
"INSERT INTO adresar (id, firma, deleted) VALUES ($1, $2, TRUE)",
|
||||
max_id + 2,
|
||||
format!("Deleted {}", test_id)
|
||||
)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.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();
|
||||
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]
|
||||
@@ -109,26 +139,3 @@ async fn test_database_error(#[future] closed_pool: PgPool) {
|
||||
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); // One new non-deleted record
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user