From 06d98aab5ce588a29f1bf059d5878a26a5c0762a Mon Sep 17 00:00:00 2001 From: filipriec Date: Sat, 21 Jun 2025 21:01:49 +0200 Subject: [PATCH] 5 more tests to go --- server/Makefile | 2 +- .../handlers/post_table_definition.rs | 2 +- server/tests/common/mod.rs | 12 ++++++++---- .../post_table_definition_test2.rs | 5 +++-- .../post_table_definition_test4.rs | 17 +++++++++-------- 5 files changed, 22 insertions(+), 16 deletions(-) diff --git a/server/Makefile b/server/Makefile index 6662d93..73a91d5 100644 --- a/server/Makefile +++ b/server/Makefile @@ -8,6 +8,6 @@ reset_db: run_tests: @echo "Running tests..." - @cargo test + @cargo test --test mod -- --test-threads=1 .PHONY: test diff --git a/server/src/table_definition/handlers/post_table_definition.rs b/server/src/table_definition/handlers/post_table_definition.rs index cd1ff41..2679e6a 100644 --- a/server/src/table_definition/handlers/post_table_definition.rs +++ b/server/src/table_definition/handlers/post_table_definition.rs @@ -229,7 +229,7 @@ async fn execute_table_definition( return Err(Status::invalid_argument("Invalid column name")); } if col_name.ends_with("_id") || col_name == "id" || col_name == "deleted" || col_name == "created_at" { - return Err(Status::invalid_argument("Invalid column name")); + return Err(Status::invalid_argument("Column name cannot be 'id', 'deleted', 'created_at' or end with '_id'")); } let sql_type = map_field_type(&col_def.field_type)?; columns.push(format!("\"{}\" {}", col_name, sql_type)); diff --git a/server/tests/common/mod.rs b/server/tests/common/mod.rs index cb085bd..bc0165f 100644 --- a/server/tests/common/mod.rs +++ b/server/tests/common/mod.rs @@ -24,13 +24,17 @@ async fn get_root_connection() -> PgConnection { /// This is the key to test isolation. pub async fn setup_isolated_db() -> PgPool { let mut root_conn = get_root_connection().await; - + + // Make schema names more unique - include timestamp + random let schema_name = format!( - "test_{}", + "test_{}_{}", + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(), rand::thread_rng() - // --- CHANGE 2: Pass a reference to Alphanumeric directly --- .sample_iter(&Alphanumeric) - .take(12) + .take(8) .map(char::from) .collect::() .to_lowercase() diff --git a/server/tests/table_definition/post_table_definition_test2.rs b/server/tests/table_definition/post_table_definition_test2.rs index d4215dd..51269d5 100644 --- a/server/tests/table_definition/post_table_definition_test2.rs +++ b/server/tests/table_definition/post_table_definition_test2.rs @@ -157,8 +157,9 @@ async fn test_name_sanitization(#[future] pool: PgPool) { #[tokio::test] async fn test_create_minimal_table(#[future] pool: PgPool) { let pool = pool.await; + let profile_name = "test_minimal"; let req = PostTableDefinitionRequest { - profile_name: "default".into(), + profile_name: profile_name.into(), table_name: "minimal".into(), ..Default::default() }; @@ -168,7 +169,7 @@ async fn test_create_minimal_table(#[future] pool: PgPool) { assert!(resp.sql.contains("created_at TIMESTAMPTZ")); assert_table_structure_is_correct( &pool, - "default", // FIXED: Added schema parameter + profile_name, "minimal", &[ ("id", "bigint"), diff --git a/server/tests/table_definition/post_table_definition_test4.rs b/server/tests/table_definition/post_table_definition_test4.rs index 0378cae..faf7953 100644 --- a/server/tests/table_definition/post_table_definition_test4.rs +++ b/server/tests/table_definition/post_table_definition_test4.rs @@ -162,26 +162,24 @@ async fn test_fail_gracefully_if_schema_is_missing(#[future] pool: PgPool) { #[rstest] #[tokio::test] async fn test_column_name_with_id_suffix_is_rejected(#[future] pool: PgPool) { - // Test that column names ending with '_id' are properly rejected during input validation let pool = pool.await; - // Test 1: Column ending with '_id' should be rejected let request = PostTableDefinitionRequest { profile_name: "default".into(), - table_name: "orders".into(), // Valid table name + table_name: "orders".into(), columns: vec![ColumnDefinition { - name: "legacy_order_id".into(), // This should be rejected + name: "legacy_order_id".into(), field_type: "integer".into(), }], ..Default::default() }; - // Act & Assert - should fail validation let result = post_table_definition(&pool, request).await; assert!(result.is_err(), "Column names ending with '_id' should be rejected"); if let Err(status) = result { assert_eq!(status.code(), tonic::Code::InvalidArgument); - assert!(status.message().contains("Invalid column name")); + // Update this line to match the actual error message: + assert!(status.message().contains("Column name cannot be") && status.message().contains("end with '_id'")); } // Test 2: Column named exactly 'id' should be rejected @@ -189,14 +187,17 @@ async fn test_column_name_with_id_suffix_is_rejected(#[future] pool: PgPool) { profile_name: "default".into(), table_name: "orders".into(), columns: vec![ColumnDefinition { - name: "id".into(), // This should be rejected + name: "id".into(), field_type: "integer".into(), }], ..Default::default() }; - let result2 = post_table_definition(&pool, request2).await; assert!(result2.is_err(), "Column named 'id' should be rejected"); + if let Err(status) = result2 { + assert_eq!(status.code(), tonic::Code::InvalidArgument); + assert!(status.message().contains("Column name cannot be") && status.message().contains("'id'")); + } } #[rstest]