test is finally passing properly well
This commit is contained in:
@@ -20,20 +20,14 @@ async fn closed_pool(#[future] pool: PgPool) -> PgPool {
|
|||||||
pool
|
pool
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
async fn setup_test_environment(pool: &PgPool) -> (String, String, i64) {
|
||||||
#[tokio::test]
|
|
||||||
async fn test_returns_correct_count(#[future] pool: PgPool) {
|
|
||||||
let pool = pool.await;
|
|
||||||
let mut tx = pool.begin().await.unwrap();
|
let mut tx = pool.begin().await.unwrap();
|
||||||
|
|
||||||
|
// Create unique profile and table names
|
||||||
|
let profile_name = format!("test_profile_{}", Utc::now().timestamp_nanos());
|
||||||
|
let table_name = format!("test_table_{}", Utc::now().timestamp_nanos());
|
||||||
|
|
||||||
// Clean up existing profiles
|
// Create profile
|
||||||
sqlx::query!("DELETE FROM profiles WHERE name LIKE 'test_profile%'")
|
|
||||||
.execute(&mut *tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Setup profile and table definition
|
|
||||||
let profile_name = format!("test_profile_{}", Utc::now().timestamp());
|
|
||||||
let profile_id = sqlx::query_scalar!(
|
let profile_id = sqlx::query_scalar!(
|
||||||
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
|
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
|
||||||
profile_name
|
profile_name
|
||||||
@@ -42,103 +36,146 @@ async fn test_returns_correct_count(#[future] pool: PgPool) {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
// Create table definition
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"INSERT INTO table_definitions (profile_id, table_name, columns, indexes)
|
||||||
INSERT INTO table_definitions (profile_id, table_name, columns)
|
VALUES ($1, $2, $3, $4)"#,
|
||||||
VALUES ($1, $2, $3)
|
|
||||||
"#,
|
|
||||||
profile_id,
|
profile_id,
|
||||||
"adresar",
|
table_name,
|
||||||
json!({}) // Provide a valid JSON object for columns
|
json!({}),
|
||||||
|
json!([])
|
||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Insert test data
|
// Create actual table
|
||||||
sqlx::query!("INSERT INTO adresar (firma, deleted) VALUES ('Test 1', FALSE)")
|
sqlx::query(&format!(
|
||||||
|
r#"CREATE TABLE "{}" (
|
||||||
|
id BIGSERIAL PRIMARY KEY,
|
||||||
|
deleted BOOLEAN NOT NULL DEFAULT false,
|
||||||
|
firma TEXT NOT NULL
|
||||||
|
)"#,
|
||||||
|
table_name
|
||||||
|
))
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
tx.commit().await.unwrap();
|
||||||
|
(profile_name, table_name, profile_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn cleanup_test_environment(pool: &PgPool, profile_id: i64, table_name: &str) {
|
||||||
|
let mut tx = pool.begin().await.unwrap();
|
||||||
|
|
||||||
|
// Cleanup order matters!
|
||||||
|
sqlx::query(&format!(r#"DROP TABLE IF EXISTS "{}" CASCADE"#, table_name))
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
sqlx::query!("INSERT INTO adresar (firma, deleted) VALUES ('Test 2', FALSE)")
|
|
||||||
|
sqlx::query!("DELETE FROM table_definitions WHERE profile_id = $1", profile_id)
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
sqlx::query!("DELETE FROM profiles WHERE id = $1", profile_id)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Commit to make data visible to handler
|
tx.commit().await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_returns_correct_count(#[future] pool: PgPool) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let (profile_name, table_name, profile_id) = setup_test_environment(&pool).await;
|
||||||
|
|
||||||
|
// Insert test data
|
||||||
|
let mut tx = pool.begin().await.unwrap();
|
||||||
|
sqlx::query(&format!(
|
||||||
|
r#"INSERT INTO "{}" (firma) VALUES ('Test 1')"#,
|
||||||
|
table_name
|
||||||
|
))
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
sqlx::query(&format!(
|
||||||
|
r#"INSERT INTO "{}" (firma) VALUES ('Test 2')"#,
|
||||||
|
table_name
|
||||||
|
))
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
tx.commit().await.unwrap();
|
tx.commit().await.unwrap();
|
||||||
|
|
||||||
// Call handler
|
// Test
|
||||||
let request = GetTableDataCountRequest {
|
let request = GetTableDataCountRequest {
|
||||||
profile_name,
|
profile_name: profile_name.clone(),
|
||||||
table_name: "adresar".to_string(),
|
table_name: table_name.clone(),
|
||||||
};
|
};
|
||||||
let response = get_table_data_count(&pool, request).await.unwrap();
|
let response = get_table_data_count(&pool, request).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(response.count, 2);
|
assert_eq!(response.count, 2);
|
||||||
|
|
||||||
// Cleanup
|
cleanup_test_environment(&pool, profile_id, &table_name).await;
|
||||||
let mut cleanup_tx = pool.begin().await.unwrap();
|
|
||||||
sqlx::query!("DELETE FROM adresar WHERE firma LIKE 'Test%'")
|
|
||||||
.execute(&mut *cleanup_tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
cleanup_tx.commit().await.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_excludes_deleted_records(#[future] pool: PgPool) {
|
async fn test_excludes_deleted_records(#[future] pool: PgPool) {
|
||||||
let pool = pool.await;
|
let pool = pool.await;
|
||||||
|
let (profile_name, table_name, profile_id) = setup_test_environment(&pool).await;
|
||||||
|
|
||||||
|
// Insert test data
|
||||||
let mut tx = pool.begin().await.unwrap();
|
let mut tx = pool.begin().await.unwrap();
|
||||||
|
sqlx::query(&format!(
|
||||||
// Clean up existing profiles
|
r#"INSERT INTO "{}" (firma, deleted) VALUES ('Active', false)"#,
|
||||||
sqlx::query!("DELETE FROM profiles WHERE name LIKE 'test_profile%'")
|
table_name
|
||||||
.execute(&mut *tx)
|
))
|
||||||
.await
|
.execute(&mut *tx)
|
||||||
.unwrap();
|
.await
|
||||||
|
.unwrap();
|
||||||
let profile_name = format!("test_profile_{}", Utc::now().timestamp());
|
|
||||||
let profile_id = sqlx::query_scalar!(
|
sqlx::query(&format!(
|
||||||
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
|
r#"INSERT INTO "{}" (firma, deleted) VALUES ('Deleted', true)"#,
|
||||||
profile_name
|
table_name
|
||||||
)
|
))
|
||||||
.fetch_one(&mut *tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
sqlx::query!(
|
|
||||||
r#"
|
|
||||||
INSERT INTO table_definitions (profile_id, table_name, columns)
|
|
||||||
VALUES ($1, $2, $3)
|
|
||||||
"#,
|
|
||||||
profile_id,
|
|
||||||
"adresar",
|
|
||||||
json!({}) // Provide a valid JSON object for columns
|
|
||||||
)
|
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
// Insert active and deleted records
|
|
||||||
sqlx::query!("INSERT INTO adresar (firma, deleted) VALUES ('Active', FALSE)")
|
|
||||||
.execute(&mut *tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
sqlx::query!("INSERT INTO adresar (firma, deleted) VALUES ('Deleted', TRUE)")
|
|
||||||
.execute(&mut *tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
tx.commit().await.unwrap();
|
tx.commit().await.unwrap();
|
||||||
|
|
||||||
|
// Test
|
||||||
let request = GetTableDataCountRequest {
|
let request = GetTableDataCountRequest {
|
||||||
profile_name,
|
profile_name: profile_name.clone(),
|
||||||
table_name: "adresar".to_string(),
|
table_name: table_name.clone(),
|
||||||
};
|
};
|
||||||
let response = get_table_data_count(&pool, request).await.unwrap();
|
let response = get_table_data_count(&pool, request).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(response.count, 1);
|
assert_eq!(response.count, 1);
|
||||||
|
|
||||||
|
cleanup_test_environment(&pool, profile_id, &table_name).await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[rstest]
|
||||||
|
#[tokio::test]
|
||||||
|
async fn test_table_not_in_profile(#[future] pool: PgPool) {
|
||||||
|
let pool = pool.await;
|
||||||
|
let (profile_name, _, profile_id) = setup_test_environment(&pool).await;
|
||||||
|
|
||||||
|
// Test with non-existent table
|
||||||
|
let request = GetTableDataCountRequest {
|
||||||
|
profile_name,
|
||||||
|
table_name: "non_existent_table".to_string(),
|
||||||
|
};
|
||||||
|
let result = get_table_data_count(&pool, request).await;
|
||||||
|
assert!(result.is_err());
|
||||||
|
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
|
||||||
|
|
||||||
|
cleanup_test_environment(&pool, profile_id, "dummy_table").await;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
@@ -156,40 +193,6 @@ async fn test_profile_not_found(#[future] pool: PgPool) {
|
|||||||
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
|
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rstest]
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_table_not_in_profile(#[future] pool: PgPool) {
|
|
||||||
let pool = pool.await;
|
|
||||||
let mut tx = pool.begin().await.unwrap();
|
|
||||||
|
|
||||||
// Clean up existing profiles
|
|
||||||
sqlx::query!("DELETE FROM profiles WHERE name LIKE 'test_profile%'")
|
|
||||||
.execute(&mut *tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
let profile_name = format!("test_profile_{}", Utc::now().timestamp());
|
|
||||||
let profile_id = sqlx::query_scalar!(
|
|
||||||
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
|
|
||||||
profile_name
|
|
||||||
)
|
|
||||||
.fetch_one(&mut *tx)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
// Do not link 'adresar' to profile
|
|
||||||
tx.commit().await.unwrap();
|
|
||||||
|
|
||||||
let request = GetTableDataCountRequest {
|
|
||||||
profile_name,
|
|
||||||
table_name: "adresar".to_string(),
|
|
||||||
};
|
|
||||||
let result = get_table_data_count(&pool, request).await;
|
|
||||||
|
|
||||||
assert!(result.is_err());
|
|
||||||
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rstest]
|
#[rstest]
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_database_error(#[future] closed_pool: PgPool) {
|
async fn test_database_error(#[future] closed_pool: PgPool) {
|
||||||
@@ -210,13 +213,17 @@ async fn test_empty_table_count(#[future] pool: PgPool) {
|
|||||||
let pool = pool.await;
|
let pool = pool.await;
|
||||||
let mut tx = pool.begin().await.unwrap();
|
let mut tx = pool.begin().await.unwrap();
|
||||||
|
|
||||||
// Clean up existing profiles
|
// Clean up existing profiles and their table definitions
|
||||||
|
sqlx::query!("DELETE FROM table_definitions WHERE profile_id IN (SELECT id FROM profiles WHERE name LIKE 'empty_test%')")
|
||||||
|
.execute(&mut *tx)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
sqlx::query!("DELETE FROM profiles WHERE name LIKE 'empty_test%'")
|
sqlx::query!("DELETE FROM profiles WHERE name LIKE 'empty_test%'")
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let profile_name = format!("empty_test_{}", Utc::now().timestamp());
|
let profile_name = format!("empty_test_{}", Utc::now().timestamp_nanos()); // Unique profile name
|
||||||
let profile_id = sqlx::query_scalar!(
|
let profile_id = sqlx::query_scalar!(
|
||||||
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
|
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
|
||||||
profile_name
|
profile_name
|
||||||
@@ -227,12 +234,13 @@ async fn test_empty_table_count(#[future] pool: PgPool) {
|
|||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO table_definitions (profile_id, table_name, columns)
|
INSERT INTO table_definitions (profile_id, table_name, columns, indexes)
|
||||||
VALUES ($1, $2, $3)
|
VALUES ($1, $2, $3, $4)
|
||||||
"#,
|
"#,
|
||||||
profile_id,
|
profile_id,
|
||||||
"adresar",
|
"adresar",
|
||||||
json!({}) // Provide a valid JSON object for columns
|
json!({}), // columns
|
||||||
|
json!([]) // indexes
|
||||||
)
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
|
|||||||
Reference in New Issue
Block a user