From fd36cd57953573c544265b7f53caf48e42b4ebdc Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 22 Jun 2025 13:13:20 +0200 Subject: [PATCH] tests are now passing fully --- .../post_table_definition_test5.rs | 94 +++++++++++-------- 1 file changed, 57 insertions(+), 37 deletions(-) diff --git a/server/tests/table_definition/post_table_definition_test5.rs b/server/tests/table_definition/post_table_definition_test5.rs index a54be77..2dfc7c3 100644 --- a/server/tests/table_definition/post_table_definition_test5.rs +++ b/server/tests/table_definition/post_table_definition_test5.rs @@ -120,8 +120,8 @@ async fn test_unicode_in_schema_names_rejected(#[future] pool: PgPool) { #[tokio::test] async fn test_fk_column_name_uniqueness_collision(#[future] pool: PgPool) { let pool = pool.await; - - // Create tables that would cause FK column name collisions + + // Create tables with similar suffixes let req1 = PostTableDefinitionRequest { profile_name: "default".into(), table_name: "customers_146053".into(), @@ -130,7 +130,7 @@ async fn test_fk_column_name_uniqueness_collision(#[future] pool: PgPool) { links: vec![], }; post_table_definition(&pool, req1).await.unwrap(); - + let req2 = PostTableDefinitionRequest { profile_name: "default".into(), table_name: "suppliers_146053".into(), @@ -139,11 +139,11 @@ async fn test_fk_column_name_uniqueness_collision(#[future] pool: PgPool) { links: vec![], }; post_table_definition(&pool, req2).await.unwrap(); - - // Try to create a table linking to both - this should reveal the FK naming bug + + // Create a table linking to both - should succeed with full table names let request = PostTableDefinitionRequest { profile_name: "default".into(), - table_name: "orders".into(), + table_name: "orders_test".into(), // Use unique name to avoid conflicts columns: vec![], indexes: vec![], links: vec![ @@ -157,23 +157,20 @@ async fn test_fk_column_name_uniqueness_collision(#[future] pool: PgPool) { }, ], }; - + let result = post_table_definition(&pool, request).await; - - // This test documents the current bug - both tables create "146053_id" columns - if result.is_err() { - let err = result.unwrap_err(); - if err.message().contains("specified more than once") { - // This confirms the FK naming collision bug described in the analysis - assert!(err.message().contains("146053_id")); - } else { - // If it's a different error, let it fail normally - panic!("Unexpected error: {:?}", err); - } - } else { - // If this passes, the bug has been fixed - assert!(result.is_ok()); - } + + // Should succeed - no collision with full table names + assert!(result.is_ok()); + let response = result.unwrap(); + + // Verify both FK columns are created with full table names + assert!(response.sql.contains("\"customers_146053_id\"")); + assert!(response.sql.contains("\"suppliers_146053_id\"")); + + // Verify both are NOT NULL (required = true) + assert!(response.sql.contains("\"customers_146053_id\" BIGINT NOT NULL")); + assert!(response.sql.contains("\"suppliers_146053_id\" BIGINT NOT NULL")); } #[rstest] @@ -183,7 +180,7 @@ async fn test_cross_schema_references_prevented(#[future] pool: PgPool) { // Create table in schema A let req_a = PostTableDefinitionRequest { - profile_name: "A".into(), + profile_name: "scheam_a".into(), table_name: "users".into(), columns: vec![], indexes: vec![], @@ -193,7 +190,7 @@ async fn test_cross_schema_references_prevented(#[future] pool: PgPool) { // Try to link from schema B to schema A's table let req_b = PostTableDefinitionRequest { - profile_name: "B".into(), + profile_name: "schema_b".into(), table_name: "orders".into(), columns: vec![], indexes: vec![], @@ -309,31 +306,54 @@ async fn test_empty_schema_and_table_names_rejected(#[future] pool: PgPool) { #[tokio::test] async fn test_schema_name_case_sensitivity(#[future] pool: PgPool) { let pool = pool.await; - - // Test that schema names are properly case-sensitive + + // First, verify that uppercase letters are rejected + let invalid_request = PostTableDefinitionRequest { + profile_name: "TestSchema".into(), // Contains uppercase - should be rejected + table_name: "test_table".into(), + columns: vec![], + indexes: vec![], + links: vec![], + }; + let result = post_table_definition(&pool, invalid_request).await; + assert!(result.is_err()); + let err = result.unwrap_err(); + assert_eq!(err.code(), Code::InvalidArgument); + assert!(err.message().contains("contains invalid characters")); + + // Now test with valid lowercase names - create first schema let request1 = PostTableDefinitionRequest { - profile_name: "TestSchema".into(), + profile_name: "test_schema_a".into(), table_name: "test_table".into(), columns: vec![], indexes: vec![], links: vec![], }; post_table_definition(&pool, request1).await.unwrap(); - - // Different case should be treated as different schema + + // Different lowercase schema should work fine let request2 = PostTableDefinitionRequest { - profile_name: "testschema".into(), - table_name: "test_table".into(), + profile_name: "test_schema_b".into(), + table_name: "test_table".into(), // Same table name, different schema columns: vec![], indexes: vec![], links: vec![], }; - let result = post_table_definition(&pool, request2).await; - // Under case-insensitive profiles this must collide - assert!(result.is_err(), "Expected duplicate‐schema error"); - let err = result.unwrap_err(); - // pick the right code for “already exists” in your handler - assert_eq!(err.code(), Code::AlreadyExists, "{:?}", err); + let result2 = post_table_definition(&pool, request2).await; + assert!(result2.is_ok(), "Different schemas should allow same table names"); + + // Same schema name should cause table collision + let request3 = PostTableDefinitionRequest { + profile_name: "test_schema_a".into(), // Same schema as request1 + table_name: "test_table".into(), // Same table name as request1 + columns: vec![], + indexes: vec![], + links: vec![], + }; + let result3 = post_table_definition(&pool, request3).await; + assert!(result3.is_err(), "Same schema + table should cause collision"); + let err3 = result3.unwrap_err(); + assert_eq!(err3.code(), Code::AlreadyExists); } #[rstest]