steel scripts now have far better logic than before
This commit is contained in:
181
server/tests/table_script/prohibited_types_test.rs
Normal file
181
server/tests/table_script/prohibited_types_test.rs
Normal file
@@ -0,0 +1,181 @@
|
||||
// tests/table_script/prohibited_types_test.rs
|
||||
|
||||
#[cfg(test)]
|
||||
mod prohibited_types_tests {
|
||||
use super::*;
|
||||
use common::proto::multieko2::table_script::PostTableScriptRequest;
|
||||
use sqlx::PgPool;
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reject_bigint_target_column() {
|
||||
let pool = setup_test_db().await;
|
||||
|
||||
// Create a table with a BIGINT column
|
||||
let table_id = create_test_table_with_bigint_column(&pool).await;
|
||||
|
||||
let request = PostTableScriptRequest {
|
||||
table_definition_id: table_id,
|
||||
target_column: "big_number".to_string(), // This is BIGINT
|
||||
script: r#"
|
||||
(define result "some calculation")
|
||||
result
|
||||
"#.to_string(),
|
||||
description: "Test script".to_string(),
|
||||
};
|
||||
|
||||
let result = post_table_script(&pool, request).await;
|
||||
|
||||
// Should fail with prohibited type error
|
||||
assert!(result.is_err());
|
||||
let error_msg = result.unwrap_err().to_string();
|
||||
assert!(error_msg.contains("Cannot create script for column 'big_number' with type 'BIGINT'"));
|
||||
assert!(error_msg.contains("Steel scripts cannot target columns of type: BIGINT, DATE, TIMESTAMPTZ"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reject_date_target_column() {
|
||||
let pool = setup_test_db().await;
|
||||
|
||||
// Create a table with a DATE column
|
||||
let table_id = create_test_table_with_date_column(&pool).await;
|
||||
|
||||
let request = PostTableScriptRequest {
|
||||
table_definition_id: table_id,
|
||||
target_column: "event_date".to_string(), // This is DATE
|
||||
script: r#"
|
||||
(define result "2024-01-01")
|
||||
result
|
||||
"#.to_string(),
|
||||
description: "Test script".to_string(),
|
||||
};
|
||||
|
||||
let result = post_table_script(&pool, request).await;
|
||||
|
||||
// Should fail with prohibited type error
|
||||
assert!(result.is_err());
|
||||
let error_msg = result.unwrap_err().to_string();
|
||||
assert!(error_msg.contains("Cannot create script for column 'event_date' with type 'DATE'"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reject_timestamptz_target_column() {
|
||||
let pool = setup_test_db().await;
|
||||
|
||||
// Create a table with a TIMESTAMPTZ column
|
||||
let table_id = create_test_table_with_timestamptz_column(&pool).await;
|
||||
|
||||
let request = PostTableScriptRequest {
|
||||
table_definition_id: table_id,
|
||||
target_column: "created_time".to_string(), // This is TIMESTAMPTZ
|
||||
script: r#"
|
||||
(define result "2024-01-01T10:00:00Z")
|
||||
result
|
||||
"#.to_string(),
|
||||
description: "Test script".to_string(),
|
||||
};
|
||||
|
||||
let result = post_table_script(&pool, request).await;
|
||||
|
||||
// Should fail with prohibited type error
|
||||
assert!(result.is_err());
|
||||
let error_msg = result.unwrap_err().to_string();
|
||||
assert!(error_msg.contains("Cannot create script for column 'created_time' with type 'TIMESTAMPTZ'"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_reject_script_referencing_prohibited_column() {
|
||||
let pool = setup_test_db().await;
|
||||
|
||||
// Create linked tables - one with BIGINT column, another with TEXT target
|
||||
let source_table_id = create_test_table_with_text_column(&pool).await;
|
||||
let linked_table_id = create_test_table_with_bigint_column(&pool).await;
|
||||
|
||||
// Create link between tables
|
||||
create_table_link(&pool, source_table_id, linked_table_id).await;
|
||||
|
||||
let request = PostTableScriptRequest {
|
||||
table_definition_id: source_table_id,
|
||||
target_column: "description".to_string(), // This is TEXT (allowed)
|
||||
script: r#"
|
||||
(define big_val (steel_get_column "linked_table" "big_number"))
|
||||
(string-append "Value: " (number->string big_val))
|
||||
"#.to_string(),
|
||||
description: "Script that tries to access BIGINT column".to_string(),
|
||||
};
|
||||
|
||||
let result = post_table_script(&pool, request).await;
|
||||
|
||||
// Should fail because script references BIGINT column
|
||||
assert!(result.is_err());
|
||||
let error_msg = result.unwrap_err().to_string();
|
||||
assert!(error_msg.contains("Script cannot reference column 'big_number'"));
|
||||
assert!(error_msg.contains("prohibited type 'BIGINT'"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_allow_valid_script_with_allowed_types() {
|
||||
let pool = setup_test_db().await;
|
||||
|
||||
// Create a table with allowed column types
|
||||
let table_id = create_test_table_with_allowed_columns(&pool).await;
|
||||
|
||||
let request = PostTableScriptRequest {
|
||||
table_definition_id: table_id,
|
||||
target_column: "computed_value".to_string(), // This is TEXT (allowed)
|
||||
script: r#"
|
||||
(define name_val (steel_get_column "test_table" "name"))
|
||||
(define count_val (steel_get_column "test_table" "count"))
|
||||
(string-append name_val " has " (number->string count_val) " items")
|
||||
"#.to_string(),
|
||||
description: "Valid script using allowed types".to_string(),
|
||||
};
|
||||
|
||||
let result = post_table_script(&pool, request).await;
|
||||
|
||||
// Should succeed
|
||||
assert!(result.is_ok());
|
||||
let response = result.unwrap();
|
||||
assert!(response.id > 0);
|
||||
}
|
||||
|
||||
// Helper functions for test setup
|
||||
async fn setup_test_db() -> PgPool {
|
||||
// Your test database setup code here
|
||||
todo!("Implement test DB setup")
|
||||
}
|
||||
|
||||
async fn create_test_table_with_bigint_column(pool: &PgPool) -> i64 {
|
||||
// Create table definition with BIGINT column
|
||||
// JSON columns would be: ["name TEXT", "big_number BIGINT"]
|
||||
todo!("Implement table creation with BIGINT")
|
||||
}
|
||||
|
||||
async fn create_test_table_with_date_column(pool: &PgPool) -> i64 {
|
||||
// Create table definition with DATE column
|
||||
// JSON columns would be: ["name TEXT", "event_date DATE"]
|
||||
todo!("Implement table creation with DATE")
|
||||
}
|
||||
|
||||
async fn create_test_table_with_timestamptz_column(pool: &PgPool) -> i64 {
|
||||
// Create table definition with TIMESTAMPTZ column
|
||||
// JSON columns would be: ["name TEXT", "created_time TIMESTAMPTZ"]
|
||||
todo!("Implement table creation with TIMESTAMPTZ")
|
||||
}
|
||||
|
||||
async fn create_test_table_with_text_column(pool: &PgPool) -> i64 {
|
||||
// Create table definition with TEXT columns only
|
||||
// JSON columns would be: ["name TEXT", "description TEXT"]
|
||||
todo!("Implement table creation with TEXT")
|
||||
}
|
||||
|
||||
async fn create_test_table_with_allowed_columns(pool: &PgPool) -> i64 {
|
||||
// Create table definition with only allowed column types
|
||||
// JSON columns would be: ["name TEXT", "count INTEGER", "computed_value TEXT"]
|
||||
todo!("Implement table creation with allowed types")
|
||||
}
|
||||
|
||||
async fn create_table_link(pool: &PgPool, source_id: i64, target_id: i64) {
|
||||
// Create a link in table_definition_links
|
||||
todo!("Implement table linking")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user