full passer on the tables data now

This commit is contained in:
filipriec
2025-06-25 13:46:35 +02:00
parent b7a3f0f8d9
commit 2e82aba0d1

View File

@@ -3,6 +3,8 @@ use rstest::{fixture, rstest};
use tonic;
use sqlx::PgPool;
use common::proto::multieko2::tables_data::GetTableDataByPositionRequest;
use common::proto::multieko2::table_definition::{PostTableDefinitionRequest, ColumnDefinition};
use server::table_definition::handlers::post_table_definition;
use server::tables_data::handlers::get_table_data_by_position;
use crate::common::setup_test_db;
use chrono::Utc;
@@ -21,73 +23,53 @@ async fn closed_pool(#[future] pool: PgPool) -> PgPool {
}
async fn setup_test_environment(pool: &PgPool) -> (String, String, i64) {
let now = Utc::now();
let profile_name = format!("test_profile_{}", now.timestamp_nanos_opt().unwrap());
let table_name = format!("test_table_{}", now.timestamp_nanos_opt().unwrap());
// Start transaction
let mut tx = pool.begin().await.unwrap();
// Create unique profile and table names
let profile_name = format!("test_profile_{}", Utc::now().timestamp_nanos_opt().unwrap_or(0));
let table_name = format!("test_table_{}", Utc::now().timestamp_nanos_opt().unwrap_or(0));
// Create profile
let profile_id = sqlx::query_scalar!(
"INSERT INTO profiles (name) VALUES ($1) RETURNING id",
// Use the table definition handler to create the table properly
let request = PostTableDefinitionRequest {
profile_name: profile_name.clone(),
table_name: table_name.clone(),
columns: vec![
ColumnDefinition {
name: "firma".to_string(),
field_type: "text".to_string(),
}
],
indexes: vec![],
links: vec![],
};
post_table_definition(pool, request).await.unwrap();
// Get the schema_id for cleanup
let schema_id = sqlx::query_scalar!(
"SELECT id FROM schemas WHERE name = $1",
profile_name
)
.fetch_one(&mut *tx)
.fetch_one(pool)
.await
.unwrap();
// Create table definition with proper columns
let columns_json = serde_json::json!([
r#""id" BIGSERIAL PRIMARY KEY"#,
r#""deleted" BOOLEAN NOT NULL DEFAULT FALSE"#,
r#""firma" TEXT NOT NULL"#
]);
sqlx::query!(
r#"INSERT INTO table_definitions (profile_id, table_name, columns, indexes)
VALUES ($1, $2, $3, $4)"#,
profile_id,
table_name,
columns_json, // Use proper columns array
json!([])
)
.execute(&mut *tx)
.await
.unwrap();
// Create actual table
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)
(profile_name, table_name, schema_id)
}
async fn cleanup_test_environment(pool: &PgPool, profile_id: i64, table_name: &str) {
async fn cleanup_test_environment(pool: &PgPool, schema_id: i64, profile_name: &str) {
let mut tx = pool.begin().await.unwrap();
// Cleanup order matters!
sqlx::query(&format!(r#"DROP TABLE IF EXISTS "{}" CASCADE"#, table_name))
sqlx::query(&format!(r#"DROP SCHEMA IF EXISTS "{}" CASCADE"#, profile_name))
.execute(&mut *tx)
.await
.unwrap();
sqlx::query!("DELETE FROM table_definitions WHERE profile_id = $1", profile_id)
sqlx::query!("DELETE FROM table_definitions WHERE schema_id = $1", schema_id)
.execute(&mut *tx)
.await
.unwrap();
sqlx::query!("DELETE FROM profiles WHERE id = $1", profile_id)
sqlx::query!("DELETE FROM schemas WHERE id = $1", schema_id)
.execute(&mut *tx)
.await
.unwrap();
@@ -101,12 +83,13 @@ async fn test_retrieves_correct_record_by_position(
#[future] pool: PgPool,
) {
let pool = pool.await;
let (profile_name, table_name, profile_id) = setup_test_environment(&pool).await;
let (profile_name, table_name, schema_id) = setup_test_environment(&pool).await;
// Insert test data
let mut tx = pool.begin().await.unwrap();
let id1: i64 = sqlx::query_scalar(&format!(
r#"INSERT INTO "{}" (firma) VALUES ('Test 1') RETURNING id"#,
r#"INSERT INTO "{}"."{}" (firma) VALUES ('Test 1') RETURNING id"#,
profile_name,
table_name
))
.fetch_one(&mut *tx)
@@ -114,7 +97,8 @@ async fn test_retrieves_correct_record_by_position(
.unwrap();
let id2: i64 = sqlx::query_scalar(&format!(
r#"INSERT INTO "{}" (firma) VALUES ('Test 2') RETURNING id"#,
r#"INSERT INTO "{}"."{}" (firma) VALUES ('Test 2') RETURNING id"#,
profile_name,
table_name
))
.fetch_one(&mut *tx)
@@ -140,7 +124,7 @@ async fn test_retrieves_correct_record_by_position(
let response = get_table_data_by_position(&pool, request).await.unwrap();
assert_eq!(response.data["id"], id2.to_string());
cleanup_test_environment(&pool, profile_id, &table_name).await;
cleanup_test_environment(&pool, schema_id, &profile_name).await;
}
#[rstest]
@@ -149,12 +133,13 @@ async fn test_excludes_deleted_records(
#[future] pool: PgPool,
) {
let pool = pool.await;
let (profile_name, table_name, profile_id) = setup_test_environment(&pool).await;
let (profile_name, table_name, schema_id) = setup_test_environment(&pool).await;
// Insert test data
let mut tx = pool.begin().await.unwrap();
let id1: i64 = sqlx::query_scalar(&format!(
r#"INSERT INTO "{}" (firma) VALUES ('Test 1') RETURNING id"#,
r#"INSERT INTO "{}"."{}" (firma) VALUES ('Test 1') RETURNING id"#,
profile_name,
table_name
))
.fetch_one(&mut *tx)
@@ -163,7 +148,8 @@ async fn test_excludes_deleted_records(
// Insert and delete a record
let deleted_id: i64 = sqlx::query_scalar(&format!(
r#"INSERT INTO "{}" (firma) VALUES ('Deleted') RETURNING id"#,
r#"INSERT INTO "{}"."{}" (firma) VALUES ('Deleted') RETURNING id"#,
profile_name,
table_name
))
.fetch_one(&mut *tx)
@@ -171,7 +157,8 @@ async fn test_excludes_deleted_records(
.unwrap();
let id2: i64 = sqlx::query_scalar(&format!(
r#"INSERT INTO "{}" (firma) VALUES ('Test 2') RETURNING id"#,
r#"INSERT INTO "{}"."{}" (firma) VALUES ('Test 2') RETURNING id"#,
profile_name,
table_name
))
.fetch_one(&mut *tx)
@@ -179,7 +166,8 @@ async fn test_excludes_deleted_records(
.unwrap();
sqlx::query(&format!(
r#"UPDATE "{}" SET deleted = true WHERE id = $1"#,
r#"UPDATE "{}"."{}" SET deleted = true WHERE id = $1"#,
profile_name,
table_name
))
.bind(deleted_id)
@@ -206,7 +194,7 @@ async fn test_excludes_deleted_records(
let response = get_table_data_by_position(&pool, request).await.unwrap();
assert_eq!(response.data["id"], id2.to_string());
cleanup_test_environment(&pool, profile_id, &table_name).await;
cleanup_test_environment(&pool, schema_id, &profile_name).await;
}
#[rstest]
@@ -215,7 +203,7 @@ async fn test_invalid_position(
#[future] pool: PgPool,
) {
let pool = pool.await;
let (profile_name, table_name, profile_id) = setup_test_environment(&pool).await;
let (profile_name, table_name, schema_id) = setup_test_environment(&pool).await;
// Test position 0
let request = GetTableDataByPositionRequest {
@@ -237,7 +225,7 @@ async fn test_invalid_position(
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::InvalidArgument);
cleanup_test_environment(&pool, profile_id, &table_name).await;
cleanup_test_environment(&pool, schema_id, &profile_name).await;
}
#[rstest]
@@ -246,12 +234,13 @@ async fn test_position_out_of_bounds(
#[future] pool: PgPool,
) {
let pool = pool.await;
let (profile_name, table_name, profile_id) = setup_test_environment(&pool).await;
let (profile_name, table_name, schema_id) = setup_test_environment(&pool).await;
// Insert one record
let mut tx = pool.begin().await.unwrap();
sqlx::query(&format!(
r#"INSERT INTO "{}" (firma) VALUES ('Test 1')"#,
r#"INSERT INTO "{}"."{}" (firma) VALUES ('Test 1')"#,
profile_name,
table_name
))
.execute(&mut *tx)
@@ -269,7 +258,7 @@ async fn test_position_out_of_bounds(
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
cleanup_test_environment(&pool, profile_id, &table_name).await;
cleanup_test_environment(&pool, schema_id, &profile_name).await;
}
#[rstest]
@@ -278,7 +267,7 @@ async fn test_table_not_in_profile(
#[future] pool: PgPool,
) {
let pool = pool.await;
let (profile_name, _, profile_id) = setup_test_environment(&pool).await;
let (profile_name, _, schema_id) = setup_test_environment(&pool).await;
// Test with non-existent table
let request = GetTableDataByPositionRequest {
@@ -290,7 +279,7 @@ async fn test_table_not_in_profile(
assert!(result.is_err());
assert_eq!(result.unwrap_err().code(), tonic::Code::NotFound);
cleanup_test_environment(&pool, profile_id, "dummy_table").await;
cleanup_test_environment(&pool, schema_id, "dummy_table").await;
}
#[rstest]