From ff34d97f2ac84253445020b7850d3c7448573127 Mon Sep 17 00:00:00 2001 From: filipriec Date: Wed, 5 Mar 2025 10:41:21 +0100 Subject: [PATCH] nothing changed, still 3/6 --- .../handlers/get_table_data_count_test.rs | 78 +++++++++++++++---- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/server/tests/tables_data/handlers/get_table_data_count_test.rs b/server/tests/tables_data/handlers/get_table_data_count_test.rs index e72a9f7..e9de291 100644 --- a/server/tests/tables_data/handlers/get_table_data_count_test.rs +++ b/server/tests/tables_data/handlers/get_table_data_count_test.rs @@ -4,8 +4,9 @@ use tonic; use sqlx::PgPool; use common::proto::multieko2::tables_data::GetTableDataCountRequest; use server::tables_data::handlers::get_table_data_count; - use crate::common::setup_test_db; +use chrono::Utc; +use serde_json::json; #[fixture] async fn pool() -> PgPool { @@ -25,17 +26,30 @@ async fn test_returns_correct_count(#[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(); + // Setup profile and table definition + let profile_name = format!("test_profile_{}", Utc::now().timestamp()); let profile_id = sqlx::query_scalar!( - "INSERT INTO profiles (name) VALUES ('test_profile') RETURNING id" + "INSERT INTO profiles (name) VALUES ($1) RETURNING id", + profile_name ) .fetch_one(&mut *tx) .await .unwrap(); sqlx::query!( - "INSERT INTO table_definitions (profile_id, table_name) VALUES ($1, 'adresar')", - profile_id + 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) .await @@ -56,7 +70,7 @@ async fn test_returns_correct_count(#[future] pool: PgPool) { // Call handler let request = GetTableDataCountRequest { - profile_name: "test_profile".to_string(), + profile_name, table_name: "adresar".to_string(), }; let response = get_table_data_count(&pool, request).await.unwrap(); @@ -78,16 +92,29 @@ async fn test_excludes_deleted_records(#[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 ('test_profile') RETURNING id" + "INSERT INTO profiles (name) VALUES ($1) RETURNING id", + profile_name ) .fetch_one(&mut *tx) .await .unwrap(); sqlx::query!( - "INSERT INTO table_definitions (profile_id, table_name) VALUES ($1, 'adresar')", - profile_id + 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) .await @@ -106,7 +133,7 @@ async fn test_excludes_deleted_records(#[future] pool: PgPool) { tx.commit().await.unwrap(); let request = GetTableDataCountRequest { - profile_name: "test_profile".to_string(), + profile_name, table_name: "adresar".to_string(), }; let response = get_table_data_count(&pool, request).await.unwrap(); @@ -135,8 +162,16 @@ 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 ('test_profile') RETURNING id" + "INSERT INTO profiles (name) VALUES ($1) RETURNING id", + profile_name ) .fetch_one(&mut *tx) .await @@ -146,7 +181,7 @@ async fn test_table_not_in_profile(#[future] pool: PgPool) { tx.commit().await.unwrap(); let request = GetTableDataCountRequest { - profile_name: "test_profile".to_string(), + profile_name, table_name: "adresar".to_string(), }; let result = get_table_data_count(&pool, request).await; @@ -175,16 +210,29 @@ async fn test_empty_table_count(#[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 'empty_test%'") + .execute(&mut *tx) + .await + .unwrap(); + + let profile_name = format!("empty_test_{}", Utc::now().timestamp()); let profile_id = sqlx::query_scalar!( - "INSERT INTO profiles (name) VALUES ('empty_test') RETURNING id" + "INSERT INTO profiles (name) VALUES ($1) RETURNING id", + profile_name ) .fetch_one(&mut *tx) .await .unwrap(); sqlx::query!( - "INSERT INTO table_definitions (profile_id, table_name) VALUES ($1, 'adresar')", - profile_id + 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) .await @@ -193,7 +241,7 @@ async fn test_empty_table_count(#[future] pool: PgPool) { tx.commit().await.unwrap(); let request = GetTableDataCountRequest { - profile_name: "empty_test".to_string(), + profile_name, table_name: "adresar".to_string(), }; let response = get_table_data_count(&pool, request).await.unwrap();