CHECK THIS COMMIT I HAVE NO CLUE IF ITS CORRECT

This commit is contained in:
filipriec
2025-07-23 23:30:56 +02:00
parent 8a6a584cf3
commit aca3d718b5
4 changed files with 321 additions and 124 deletions

View File

@@ -117,7 +117,7 @@ async fn test_put_basic_arithmetic_validation_success(pool: PgPool) {
let script_request = PostTableScriptRequest {
table_definition_id: table_def_id,
target_column: "total".to_string(),
script: "(* $price $quantity)".to_string(),
script: "(* @price @quantity)".to_string(),
description: "Total = Price × Quantity".to_string(),
};
post_table_script(&pool, script_request).await.unwrap();
@@ -155,7 +155,7 @@ async fn test_put_basic_arithmetic_validation_failure(pool: PgPool) {
let script_request = PostTableScriptRequest {
table_definition_id: table_def_id,
target_column: "total".to_string(),
script: "(* $price $quantity)".to_string(),
script: "(* @price @quantity)".to_string(),
description: "Total = Price × Quantity".to_string(),
};
post_table_script(&pool, script_request).await.unwrap();
@@ -187,7 +187,7 @@ async fn test_put_basic_arithmetic_validation_failure(pool: PgPool) {
assert_eq!(error.code(), tonic::Code::InvalidArgument);
let msg = error.message();
assert!(msg.contains("Validation failed for column 'total'"));
assert!(msg.contains("Script calculated '76.5'"));
assert!(msg.contains("Script calculated '76.50'"));
assert!(msg.contains("but user provided '70.00'"));
}
@@ -199,7 +199,7 @@ async fn test_put_complex_formula_validation(pool: PgPool) {
let script_request = PostTableScriptRequest {
table_definition_id: table_def_id,
target_column: "total".to_string(),
script: "(+ (* $price $quantity) (* (* $price $quantity) 0.08))".to_string(),
script: "(+ (* @price @quantity) (* (* @price @quantity) 0.08))".to_string(),
description: "Total with 8% tax".to_string(),
};
post_table_script(&pool, script_request).await.unwrap();
@@ -236,7 +236,7 @@ async fn test_put_division_with_precision(pool: PgPool) {
let script_request = PostTableScriptRequest {
table_definition_id: table_def_id,
target_column: "percentage".to_string(),
script: "(/ $total $price)".to_string(),
script: "(/ @total @price)".to_string(),
description: "Percentage = Total / Price".to_string(),
};
post_table_script(&pool, script_request).await.unwrap();
@@ -301,7 +301,7 @@ async fn test_put_advanced_math_functions(pool: PgPool) {
let sqrt_script = PostTableScriptRequest {
table_definition_id: table_row.id,
target_column: "square_root".to_string(),
script: "(sqrt $input)".to_string(),
script: "(sqrt @input)".to_string(),
description: "Square root validation".to_string(),
};
post_table_script(&pool, sqrt_script).await.unwrap();
@@ -309,7 +309,7 @@ async fn test_put_advanced_math_functions(pool: PgPool) {
let power_script = PostTableScriptRequest {
table_definition_id: table_row.id,
target_column: "power_result".to_string(),
script: "(^ $input 2.0)".to_string(),
script: "(^ @input 2.0)".to_string(),
description: "Power function validation".to_string(),
};
post_table_script(&pool, power_script).await.unwrap();
@@ -364,7 +364,7 @@ async fn test_put_financial_calculations(pool: PgPool) {
let compound_script = PostTableScriptRequest {
table_definition_id: table_row.id,
target_column: "compound_result".to_string(),
script: "(* $principal (^ (+ 1.0 $rate) $time))".to_string(),
script: "(* @principal (^ (+ 1.0 @rate) @time))".to_string(),
description: "Compound interest calculation".to_string(),
};
post_table_script(&pool, compound_script).await.unwrap();
@@ -372,7 +372,7 @@ async fn test_put_financial_calculations(pool: PgPool) {
let percentage_script = PostTableScriptRequest {
table_definition_id: table_row.id,
target_column: "percentage_result".to_string(),
script: "(* $principal $rate)".to_string(),
script: "(* @principal @rate)".to_string(),
description: "Percentage calculation".to_string(),
};
post_table_script(&pool, percentage_script).await.unwrap();
@@ -416,17 +416,15 @@ async fn test_put_partial_update_with_validation(pool: PgPool) {
let script_request = PostTableScriptRequest {
table_definition_id: table_def_id,
target_column: "total".to_string(),
script: "(* $price $quantity)".to_string(),
script: "(* @price @quantity)".to_string(),
description: "Total = Price × Quantity".to_string(),
};
post_table_script(&pool, script_request).await.unwrap();
let record_id = create_initial_record(&pool, "test_put_partial", "invoice", &indexer_tx).await;
// Partial update: only update quantity. Validation for total should still run and pass.
// The merged context will be { price: 10.00, quantity: 5, total: 10.00, ... }
// The script will calculate total as 10.00 * 5 = 50.00.
// Since we are not providing 'total' in the update, validation for it is skipped.
// Partial update: only update quantity. The script detects this would change total
// from 10.00 to 50.00 and requires the user to include 'total' in the update.
let mut update_data = HashMap::new();
update_data.insert("quantity".to_string(), ProtoValue {
kind: Some(Kind::NumberValue(5.0)),
@@ -439,8 +437,13 @@ async fn test_put_partial_update_with_validation(pool: PgPool) {
data: update_data,
};
let response = put_table_data(&pool, put_request, &indexer_tx).await.unwrap();
assert!(response.success);
// This should fail because script would change total value
let result = put_table_data(&pool, put_request, &indexer_tx).await;
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.code(), tonic::Code::FailedPrecondition);
assert!(error.message().contains("Script for column 'total' was triggered"));
assert!(error.message().contains("from '10.00' to '50.00'"));
// Now, test a partial update that SHOULD fail validation.
// We update quantity and provide an incorrect total.
@@ -463,7 +466,8 @@ async fn test_put_partial_update_with_validation(pool: PgPool) {
assert!(result.is_err());
let error = result.unwrap_err();
assert_eq!(error.code(), tonic::Code::InvalidArgument);
assert!(error.message().contains("Script calculated '30'"));
assert!(error.message().contains("Script calculated '30.00'"));
assert!(error.message().contains("but user provided '99.99'"));
}
#[sqlx::test]
@@ -520,7 +524,7 @@ async fn test_put_steel_script_error_handling(pool: PgPool) {
let script_request = PostTableScriptRequest {
table_definition_id: table_def_id,
target_column: "total".to_string(),
script: "(/ $price 0.0)".to_string(),
script: "(/ @price 0.0)".to_string(),
description: "Error test".to_string(),
};
post_table_script(&pool, script_request).await.unwrap();