fixing post table script
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
// tests/table_script/post_scripts_integration_tests.rs
|
||||
|
||||
#[cfg(test)]
|
||||
mod integration_tests {
|
||||
use super::*;
|
||||
use sqlx::PgPool;
|
||||
use tokio_test;
|
||||
use serde_json::json;
|
||||
use common::proto::multieko2::table_script::{PostTableScriptRequest, TableScriptResponse};
|
||||
use std::collections::HashMap;
|
||||
use server::table_script::handlers::post_table_script::post_table_script;
|
||||
|
||||
/// Test utilities for table script integration testing
|
||||
pub struct TableScriptTestHelper {
|
||||
@@ -18,10 +18,10 @@ mod integration_tests {
|
||||
pub async fn new(test_name: &str) -> Self {
|
||||
let database_url = std::env::var("TEST_DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost/multieko2_test".to_string());
|
||||
|
||||
|
||||
let pool = PgPool::connect(&database_url).await.expect("Failed to connect to test database");
|
||||
sqlx::migrate!("./migrations").run(&pool).await.expect("Failed to run migrations");
|
||||
|
||||
|
||||
let schema_name = format!("test_schema_{}", test_name);
|
||||
let schema_id = sqlx::query_scalar!(
|
||||
"INSERT INTO schemas (name) VALUES ($1) RETURNING id",
|
||||
@@ -43,7 +43,7 @@ mod integration_tests {
|
||||
.iter()
|
||||
.map(|(name, type_def)| format!("\"{}\" {}", name, type_def))
|
||||
.collect();
|
||||
|
||||
|
||||
let columns_json = json!(columns);
|
||||
let indexes_json = json!([]);
|
||||
|
||||
@@ -65,7 +65,7 @@ mod integration_tests {
|
||||
table_definition_id: table_id,
|
||||
target_column: target_column.to_string(),
|
||||
script: script.to_string(),
|
||||
description: Some("Test script".to_string()),
|
||||
description: "Test script".to_string(), // Fixed: removed Some()
|
||||
};
|
||||
|
||||
post_table_script(&self.pool, request).await
|
||||
@@ -75,7 +75,7 @@ mod integration_tests {
|
||||
let _ = sqlx::query(&format!("DROP SCHEMA IF EXISTS \"{}\" CASCADE", self.schema_name))
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
|
||||
|
||||
let _ = sqlx::query("DELETE FROM schemas WHERE name = $1")
|
||||
.bind(&self.schema_name)
|
||||
.execute(&self.pool)
|
||||
@@ -86,7 +86,7 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_comprehensive_type_validation_matrix() {
|
||||
let helper = TableScriptTestHelper::new("type_validation_matrix").await;
|
||||
|
||||
|
||||
// Create a comprehensive table with all supported and unsupported types
|
||||
let table_id = helper.create_table_with_types(
|
||||
"comprehensive_table",
|
||||
@@ -96,16 +96,16 @@ mod integration_tests {
|
||||
("numeric_basic", "NUMERIC(10, 2)"),
|
||||
("numeric_high_precision", "NUMERIC(28, 15)"),
|
||||
("numeric_currency", "NUMERIC(14, 4)"),
|
||||
|
||||
|
||||
// Supported but not for math operations
|
||||
("text_col", "TEXT"),
|
||||
("boolean_col", "BOOLEAN"),
|
||||
|
||||
|
||||
// Prohibited types entirely
|
||||
("bigint_col", "BIGINT"),
|
||||
("date_col", "DATE"),
|
||||
("date_col", "DATE"),
|
||||
("timestamp_col", "TIMESTAMPTZ"),
|
||||
|
||||
|
||||
// Result columns of various types
|
||||
("result_integer", "INTEGER"),
|
||||
("result_numeric", "NUMERIC(15, 5)"),
|
||||
@@ -120,14 +120,14 @@ mod integration_tests {
|
||||
("numeric_basic", "*", "result_numeric", true),
|
||||
("numeric_high_precision", "/", "result_numeric", true),
|
||||
("integer_col", "sqrt", "result_numeric", true),
|
||||
|
||||
|
||||
// Invalid mathematical operations - prohibited types in math
|
||||
("text_col", "+", "result_numeric", false),
|
||||
("boolean_col", "*", "result_numeric", false),
|
||||
("bigint_col", "/", "result_numeric", false),
|
||||
("date_col", "-", "result_numeric", false),
|
||||
("timestamp_col", "+", "result_numeric", false),
|
||||
|
||||
|
||||
// Invalid target columns - prohibited types as targets
|
||||
("integer_col", "+", "bigint_col", false),
|
||||
("numeric_basic", "*", "date_col", false),
|
||||
@@ -166,7 +166,7 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_steel_decimal_precision_requirements() {
|
||||
let helper = TableScriptTestHelper::new("precision_requirements").await;
|
||||
|
||||
|
||||
// Create table with various precision requirements
|
||||
let table_id = helper.create_table_with_types(
|
||||
"precision_table",
|
||||
@@ -220,7 +220,7 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_complex_financial_calculations() {
|
||||
let helper = TableScriptTestHelper::new("financial_calculations").await;
|
||||
|
||||
|
||||
// Create a realistic financial table
|
||||
let table_id = helper.create_table_with_types(
|
||||
"financial_instruments",
|
||||
@@ -236,11 +236,11 @@ mod integration_tests {
|
||||
|
||||
// Complex compound interest calculation
|
||||
let compound_interest_script = r#"
|
||||
(-
|
||||
(*
|
||||
(-
|
||||
(*
|
||||
(steel_get_column "financial_instruments" "principal")
|
||||
(pow
|
||||
(+ "1"
|
||||
(pow
|
||||
(+ "1"
|
||||
(/ (steel_get_column "financial_instruments" "annual_rate")
|
||||
(steel_get_column "financial_instruments" "compounding_periods")))
|
||||
(* (steel_get_column "financial_instruments" "years")
|
||||
@@ -258,9 +258,9 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_scientific_notation_support() {
|
||||
let helper = TableScriptTestHelper::new("scientific_notation").await;
|
||||
|
||||
|
||||
let table_id = helper.create_table_with_types(
|
||||
"scientific_data",
|
||||
"scientific_data",
|
||||
vec![
|
||||
("large_number", "NUMERIC(30, 10)"),
|
||||
("small_number", "NUMERIC(30, 20)"),
|
||||
@@ -270,7 +270,7 @@ mod integration_tests {
|
||||
|
||||
// Test that steel_decimal can handle scientific notation in scripts
|
||||
let scientific_script = r#"
|
||||
(+
|
||||
(+
|
||||
(steel_get_column "scientific_data" "large_number")
|
||||
(* "1.5e-10" (steel_get_column "scientific_data" "small_number")))
|
||||
"#;
|
||||
@@ -284,7 +284,7 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_script_dependencies_and_cycles() {
|
||||
let helper = TableScriptTestHelper::new("dependencies_cycles").await;
|
||||
|
||||
|
||||
// Create multiple tables to test dependencies
|
||||
let table_a_id = helper.create_table_with_types(
|
||||
"table_a",
|
||||
@@ -295,7 +295,7 @@ mod integration_tests {
|
||||
).await;
|
||||
|
||||
let table_b_id = helper.create_table_with_types(
|
||||
"table_b",
|
||||
"table_b",
|
||||
vec![
|
||||
("value_b", "NUMERIC(10, 2)"),
|
||||
("result_b", "NUMERIC(10, 2)"),
|
||||
@@ -310,8 +310,8 @@ mod integration_tests {
|
||||
// Try to create circular dependency: table_b.result_b depends on table_a.result_a
|
||||
let script_b = r#"(* (steel_get_column "table_a" "result_a") "2")"#;
|
||||
let result_b = helper.create_script(table_b_id, "result_b", script_b).await;
|
||||
|
||||
// This should either succeed (if cycle detection allows this pattern)
|
||||
|
||||
// This should either succeed (if cycle detection allows this pattern)
|
||||
// or fail (if cycle detection is strict)
|
||||
// Based on the code, it should detect and prevent cycles
|
||||
if result_b.is_err() {
|
||||
@@ -328,7 +328,7 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_error_message_quality() {
|
||||
let helper = TableScriptTestHelper::new("error_messages").await;
|
||||
|
||||
|
||||
let table_id = helper.create_table_with_types(
|
||||
"error_test_table",
|
||||
vec![
|
||||
@@ -354,7 +354,7 @@ mod integration_tests {
|
||||
"TEXT in math operations should give clear error"
|
||||
),
|
||||
(
|
||||
"numeric_field",
|
||||
"numeric_field",
|
||||
r#"(* (steel_get_column "error_test_table" "boolean_field") "5")"#,
|
||||
vec!["mathematical", "BOOLEAN", "operations"],
|
||||
"BOOLEAN in math operations should give clear error"
|
||||
@@ -376,7 +376,7 @@ mod integration_tests {
|
||||
for (target_column, script, expected_keywords, description) in error_scenarios {
|
||||
let result = helper.create_script(table_id, target_column, script).await;
|
||||
assert!(result.is_err(), "{}", description);
|
||||
|
||||
|
||||
let error_message = result.unwrap_err().to_string().to_lowercase();
|
||||
for keyword in expected_keywords {
|
||||
assert!(
|
||||
@@ -393,7 +393,7 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_performance_with_complex_nested_expressions() {
|
||||
let helper = TableScriptTestHelper::new("performance_test").await;
|
||||
|
||||
|
||||
let table_id = helper.create_table_with_types(
|
||||
"performance_table",
|
||||
vec![
|
||||
@@ -408,14 +408,14 @@ mod integration_tests {
|
||||
// Create a deeply nested mathematical expression
|
||||
let complex_script = r#"
|
||||
(+
|
||||
(*
|
||||
(*
|
||||
(pow (steel_get_column "performance_table" "x") "3")
|
||||
(sqrt (steel_get_column "performance_table" "y")))
|
||||
(-
|
||||
(/
|
||||
(/
|
||||
(+ (steel_get_column "performance_table" "z") "100")
|
||||
(max (steel_get_column "performance_table" "w") "1"))
|
||||
(* "0.5"
|
||||
(* "0.5"
|
||||
(abs (- (steel_get_column "performance_table" "x")
|
||||
(steel_get_column "performance_table" "y"))))))
|
||||
"#;
|
||||
@@ -433,13 +433,13 @@ mod integration_tests {
|
||||
#[tokio::test]
|
||||
async fn test_boundary_conditions() {
|
||||
let helper = TableScriptTestHelper::new("boundary_conditions").await;
|
||||
|
||||
|
||||
// Test boundary conditions for NUMERIC types
|
||||
let table_id = helper.create_table_with_types(
|
||||
"boundary_table",
|
||||
vec![
|
||||
("min_numeric", "NUMERIC(1, 0)"), // Minimum: single digit, no decimal
|
||||
("max_numeric", "NUMERIC(1000, 999)"), // Maximum PostgreSQL allows
|
||||
("max_numeric", "NUMERIC(1000, 999)"), // Maximum PostgreSQL allows
|
||||
("zero_scale", "NUMERIC(10, 0)"), // Integer-like numeric
|
||||
("max_scale", "NUMERIC(28, 28)"), // Maximum scale
|
||||
("result", "NUMERIC(1000, 999)"),
|
||||
@@ -447,7 +447,7 @@ mod integration_tests {
|
||||
).await;
|
||||
|
||||
let boundary_script = r#"
|
||||
(+
|
||||
(+
|
||||
(steel_get_column "boundary_table" "min_numeric")
|
||||
(steel_get_column "boundary_table" "zero_scale"))
|
||||
"#;
|
||||
@@ -461,16 +461,16 @@ mod integration_tests {
|
||||
|
||||
#[cfg(test)]
|
||||
mod steel_decimal_integration_tests {
|
||||
use super::*;
|
||||
use crate::steel::server::execution::execute_script;
|
||||
use server::steel::server::execution::execute_script;
|
||||
use std::sync::Arc;
|
||||
use std::collections::HashMap;
|
||||
use sqlx::PgPool; // Fixed: added PgPool import
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_steel_decimal_execution_with_valid_types() {
|
||||
let database_url = std::env::var("TEST_DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost/multieko2_test".to_string());
|
||||
|
||||
|
||||
let pool = Arc::new(PgPool::connect(&database_url).await.expect("Failed to connect"));
|
||||
|
||||
// Test that steel_decimal execution works with INTEGER and NUMERIC types
|
||||
@@ -480,7 +480,7 @@ mod steel_decimal_integration_tests {
|
||||
row_data.insert("tax_rate".to_string(), "0.0825".to_string());
|
||||
|
||||
let script = r#"
|
||||
(+
|
||||
(+
|
||||
(* amount quantity)
|
||||
(* amount tax_rate))
|
||||
"#;
|
||||
@@ -502,7 +502,7 @@ mod steel_decimal_integration_tests {
|
||||
async fn test_steel_decimal_precision_handling() {
|
||||
let database_url = std::env::var("TEST_DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost/multieko2_test".to_string());
|
||||
|
||||
|
||||
let pool = Arc::new(PgPool::connect(&database_url).await.expect("Failed to connect"));
|
||||
|
||||
// Test high precision calculations
|
||||
@@ -524,7 +524,7 @@ mod steel_decimal_integration_tests {
|
||||
|
||||
assert!(result.is_ok(), "Steel decimal should handle high precision calculations");
|
||||
|
||||
if let Ok(crate::steel::server::execution::Value::Strings(values)) = result {
|
||||
if let Ok(server::steel::server::execution::Value::Strings(values)) = result {
|
||||
assert!(!values.is_empty(), "Should return calculated values");
|
||||
// The result should maintain precision
|
||||
let result_value: f64 = values[0].parse().unwrap_or(0.0);
|
||||
@@ -537,21 +537,19 @@ mod steel_decimal_integration_tests {
|
||||
#[cfg(test)]
|
||||
pub mod test_config {
|
||||
use std::sync::Once;
|
||||
|
||||
|
||||
static INIT: Once = Once::new();
|
||||
|
||||
|
||||
pub fn setup_test_environment() {
|
||||
INIT.call_once(|| {
|
||||
// Set up test environment variables
|
||||
std::env::set_var("TEST_DATABASE_URL",
|
||||
std::env::set_var("TEST_DATABASE_URL",
|
||||
std::env::var("TEST_DATABASE_URL")
|
||||
.unwrap_or_else(|_| "postgresql://postgres:postgres@localhost/multieko2_test".to_string())
|
||||
);
|
||||
|
||||
// Initialize logging for tests if needed
|
||||
let _ = env_logger::builder()
|
||||
.filter_level(log::LevelFilter::Warn)
|
||||
.try_init();
|
||||
|
||||
// Initialize logging for tests if needed (removed env_logger dependency)
|
||||
println!("Test environment initialized");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user