we have a passer

This commit is contained in:
filipriec
2025-07-17 22:55:54 +02:00
parent 24c2376ea1
commit 7d1b130b68
3 changed files with 201 additions and 65 deletions

View File

@@ -34,6 +34,21 @@ async fn create_test_table(
.expect("Failed to create test table")
}
/// Helper function to create a table link (for cross-table references)
async fn create_table_link(pool: &PgPool, source_table_id: i64, linked_table_id: i64, is_required: bool) {
sqlx::query!(
r#"INSERT INTO table_definition_links (source_table_id, linked_table_id, is_required)
VALUES ($1, $2, $3)
ON CONFLICT (source_table_id, linked_table_id) DO NOTHING"#,
source_table_id,
linked_table_id,
is_required
)
.execute(pool)
.await
.expect("Failed to create table link");
}
/// Helper function to get default schema ID
async fn get_default_schema_id(pool: &PgPool) -> i64 {
sqlx::query_scalar!("SELECT id FROM schemas WHERE name = 'default'")
@@ -59,7 +74,7 @@ async fn test_reject_bigint_target_column() {
table_definition_id: table_id,
target_column: "big_number".to_string(), // This is BIGINT
script: r#"(+ "10" "20")"#.to_string(),
description: "Test script".to_string(), // Remove Some() wrapper
description: "Test script".to_string(),
};
let result = post_table_script(&pool, request).await;
@@ -155,6 +170,8 @@ async fn test_reject_text_in_mathematical_operations() {
]
).await;
// No self-link needed - self-references are automatically allowed
let request = PostTableScriptRequest {
table_definition_id: table_id,
target_column: "result".to_string(),
@@ -191,6 +208,8 @@ async fn test_reject_boolean_in_mathematical_operations() {
]
).await;
// No self-link needed - self-references are automatically allowed
let request = PostTableScriptRequest {
table_definition_id: table_id,
target_column: "result".to_string(),
@@ -226,6 +245,8 @@ async fn test_reject_bigint_in_mathematical_operations() {
]
).await;
// No self-link needed - self-references are automatically allowed
let request = PostTableScriptRequest {
table_definition_id: table_id,
target_column: "result".to_string(),
@@ -263,6 +284,8 @@ async fn test_allow_valid_script_with_allowed_types() {
]
).await;
// No self-link needed - self-references are automatically allowed
let request = PostTableScriptRequest {
table_definition_id: table_id,
target_column: "computed_value".to_string(), // This is TEXT (allowed as target)
@@ -295,6 +318,19 @@ async fn test_allow_integer_and_numeric_in_math_operations() {
]
).await;
println!("Created table with ID: {}", table_id);
println!("Schema ID: {}", schema_id);
// Verify the table exists
let table_check = sqlx::query!(
"SELECT table_name FROM table_definitions WHERE id = $1",
table_id
)
.fetch_one(&pool)
.await;
println!("Table verification: {:?}", table_check);
let request = PostTableScriptRequest {
table_definition_id: table_id,
target_column: "total".to_string(),
@@ -303,10 +339,95 @@ async fn test_allow_integer_and_numeric_in_math_operations() {
description: "Valid mathematical operation with INTEGER and NUMERIC".to_string(),
};
println!("About to call post_table_script");
let result = post_table_script(&pool, request).await;
// SHOW THE ACTUAL ERROR
if let Err(e) = &result {
println!("ERROR: {}", e);
println!("ERROR DEBUG: {:?}", e);
}
// Should succeed
assert!(result.is_ok(), "Mathematical operations with INTEGER and NUMERIC should succeed");
let response = result.unwrap();
assert!(response.id > 0);
}
#[tokio::test]
async fn test_script_without_table_links_should_fail() {
let pool = setup_isolated_db().await;
let schema_id = get_default_schema_id(&pool).await;
// Create two separate tables
let table_a_id = create_test_table(
&pool,
schema_id,
"table_a",
vec![("value_a", "INTEGER"), ("result", "INTEGER")]
).await;
let _table_b_id = create_test_table(
&pool,
schema_id,
"table_b",
vec![("value_b", "INTEGER")]
).await;
// DON'T create a link between the tables
let request = PostTableScriptRequest {
table_definition_id: table_a_id,
target_column: "result".to_string(),
script: r#"(+ (steel_get_column "table_b" "value_b") "10")"#.to_string(),
description: "Script trying to access unlinked table".to_string(),
};
let result = post_table_script(&pool, request).await;
// Should fail because tables aren't linked
assert!(result.is_err(), "Should fail when tables aren't linked");
let error_msg = result.unwrap_err().to_string();
assert!(
error_msg.contains("not linked") || error_msg.contains("link"),
"Error should mention table linking requirement: {}",
error_msg
);
}
#[tokio::test]
async fn test_script_with_table_links_should_succeed() {
let pool = setup_isolated_db().await;
let schema_id = get_default_schema_id(&pool).await;
// Create two separate tables
let table_a_id = create_test_table(
&pool,
schema_id,
"linked_table_a",
vec![("value_a", "INTEGER"), ("result", "INTEGER")]
).await;
let table_b_id = create_test_table(
&pool,
schema_id,
"linked_table_b",
vec![("value_b", "INTEGER")]
).await;
// Create a link between the tables (table_a can access table_b)
create_table_link(&pool, table_a_id, table_b_id, false).await;
let request = PostTableScriptRequest {
table_definition_id: table_a_id,
target_column: "result".to_string(),
script: r#"(+ (steel_get_column "linked_table_b" "value_b") "10")"#.to_string(),
description: "Script accessing properly linked table".to_string(),
};
let result = post_table_script(&pool, request).await;
// Should succeed because tables are properly linked
assert!(result.is_ok(), "Should succeed when tables are properly linked");
let response = result.unwrap();
assert!(response.id > 0);
}