Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d1ebe4732f | ||
|
|
7b7f3ca05a | ||
|
|
234613f831 | ||
|
|
f6d84e70cc | ||
|
|
5cd324b6ae | ||
|
|
a7457f5749 | ||
|
|
a5afc75099 |
45
Cargo.lock
generated
45
Cargo.lock
generated
@@ -163,6 +163,28 @@ dependencies = [
|
||||
"abi_stable",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-stream"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b5a71a6f37880a80d1d7f19efd781e4b5de42c88f0722cc13bcb6cc2cfe8476"
|
||||
dependencies = [
|
||||
"async-stream-impl",
|
||||
"futures-core",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-stream-impl"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7c24de15d275a1ecfd47a380fb4d5ec9bfe0933f309ed5e705b775596a3574d"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.100",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "async-trait"
|
||||
version = "0.1.88"
|
||||
@@ -527,14 +549,17 @@ dependencies = [
|
||||
"crossterm",
|
||||
"dirs 6.0.0",
|
||||
"dotenvy",
|
||||
"futures",
|
||||
"lazy_static",
|
||||
"prost",
|
||||
"prost-types",
|
||||
"ratatui",
|
||||
"rstest",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"time",
|
||||
"tokio",
|
||||
"tokio-test",
|
||||
"toml",
|
||||
"tonic",
|
||||
"tracing",
|
||||
@@ -542,6 +567,7 @@ dependencies = [
|
||||
"tui-textarea",
|
||||
"unicode-segmentation",
|
||||
"unicode-width 0.2.0",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -3949,6 +3975,19 @@ dependencies = [
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-test"
|
||||
version = "0.4.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2468baabc3311435b55dd935f702f42cd1b8abb7e754fb7dfb16bd36aa88f9f7"
|
||||
dependencies = [
|
||||
"async-stream",
|
||||
"bytes",
|
||||
"futures-core",
|
||||
"tokio",
|
||||
"tokio-stream",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio-util"
|
||||
version = "0.7.14"
|
||||
@@ -4279,12 +4318,14 @@ checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "1.16.0"
|
||||
version = "1.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "458f7a779bf54acc9f347480ac654f68407d3aab21269a6e3c9f922acd9e2da9"
|
||||
checksum = "3cf4199d1e5d15ddd86a694e4d0dffa9c323ce759fea589f00fef9d81cc1931d"
|
||||
dependencies = [
|
||||
"getrandom 0.3.2",
|
||||
"js-sys",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -31,3 +31,9 @@ unicode-width = "0.2.0"
|
||||
[features]
|
||||
default = []
|
||||
ui-debug = []
|
||||
|
||||
[dev-dependencies]
|
||||
rstest = "0.25.0"
|
||||
tokio-test = "0.4.4"
|
||||
uuid = { version = "1.17.0", features = ["v4"] }
|
||||
futures = "0.3.31"
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
// src/components/form/form.rs
|
||||
use crate::components::common::autocomplete; // <--- ADD THIS IMPORT
|
||||
use crate::components::common::autocomplete;
|
||||
use crate::components::handlers::canvas::render_canvas;
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::pages::form::FormState; // <--- CHANGE THIS IMPORT
|
||||
use crate::state::pages::form::FormState;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
|
||||
style::Style,
|
||||
|
||||
@@ -14,7 +14,10 @@ use common::proto::multieko2::table_script::{
|
||||
use common::proto::multieko2::tables_data::{
|
||||
tables_data_client::TablesDataClient,
|
||||
GetTableDataByPositionRequest,
|
||||
GetTableDataRequest, // ADD THIS
|
||||
GetTableDataResponse,
|
||||
DeleteTableDataRequest, // ADD THIS
|
||||
DeleteTableDataResponse, // ADD THIS
|
||||
GetTableDataCountRequest,
|
||||
PostTableDataRequest, PostTableDataResponse, PutTableDataRequest,
|
||||
PutTableDataResponse,
|
||||
@@ -116,7 +119,7 @@ impl GrpcClient {
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
// NEW Methods for TablesData service
|
||||
// Existing TablesData methods
|
||||
pub async fn get_table_data_count(
|
||||
&mut self,
|
||||
profile_name: String,
|
||||
@@ -135,7 +138,7 @@ impl GrpcClient {
|
||||
Ok(response.into_inner().count as u64)
|
||||
}
|
||||
|
||||
pub async fn get_table_data_by_position(
|
||||
pub async fn get_table_data_by_position(
|
||||
&mut self,
|
||||
profile_name: String,
|
||||
table_name: String,
|
||||
@@ -155,18 +158,58 @@ pub async fn get_table_data_by_position(
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
// ADD THIS: Missing get_table_data method
|
||||
pub async fn get_table_data(
|
||||
&mut self,
|
||||
profile_name: String,
|
||||
table_name: String,
|
||||
id: i64,
|
||||
) -> Result<GetTableDataResponse> {
|
||||
let grpc_request = GetTableDataRequest {
|
||||
profile_name,
|
||||
table_name,
|
||||
id,
|
||||
};
|
||||
let request = tonic::Request::new(grpc_request);
|
||||
let response = self
|
||||
.tables_data_client
|
||||
.get_table_data(request)
|
||||
.await
|
||||
.context("gRPC GetTableData call failed")?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
// ADD THIS: Missing delete_table_data method
|
||||
pub async fn delete_table_data(
|
||||
&mut self,
|
||||
profile_name: String,
|
||||
table_name: String,
|
||||
record_id: i64,
|
||||
) -> Result<DeleteTableDataResponse> {
|
||||
let grpc_request = DeleteTableDataRequest {
|
||||
profile_name,
|
||||
table_name,
|
||||
record_id,
|
||||
};
|
||||
let request = tonic::Request::new(grpc_request);
|
||||
let response = self
|
||||
.tables_data_client
|
||||
.delete_table_data(request)
|
||||
.await
|
||||
.context("gRPC DeleteTableData call failed")?;
|
||||
Ok(response.into_inner())
|
||||
}
|
||||
|
||||
pub async fn post_table_data(
|
||||
&mut self,
|
||||
profile_name: String,
|
||||
table_name: String,
|
||||
// CHANGE THIS: Accept the pre-converted data
|
||||
data: HashMap<String, Value>,
|
||||
) -> Result<PostTableDataResponse> {
|
||||
// The conversion logic is now gone from here.
|
||||
let grpc_request = PostTableDataRequest {
|
||||
profile_name,
|
||||
table_name,
|
||||
data, // This is now the correct type
|
||||
data,
|
||||
};
|
||||
let request = tonic::Request::new(grpc_request);
|
||||
let response = self
|
||||
@@ -182,15 +225,13 @@ pub async fn get_table_data_by_position(
|
||||
profile_name: String,
|
||||
table_name: String,
|
||||
id: i64,
|
||||
// CHANGE THIS: Accept the pre-converted data
|
||||
data: HashMap<String, Value>,
|
||||
) -> Result<PutTableDataResponse> {
|
||||
// The conversion logic is now gone from here.
|
||||
let grpc_request = PutTableDataRequest {
|
||||
profile_name,
|
||||
table_name,
|
||||
id,
|
||||
data, // This is now the correct type
|
||||
data,
|
||||
};
|
||||
let request = tonic::Request::new(grpc_request);
|
||||
let response = self
|
||||
|
||||
262
client/tests/form/gui/form_tests.rs
Normal file
262
client/tests/form/gui/form_tests.rs
Normal file
@@ -0,0 +1,262 @@
|
||||
// client/tests/form_tests.rs
|
||||
use rstest::{fixture, rstest};
|
||||
use std::collections::HashMap;
|
||||
use client::state::pages::form::{FormState, FieldDefinition};
|
||||
use client::state::pages::canvas_state::CanvasState;
|
||||
|
||||
#[fixture]
|
||||
fn test_form_state() -> FormState {
|
||||
let fields = vec![
|
||||
FieldDefinition {
|
||||
display_name: "Company".to_string(),
|
||||
data_key: "firma".to_string(),
|
||||
is_link: false,
|
||||
link_target_table: None,
|
||||
},
|
||||
FieldDefinition {
|
||||
display_name: "Phone".to_string(),
|
||||
data_key: "telefon".to_string(),
|
||||
is_link: false,
|
||||
link_target_table: None,
|
||||
},
|
||||
FieldDefinition {
|
||||
display_name: "Email".to_string(),
|
||||
data_key: "email".to_string(),
|
||||
is_link: false,
|
||||
link_target_table: None,
|
||||
},
|
||||
];
|
||||
|
||||
FormState::new("test_profile".to_string(), "test_table".to_string(), fields)
|
||||
}
|
||||
|
||||
#[fixture]
|
||||
fn test_form_data() -> HashMap<String, String> {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), "Test Company".to_string());
|
||||
data.insert("telefon".to_string(), "+421123456789".to_string());
|
||||
data.insert("email".to_string(), "test@example.com".to_string());
|
||||
data
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_state_creation(test_form_state: FormState) {
|
||||
assert_eq!(test_form_state.profile_name, "test_profile");
|
||||
assert_eq!(test_form_state.table_name, "test_table");
|
||||
assert_eq!(test_form_state.fields.len(), 3);
|
||||
assert_eq!(test_form_state.current_field(), 0);
|
||||
assert!(!test_form_state.has_unsaved_changes());
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_field_navigation(mut test_form_state: FormState) {
|
||||
// Test initial field
|
||||
assert_eq!(test_form_state.current_field(), 0);
|
||||
|
||||
// Test navigation to next field
|
||||
test_form_state.set_current_field(1);
|
||||
assert_eq!(test_form_state.current_field(), 1);
|
||||
|
||||
// Test navigation to last field
|
||||
test_form_state.set_current_field(2);
|
||||
assert_eq!(test_form_state.current_field(), 2);
|
||||
|
||||
// Test invalid field (should not crash)
|
||||
test_form_state.set_current_field(999);
|
||||
assert_eq!(test_form_state.current_field(), 2); // Should stay at valid field
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_data_entry(mut test_form_state: FormState) {
|
||||
// Test entering data in first field
|
||||
*test_form_state.get_current_input_mut() = "Test Company".to_string();
|
||||
test_form_state.set_has_unsaved_changes(true);
|
||||
|
||||
assert_eq!(test_form_state.get_current_input(), "Test Company");
|
||||
assert!(test_form_state.has_unsaved_changes());
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_field_switching_with_data(mut test_form_state: FormState) {
|
||||
// Enter data in first field
|
||||
*test_form_state.get_current_input_mut() = "Company Name".to_string();
|
||||
|
||||
// Switch to second field
|
||||
test_form_state.set_current_field(1);
|
||||
*test_form_state.get_current_input_mut() = "+421123456789".to_string();
|
||||
|
||||
// Switch back to first field
|
||||
test_form_state.set_current_field(0);
|
||||
assert_eq!(test_form_state.get_current_input(), "Company Name");
|
||||
|
||||
// Switch to second field again
|
||||
test_form_state.set_current_field(1);
|
||||
assert_eq!(test_form_state.get_current_input(), "+421123456789");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_reset_functionality(mut test_form_state: FormState) {
|
||||
// Add some data
|
||||
test_form_state.set_current_field(0);
|
||||
*test_form_state.get_current_input_mut() = "Test Company".to_string();
|
||||
test_form_state.set_current_field(1);
|
||||
*test_form_state.get_current_input_mut() = "+421123456789".to_string();
|
||||
test_form_state.set_has_unsaved_changes(true);
|
||||
test_form_state.id = 123;
|
||||
test_form_state.current_position = 5;
|
||||
|
||||
// Reset the form
|
||||
test_form_state.reset_to_empty();
|
||||
|
||||
// Verify reset
|
||||
assert_eq!(test_form_state.id, 0);
|
||||
assert!(!test_form_state.has_unsaved_changes());
|
||||
assert_eq!(test_form_state.current_field(), 0);
|
||||
|
||||
// Check all fields are empty
|
||||
for i in 0..test_form_state.fields.len() {
|
||||
test_form_state.set_current_field(i);
|
||||
assert!(test_form_state.get_current_input().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_update_from_response(mut test_form_state: FormState, test_form_data: HashMap<String, String>) {
|
||||
let position = 3;
|
||||
|
||||
// Update form with response data
|
||||
test_form_state.update_from_response(&test_form_data, position);
|
||||
|
||||
// Verify data was loaded
|
||||
assert_eq!(test_form_state.current_position, position);
|
||||
assert!(!test_form_state.has_unsaved_changes());
|
||||
assert_eq!(test_form_state.current_field(), 0);
|
||||
|
||||
// Check field values
|
||||
test_form_state.set_current_field(0);
|
||||
assert_eq!(test_form_state.get_current_input(), "Test Company");
|
||||
|
||||
test_form_state.set_current_field(1);
|
||||
assert_eq!(test_form_state.get_current_input(), "+421123456789");
|
||||
|
||||
test_form_state.set_current_field(2);
|
||||
assert_eq!(test_form_state.get_current_input(), "test@example.com");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_cursor_position(mut test_form_state: FormState) {
|
||||
// Test initial cursor position
|
||||
assert_eq!(test_form_state.current_cursor_pos(), 0);
|
||||
|
||||
// Add some text
|
||||
*test_form_state.get_current_input_mut() = "Test Company".to_string();
|
||||
|
||||
// Test cursor positioning
|
||||
test_form_state.set_current_cursor_pos(5);
|
||||
assert_eq!(test_form_state.current_cursor_pos(), 5);
|
||||
|
||||
// Test cursor bounds
|
||||
test_form_state.set_current_cursor_pos(999);
|
||||
// Should be clamped to text length
|
||||
assert!(test_form_state.current_cursor_pos() <= "Test Company".len());
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_field_display_names(test_form_state: FormState) {
|
||||
let field_names = test_form_state.fields();
|
||||
|
||||
assert_eq!(field_names.len(), 3);
|
||||
assert_eq!(field_names[0], "Company");
|
||||
assert_eq!(field_names[1], "Phone");
|
||||
assert_eq!(field_names[2], "Email");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_inputs_vector(mut test_form_state: FormState) {
|
||||
// Add data to fields
|
||||
test_form_state.set_current_field(0);
|
||||
*test_form_state.get_current_input_mut() = "Company A".to_string();
|
||||
|
||||
test_form_state.set_current_field(1);
|
||||
*test_form_state.get_current_input_mut() = "123456789".to_string();
|
||||
|
||||
test_form_state.set_current_field(2);
|
||||
*test_form_state.get_current_input_mut() = "test@test.com".to_string();
|
||||
|
||||
// Get inputs vector
|
||||
let inputs = test_form_state.inputs();
|
||||
|
||||
assert_eq!(inputs.len(), 3);
|
||||
assert_eq!(inputs[0], "Company A");
|
||||
assert_eq!(inputs[1], "123456789");
|
||||
assert_eq!(inputs[2], "test@test.com");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_position_management(mut test_form_state: FormState) {
|
||||
// Test initial position
|
||||
assert_eq!(test_form_state.current_position, 1);
|
||||
assert_eq!(test_form_state.total_count, 0);
|
||||
|
||||
// Set some values
|
||||
test_form_state.total_count = 10;
|
||||
test_form_state.current_position = 5;
|
||||
|
||||
assert_eq!(test_form_state.current_position, 5);
|
||||
assert_eq!(test_form_state.total_count, 10);
|
||||
|
||||
// Test reset affects position
|
||||
test_form_state.reset_to_empty();
|
||||
assert_eq!(test_form_state.current_position, 11); // total_count + 1
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_autocomplete_state(mut test_form_state: FormState) {
|
||||
// Test initial autocomplete state
|
||||
assert!(!test_form_state.autocomplete_active);
|
||||
assert!(test_form_state.autocomplete_suggestions.is_empty());
|
||||
assert!(test_form_state.selected_suggestion_index.is_none());
|
||||
|
||||
// Test deactivating autocomplete
|
||||
test_form_state.autocomplete_active = true;
|
||||
test_form_state.deactivate_autocomplete();
|
||||
|
||||
assert!(!test_form_state.autocomplete_active);
|
||||
assert!(test_form_state.autocomplete_suggestions.is_empty());
|
||||
assert!(test_form_state.selected_suggestion_index.is_none());
|
||||
assert!(!test_form_state.autocomplete_loading);
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_empty_data_handling(mut test_form_state: FormState) {
|
||||
let empty_data = HashMap::new();
|
||||
|
||||
// Update with empty data
|
||||
test_form_state.update_from_response(&empty_data, 1);
|
||||
|
||||
// All fields should be empty
|
||||
for i in 0..test_form_state.fields.len() {
|
||||
test_form_state.set_current_field(i);
|
||||
assert!(test_form_state.get_current_input().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
fn test_form_partial_data_handling(mut test_form_state: FormState) {
|
||||
let mut partial_data = HashMap::new();
|
||||
partial_data.insert("firma".to_string(), "Partial Company".to_string());
|
||||
// Intentionally missing telefon and email
|
||||
|
||||
test_form_state.update_from_response(&partial_data, 1);
|
||||
|
||||
// First field should have data
|
||||
test_form_state.set_current_field(0);
|
||||
assert_eq!(test_form_state.get_current_input(), "Partial Company");
|
||||
|
||||
// Other fields should be empty
|
||||
test_form_state.set_current_field(1);
|
||||
assert!(test_form_state.get_current_input().is_empty());
|
||||
|
||||
test_form_state.set_current_field(2);
|
||||
assert!(test_form_state.get_current_input().is_empty());
|
||||
}
|
||||
1
client/tests/form/gui/mod.rs
Normal file
1
client/tests/form/gui/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod form_tests;
|
||||
2
client/tests/form/mod.rs
Normal file
2
client/tests/form/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
pub mod gui;
|
||||
pub mod requests;
|
||||
1019
client/tests/form/requests/form_request_tests.rs
Normal file
1019
client/tests/form/requests/form_request_tests.rs
Normal file
File diff suppressed because it is too large
Load Diff
267
client/tests/form/requests/form_request_tests2.rs
Normal file
267
client/tests/form/requests/form_request_tests2.rs
Normal file
@@ -0,0 +1,267 @@
|
||||
// ========================================================================
|
||||
// ROBUST WORKFLOW AND INTEGRATION TESTS
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_partial_update_preserves_other_fields(
|
||||
#[future] populated_test_context: FormTestContext,
|
||||
) {
|
||||
let mut context = populated_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Create a record with multiple fields
|
||||
let mut initial_data = context.create_test_form_data();
|
||||
let original_email = "preserve.this@email.com";
|
||||
initial_data.insert(
|
||||
"email".to_string(),
|
||||
create_string_value(original_email),
|
||||
);
|
||||
|
||||
let post_res = context
|
||||
.client
|
||||
.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
initial_data,
|
||||
)
|
||||
.await
|
||||
.expect("Setup: Failed to create record for partial update test");
|
||||
let created_id = post_res.inserted_id;
|
||||
println!("Partial Update Test: Created record ID {}", created_id);
|
||||
|
||||
// 2. Update only ONE field
|
||||
let mut partial_update = HashMap::new();
|
||||
let updated_firma = "Partially Updated Inc.";
|
||||
partial_update.insert(
|
||||
"firma".to_string(),
|
||||
create_string_value(updated_firma),
|
||||
);
|
||||
|
||||
context
|
||||
.client
|
||||
.put_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
created_id,
|
||||
partial_update,
|
||||
)
|
||||
.await
|
||||
.expect("Partial update failed");
|
||||
println!("Partial Update Test: Updated only 'firma' field");
|
||||
|
||||
// 3. Get the record back and verify ALL fields
|
||||
let get_res = context
|
||||
.client
|
||||
.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
created_id,
|
||||
)
|
||||
.await
|
||||
.expect("Failed to get record after partial update");
|
||||
|
||||
let final_data = get_res.data;
|
||||
assert_eq!(
|
||||
final_data.get("firma").unwrap(),
|
||||
updated_firma,
|
||||
"The 'firma' field should be updated"
|
||||
);
|
||||
assert_eq!(
|
||||
final_data.get("email").unwrap(),
|
||||
original_email,
|
||||
"The 'email' field should have been preserved"
|
||||
);
|
||||
println!("Partial Update Test: Verified other fields were preserved. OK.");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_data_edge_cases_and_unicode(
|
||||
#[future] form_test_context: FormTestContext,
|
||||
) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
let edge_case_strings = vec![
|
||||
("Unicode", "José María González, Москва, 北京市"),
|
||||
("Emoji", "🚀 Tech Company 🌟"),
|
||||
("Quotes", "Quote\"Test'Apostrophe"),
|
||||
("Symbols", "Price: $1,000.50 (50% off!)"),
|
||||
("Empty", ""),
|
||||
("Whitespace", " "),
|
||||
];
|
||||
|
||||
for (case_name, test_string) in edge_case_strings {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value(test_string));
|
||||
data.insert(
|
||||
"kz".to_string(),
|
||||
create_string_value(&format!("EDGE-{}", case_name)),
|
||||
);
|
||||
|
||||
let post_res = context
|
||||
.client
|
||||
.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
)
|
||||
.await
|
||||
.expect(&format!("POST should succeed for case: {}", case_name));
|
||||
let created_id = post_res.inserted_id;
|
||||
|
||||
let get_res = context
|
||||
.client
|
||||
.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
created_id,
|
||||
)
|
||||
.await
|
||||
.expect(&format!(
|
||||
"GET should succeed for case: {}",
|
||||
case_name
|
||||
));
|
||||
|
||||
assert_eq!(
|
||||
get_res.data.get("firma").unwrap(),
|
||||
test_string,
|
||||
"Data should be identical after round-trip for case: {}",
|
||||
case_name
|
||||
);
|
||||
println!("Edge Case Test: '{}' passed.", case_name);
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_numeric_and_null_edge_cases(
|
||||
#[future] form_test_context: FormTestContext,
|
||||
) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Test NULL value
|
||||
let mut null_data = HashMap::new();
|
||||
null_data.insert(
|
||||
"firma".to_string(),
|
||||
create_string_value("Company With Null Phone"),
|
||||
);
|
||||
null_data.insert("telefon".to_string(), create_null_value());
|
||||
let post_res_null = context
|
||||
.client
|
||||
.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
null_data,
|
||||
)
|
||||
.await
|
||||
.expect("POST with NULL value should succeed");
|
||||
let get_res_null = context
|
||||
.client
|
||||
.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
post_res_null.inserted_id,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
// Depending on DB, NULL may come back as empty string or be absent.
|
||||
// The important part is that the operation doesn't fail.
|
||||
assert!(
|
||||
get_res_null.data.get("telefon").unwrap_or(&"".to_string()).is_empty(),
|
||||
"NULL value should result in an empty or absent field"
|
||||
);
|
||||
println!("Edge Case Test: NULL value handled correctly. OK.");
|
||||
|
||||
// 2. Test Zero value for a numeric field (assuming 'age' is numeric)
|
||||
let mut zero_data = HashMap::new();
|
||||
zero_data.insert(
|
||||
"firma".to_string(),
|
||||
create_string_value("Newborn Company"),
|
||||
);
|
||||
// Assuming 'age' is a field in your actual table definition
|
||||
// zero_data.insert("age".to_string(), create_number_value(0.0));
|
||||
// let post_res_zero = context.client.post_table_data(...).await.expect("POST with zero should succeed");
|
||||
// ... then get and verify it's "0"
|
||||
println!("Edge Case Test: Zero value test skipped (uncomment if 'age' field exists).");
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_concurrent_updates_on_same_record(
|
||||
#[future] populated_test_context: FormTestContext,
|
||||
) {
|
||||
let mut context = populated_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Create a single record to be updated by all tasks
|
||||
let initial_data = context.create_minimal_form_data();
|
||||
let post_res = context
|
||||
.client
|
||||
.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
initial_data,
|
||||
)
|
||||
.await
|
||||
.expect("Setup: Failed to create record for concurrency test");
|
||||
let record_id = post_res.inserted_id;
|
||||
println!("Concurrency Test: Target record ID is {}", record_id);
|
||||
|
||||
// 2. Spawn multiple concurrent UPDATE operations
|
||||
let mut handles = Vec::new();
|
||||
let num_concurrent_tasks = 5;
|
||||
let mut final_values = Vec::new();
|
||||
|
||||
for i in 0..num_concurrent_tasks {
|
||||
let mut client_clone = context.client.clone();
|
||||
let profile_name = context.profile_name.clone();
|
||||
let table_name = context.table_name.clone();
|
||||
let final_value = format!("Concurrent Update {}", i);
|
||||
final_values.push(final_value.clone());
|
||||
|
||||
let handle = tokio::spawn(async move {
|
||||
let mut update_data = HashMap::new();
|
||||
update_data.insert(
|
||||
"firma".to_string(),
|
||||
create_string_value(&final_value),
|
||||
);
|
||||
client_clone
|
||||
.put_table_data(profile_name, table_name, record_id, update_data)
|
||||
.await
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
// 3. Wait for all tasks to complete and check for panics
|
||||
let results = futures::future::join_all(handles).await;
|
||||
assert!(
|
||||
results.iter().all(|r| r.is_ok()),
|
||||
"No concurrent task should panic"
|
||||
);
|
||||
println!("Concurrency Test: All update tasks completed without panicking.");
|
||||
|
||||
// 4. Get the final state of the record
|
||||
let final_get_res = context
|
||||
.client
|
||||
.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
)
|
||||
.await
|
||||
.expect("Should be able to get the record after concurrent updates");
|
||||
|
||||
let final_firma = final_get_res.data.get("firma").unwrap();
|
||||
assert!(
|
||||
final_values.contains(final_firma),
|
||||
"The final state '{}' must be one of the states set by the tasks",
|
||||
final_firma
|
||||
);
|
||||
println!(
|
||||
"Concurrency Test: Final state is '{}', which is a valid outcome. OK.",
|
||||
final_firma
|
||||
);
|
||||
}
|
||||
727
client/tests/form/requests/form_request_tests3.rs
Normal file
727
client/tests/form/requests/form_request_tests3.rs
Normal file
@@ -0,0 +1,727 @@
|
||||
// form_request_tests3.rs - Comprehensive and Robust Testing
|
||||
|
||||
// ========================================================================
|
||||
// STEEL SCRIPT VALIDATION TESTS (HIGHEST PRIORITY)
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_steel_script_validation_success(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test with data that should pass script validation
|
||||
// Assuming there's a script that validates 'kz' field to start with "KZ" and be 5 chars
|
||||
let mut valid_data = HashMap::new();
|
||||
valid_data.insert("firma".to_string(), create_string_value("Script Test Company"));
|
||||
valid_data.insert("kz".to_string(), create_string_value("KZ123"));
|
||||
valid_data.insert("telefon".to_string(), create_string_value("+421123456789"));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
valid_data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(response) => {
|
||||
assert!(response.success, "Valid data should pass script validation");
|
||||
println!("Script Validation Test: Valid data passed - ID {}", response.inserted_id);
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(status) = e.downcast_ref::<Status>() {
|
||||
if status.code() == tonic::Code::Unavailable {
|
||||
println!("Script validation test skipped - backend not available");
|
||||
return;
|
||||
}
|
||||
// If there are no scripts configured, this might still work
|
||||
println!("Script validation test: {}", status.message());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_steel_script_validation_failure(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test with data that should fail script validation
|
||||
let invalid_script_data = vec![
|
||||
("TooShort", "KZ12"), // Too short
|
||||
("TooLong", "KZ12345"), // Too long
|
||||
("WrongPrefix", "AB123"), // Wrong prefix
|
||||
("NoPrefix", "12345"), // No prefix
|
||||
("Empty", ""), // Empty
|
||||
];
|
||||
|
||||
for (test_case, invalid_kz) in invalid_script_data {
|
||||
let mut invalid_data = HashMap::new();
|
||||
invalid_data.insert("firma".to_string(), create_string_value("Script Fail Company"));
|
||||
invalid_data.insert("kz".to_string(), create_string_value(invalid_kz));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
invalid_data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(_) => {
|
||||
println!("Script Validation Test: {} passed (no validation script configured)", test_case);
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(status) = e.downcast_ref::<Status>() {
|
||||
assert_eq!(status.code(), tonic::Code::InvalidArgument,
|
||||
"Script validation failure should return InvalidArgument for case: {}", test_case);
|
||||
println!("Script Validation Test: {} correctly failed - {}", test_case, status.message());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_steel_script_validation_on_update(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Create a valid record first
|
||||
let mut initial_data = HashMap::new();
|
||||
initial_data.insert("firma".to_string(), create_string_value("Update Script Test"));
|
||||
initial_data.insert("kz".to_string(), create_string_value("KZ123"));
|
||||
|
||||
let post_result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
initial_data,
|
||||
).await;
|
||||
|
||||
if let Ok(post_response) = post_result {
|
||||
let record_id = post_response.inserted_id;
|
||||
|
||||
// 2. Try to update with invalid data
|
||||
let mut invalid_update = HashMap::new();
|
||||
invalid_update.insert("kz".to_string(), create_string_value("INVALID"));
|
||||
|
||||
let update_result = context.client.put_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
invalid_update,
|
||||
).await;
|
||||
|
||||
match update_result {
|
||||
Ok(_) => {
|
||||
println!("Script Validation on Update: No validation script configured for updates");
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(status) = e.downcast_ref::<Status>() {
|
||||
assert_eq!(status.code(), tonic::Code::InvalidArgument,
|
||||
"Update with invalid data should fail script validation");
|
||||
println!("Script Validation on Update: Correctly rejected invalid update");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// COMPREHENSIVE DATA TYPE TESTS
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_boolean_data_type(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test valid boolean values
|
||||
let boolean_test_cases = vec![
|
||||
("true", true),
|
||||
("false", false),
|
||||
];
|
||||
|
||||
for (case_name, bool_value) in boolean_test_cases {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value("Boolean Test Company"));
|
||||
// Assuming there's a boolean field called 'active'
|
||||
data.insert("active".to_string(), create_bool_value(bool_value));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(response) => {
|
||||
println!("Boolean Test: {} value succeeded", case_name);
|
||||
|
||||
// Verify the value round-trip
|
||||
if let Ok(get_response) = context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
response.inserted_id,
|
||||
).await {
|
||||
if let Some(retrieved_value) = get_response.data.get("active") {
|
||||
println!("Boolean Test: {} round-trip value: {}", case_name, retrieved_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Boolean Test: {} failed (field may not exist): {}", case_name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_numeric_data_types(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test various numeric values
|
||||
let numeric_test_cases = vec![
|
||||
("Zero", 0.0),
|
||||
("Positive", 123.45),
|
||||
("Negative", -67.89),
|
||||
("Large", 999999.99),
|
||||
("SmallDecimal", 0.01),
|
||||
];
|
||||
|
||||
for (case_name, numeric_value) in numeric_test_cases {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value("Numeric Test Company"));
|
||||
// Assuming there's a numeric field called 'price' or 'amount'
|
||||
data.insert("amount".to_string(), create_number_value(numeric_value));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(response) => {
|
||||
println!("Numeric Test: {} ({}) succeeded", case_name, numeric_value);
|
||||
|
||||
// Verify round-trip
|
||||
if let Ok(get_response) = context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
response.inserted_id,
|
||||
).await {
|
||||
if let Some(retrieved_value) = get_response.data.get("amount") {
|
||||
println!("Numeric Test: {} round-trip value: {}", case_name, retrieved_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Numeric Test: {} failed (field may not exist): {}", case_name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_timestamp_data_type(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test various timestamp formats
|
||||
let timestamp_test_cases = vec![
|
||||
("ISO8601", "2024-01-15T10:30:00Z"),
|
||||
("WithTimezone", "2024-01-15T10:30:00+01:00"),
|
||||
("WithMilliseconds", "2024-01-15T10:30:00.123Z"),
|
||||
];
|
||||
|
||||
for (case_name, timestamp_str) in timestamp_test_cases {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value("Timestamp Test Company"));
|
||||
// Assuming there's a timestamp field called 'created_at'
|
||||
data.insert("created_at".to_string(), create_string_value(timestamp_str));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(response) => {
|
||||
println!("Timestamp Test: {} succeeded", case_name);
|
||||
|
||||
// Verify round-trip
|
||||
if let Ok(get_response) = context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
response.inserted_id,
|
||||
).await {
|
||||
if let Some(retrieved_value) = get_response.data.get("created_at") {
|
||||
println!("Timestamp Test: {} round-trip value: {}", case_name, retrieved_value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Timestamp Test: {} failed (field may not exist): {}", case_name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_invalid_data_types(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test invalid data type combinations
|
||||
let invalid_type_cases = vec![
|
||||
("StringForNumber", "amount", create_string_value("not-a-number")),
|
||||
("NumberForBoolean", "active", create_number_value(123.0)),
|
||||
("StringForBoolean", "active", create_string_value("maybe")),
|
||||
("InvalidTimestamp", "created_at", create_string_value("not-a-date")),
|
||||
];
|
||||
|
||||
for (case_name, field_name, invalid_value) in invalid_type_cases {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value("Invalid Type Test"));
|
||||
data.insert(field_name.to_string(), invalid_value);
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(_) => {
|
||||
println!("Invalid Type Test: {} passed (no type validation or field doesn't exist)", case_name);
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(status) = e.downcast_ref::<Status>() {
|
||||
assert_eq!(status.code(), tonic::Code::InvalidArgument,
|
||||
"Invalid data type should return InvalidArgument for case: {}", case_name);
|
||||
println!("Invalid Type Test: {} correctly rejected - {}", case_name, status.message());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// FOREIGN KEY RELATIONSHIP TESTS
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_foreign_key_valid_relationship(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Create a parent record first (e.g., company)
|
||||
let mut parent_data = HashMap::new();
|
||||
parent_data.insert("firma".to_string(), create_string_value("Parent Company"));
|
||||
|
||||
let parent_result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
"companies".to_string(), // Assuming companies table exists
|
||||
parent_data,
|
||||
).await;
|
||||
|
||||
if let Ok(parent_response) = parent_result {
|
||||
let parent_id = parent_response.inserted_id;
|
||||
|
||||
// 2. Create a child record that references the parent
|
||||
let mut child_data = HashMap::new();
|
||||
child_data.insert("name".to_string(), create_string_value("Child Record"));
|
||||
child_data.insert("company_id".to_string(), create_number_value(parent_id as f64));
|
||||
|
||||
let child_result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
"contacts".to_string(), // Assuming contacts table exists
|
||||
child_data,
|
||||
).await;
|
||||
|
||||
match child_result {
|
||||
Ok(child_response) => {
|
||||
assert!(child_response.success, "Valid foreign key relationship should succeed");
|
||||
println!("Foreign Key Test: Valid relationship created - Parent ID: {}, Child ID: {}",
|
||||
parent_id, child_response.inserted_id);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Foreign Key Test: Failed (tables may not exist or no FK constraint): {}", e);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
println!("Foreign Key Test: Could not create parent record");
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_foreign_key_invalid_relationship(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Try to create a child record with non-existent parent ID
|
||||
let mut invalid_child_data = HashMap::new();
|
||||
invalid_child_data.insert("name".to_string(), create_string_value("Orphan Record"));
|
||||
invalid_child_data.insert("company_id".to_string(), create_number_value(99999.0)); // Non-existent ID
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
"contacts".to_string(),
|
||||
invalid_child_data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(_) => {
|
||||
println!("Foreign Key Test: Invalid relationship passed (no FK constraint configured)");
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(status) = e.downcast_ref::<Status>() {
|
||||
// Could be InvalidArgument or NotFound depending on implementation
|
||||
assert!(matches!(status.code(), tonic::Code::InvalidArgument | tonic::Code::NotFound),
|
||||
"Invalid foreign key should return InvalidArgument or NotFound");
|
||||
println!("Foreign Key Test: Invalid relationship correctly rejected - {}", status.message());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// DELETED RECORD INTERACTION TESTS
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_update_deleted_record_behavior(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Create a record
|
||||
let initial_data = context.create_test_form_data();
|
||||
let post_result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
initial_data,
|
||||
).await;
|
||||
|
||||
if let Ok(post_response) = post_result {
|
||||
let record_id = post_response.inserted_id;
|
||||
println!("Deleted Record Test: Created record ID {}", record_id);
|
||||
|
||||
// 2. Delete the record (soft delete)
|
||||
let delete_result = context.client.delete_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
).await;
|
||||
|
||||
assert!(delete_result.is_ok(), "Delete should succeed");
|
||||
println!("Deleted Record Test: Soft-deleted record {}", record_id);
|
||||
|
||||
// 3. Try to UPDATE the deleted record
|
||||
let mut update_data = HashMap::new();
|
||||
update_data.insert("firma".to_string(), create_string_value("Updated Deleted Record"));
|
||||
|
||||
let update_result = context.client.put_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
update_data,
|
||||
).await;
|
||||
|
||||
match update_result {
|
||||
Ok(_) => {
|
||||
// This might be a bug - updating deleted records should probably fail
|
||||
println!("Deleted Record Test: UPDATE on deleted record succeeded (potential bug?)");
|
||||
|
||||
// Check if the record is still considered deleted
|
||||
let get_result = context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
).await;
|
||||
|
||||
if get_result.is_err() {
|
||||
println!("Deleted Record Test: Record still appears deleted after update");
|
||||
} else {
|
||||
println!("Deleted Record Test: Record appears to be undeleted after update");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if let Some(status) = e.downcast_ref::<Status>() {
|
||||
assert_eq!(status.code(), tonic::Code::NotFound,
|
||||
"UPDATE on deleted record should return NotFound");
|
||||
println!("Deleted Record Test: UPDATE correctly rejected on deleted record");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_delete_already_deleted_record(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// 1. Create and delete a record
|
||||
let initial_data = context.create_test_form_data();
|
||||
let post_result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
initial_data,
|
||||
).await;
|
||||
|
||||
if let Ok(post_response) = post_result {
|
||||
let record_id = post_response.inserted_id;
|
||||
|
||||
// First deletion
|
||||
let delete_result1 = context.client.delete_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
).await;
|
||||
assert!(delete_result1.is_ok(), "First delete should succeed");
|
||||
|
||||
// Second deletion (idempotent)
|
||||
let delete_result2 = context.client.delete_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
).await;
|
||||
|
||||
assert!(delete_result2.is_ok(), "Second delete should succeed (idempotent)");
|
||||
if let Ok(response) = delete_result2 {
|
||||
assert!(response.success, "Delete should report success even for already-deleted record");
|
||||
}
|
||||
println!("Double Delete Test: Both deletions succeeded (idempotent behavior)");
|
||||
}
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// VALIDATION AND BOUNDARY TESTS
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_large_data_handling(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test with very large string values
|
||||
let large_string = "A".repeat(10000); // 10KB string
|
||||
let very_large_string = "B".repeat(100000); // 100KB string
|
||||
|
||||
let test_cases = vec![
|
||||
("Large", large_string),
|
||||
("VeryLarge", very_large_string),
|
||||
];
|
||||
|
||||
for (case_name, large_value) in test_cases {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value(&large_value));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(response) => {
|
||||
println!("Large Data Test: {} string handled successfully", case_name);
|
||||
|
||||
// Verify round-trip
|
||||
if let Ok(get_response) = context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
response.inserted_id,
|
||||
).await {
|
||||
if let Some(retrieved_value) = get_response.data.get("firma") {
|
||||
assert_eq!(retrieved_value.len(), large_value.len(),
|
||||
"Large string should survive round-trip for case: {}", case_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Large Data Test: {} failed (may hit size limits): {}", case_name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_sql_injection_attempts(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test potential SQL injection strings
|
||||
let injection_attempts = vec![
|
||||
("SingleQuote", "'; DROP TABLE users; --"),
|
||||
("DoubleQuote", "\"; DROP TABLE users; --"),
|
||||
("Union", "' UNION SELECT * FROM users --"),
|
||||
("Comment", "/* malicious comment */"),
|
||||
("Semicolon", "; DELETE FROM users;"),
|
||||
];
|
||||
|
||||
for (case_name, injection_string) in injection_attempts {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value(injection_string));
|
||||
data.insert("kz".to_string(), create_string_value("KZ123"));
|
||||
|
||||
let result = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await;
|
||||
|
||||
match result {
|
||||
Ok(response) => {
|
||||
println!("SQL Injection Test: {} handled safely (parameterized queries)", case_name);
|
||||
|
||||
// Verify the malicious string was stored as-is (not executed)
|
||||
if let Ok(get_response) = context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
response.inserted_id,
|
||||
).await {
|
||||
if let Some(retrieved_value) = get_response.data.get("firma") {
|
||||
assert_eq!(retrieved_value, injection_string,
|
||||
"Injection string should be stored literally for case: {}", case_name);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
println!("SQL Injection Test: {} rejected: {}", case_name, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_concurrent_operations_with_same_data(#[future] form_test_context: FormTestContext) {
|
||||
let context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
// Test multiple concurrent operations with identical data
|
||||
let mut handles = Vec::new();
|
||||
let num_tasks = 10;
|
||||
|
||||
for i in 0..num_tasks {
|
||||
let mut context_clone = context.clone();
|
||||
let handle = tokio::spawn(async move {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value("Concurrent Identical"));
|
||||
data.insert("kz".to_string(), create_string_value(&format!("SAME{:02}", i)));
|
||||
|
||||
context_clone.client.post_table_data(
|
||||
context_clone.profile_name,
|
||||
context_clone.table_name,
|
||||
data,
|
||||
).await
|
||||
});
|
||||
handles.push(handle);
|
||||
}
|
||||
|
||||
// Wait for all to complete
|
||||
let mut success_count = 0;
|
||||
let mut inserted_ids = Vec::new();
|
||||
|
||||
for (i, handle) in handles.into_iter().enumerate() {
|
||||
match handle.await {
|
||||
Ok(Ok(response)) => {
|
||||
success_count += 1;
|
||||
inserted_ids.push(response.inserted_id);
|
||||
println!("Concurrent Identical Data: Task {} succeeded with ID {}", i, response.inserted_id);
|
||||
}
|
||||
Ok(Err(e)) => {
|
||||
println!("Concurrent Identical Data: Task {} failed: {}", i, e);
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Concurrent Identical Data: Task {} panicked: {}", i, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert!(success_count > 0, "At least some concurrent operations should succeed");
|
||||
|
||||
// Verify all IDs are unique
|
||||
let unique_ids: std::collections::HashSet<_> = inserted_ids.iter().collect();
|
||||
assert_eq!(unique_ids.len(), inserted_ids.len(), "All inserted IDs should be unique");
|
||||
|
||||
println!("Concurrent Identical Data: {}/{} operations succeeded with unique IDs",
|
||||
success_count, num_tasks);
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// PERFORMANCE AND STRESS TESTS
|
||||
// ========================================================================
|
||||
|
||||
#[rstest]
|
||||
#[tokio::test]
|
||||
async fn test_bulk_operations_performance(#[future] form_test_context: FormTestContext) {
|
||||
let mut context = form_test_context.await;
|
||||
skip_if_backend_unavailable!();
|
||||
|
||||
let operation_count = 50;
|
||||
let start_time = std::time::Instant::now();
|
||||
|
||||
let mut successful_operations = 0;
|
||||
let mut created_ids = Vec::new();
|
||||
|
||||
// Bulk create
|
||||
for i in 0..operation_count {
|
||||
let mut data = HashMap::new();
|
||||
data.insert("firma".to_string(), create_string_value(&format!("Bulk Company {}", i)));
|
||||
data.insert("kz".to_string(), create_string_value(&format!("BLK{:02}", i)));
|
||||
|
||||
if let Ok(response) = context.client.post_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
data,
|
||||
).await {
|
||||
successful_operations += 1;
|
||||
created_ids.push(response.inserted_id);
|
||||
}
|
||||
}
|
||||
|
||||
let create_duration = start_time.elapsed();
|
||||
println!("Bulk Performance: Created {} records in {:?}", successful_operations, create_duration);
|
||||
|
||||
// Bulk read
|
||||
let read_start = std::time::Instant::now();
|
||||
let mut successful_reads = 0;
|
||||
|
||||
for &record_id in &created_ids {
|
||||
if context.client.get_table_data(
|
||||
context.profile_name.clone(),
|
||||
context.table_name.clone(),
|
||||
record_id,
|
||||
).await.is_ok() {
|
||||
successful_reads += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let read_duration = read_start.elapsed();
|
||||
println!("Bulk Performance: Read {} records in {:?}", successful_reads, read_duration);
|
||||
|
||||
// Performance assertions
|
||||
assert!(successful_operations > operation_count * 8 / 10,
|
||||
"At least 80% of operations should succeed");
|
||||
assert!(create_duration.as_secs() < 60,
|
||||
"Bulk operations should complete in reasonable time");
|
||||
|
||||
println!("Bulk Performance Test: {}/{} creates, {}/{} reads successful",
|
||||
successful_operations, operation_count, successful_reads, created_ids.len());
|
||||
}
|
||||
1
client/tests/form/requests/mod.rs
Normal file
1
client/tests/form/requests/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod form_request_tests;
|
||||
3
client/tests/mod.rs
Normal file
3
client/tests/mod.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
// tests/mod.rs
|
||||
|
||||
pub mod form;
|
||||
@@ -33,7 +33,7 @@ validator = { version = "0.20.0", features = ["derive"] }
|
||||
uuid = { version = "1.16.0", features = ["serde", "v4"] }
|
||||
jsonwebtoken = "9.3.1"
|
||||
rust-stemmers = "1.2.0"
|
||||
rust_decimal = "1.37.2"
|
||||
rust_decimal = { version = "1.37.2", features = ["maths", "serde"] }
|
||||
rust_decimal_macros = "1.37.1"
|
||||
|
||||
[lib]
|
||||
|
||||
@@ -33,7 +33,7 @@ pub async fn qualify_table_name(
|
||||
.unwrap_or(false);
|
||||
|
||||
if definition_exists {
|
||||
Ok(format!("{}.\"{}\"", profile_name, table_name))
|
||||
Ok(format!("\"{}\".\"{}\"", profile_name, table_name))
|
||||
} else {
|
||||
// It's not a user-defined table, so it must be a system table in 'public.
|
||||
Ok(format!("\"{}\"", table_name))
|
||||
|
||||
190
server/src/steel/server/decimal_math.rs
Normal file
190
server/src/steel/server/decimal_math.rs
Normal file
@@ -0,0 +1,190 @@
|
||||
// src/steel/server/decimal_math.rs
|
||||
use rust_decimal::prelude::*;
|
||||
use rust_decimal::MathematicalOps;
|
||||
use steel::rvals::SteelVal;
|
||||
use std::str::FromStr;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum DecimalMathError {
|
||||
#[error("Invalid decimal format: {0}")]
|
||||
InvalidDecimal(String),
|
||||
#[error("Math operation failed: {0}")]
|
||||
MathError(String),
|
||||
#[error("Division by zero")]
|
||||
DivisionByZero,
|
||||
}
|
||||
|
||||
/// Converts a SteelVal to a Decimal
|
||||
fn steel_val_to_decimal(val: &SteelVal) -> Result<Decimal, DecimalMathError> {
|
||||
match val {
|
||||
SteelVal::StringV(s) => {
|
||||
Decimal::from_str(&s.to_string())
|
||||
.map_err(|e| DecimalMathError::InvalidDecimal(format!("{}: {}", s, e)))
|
||||
}
|
||||
SteelVal::NumV(n) => {
|
||||
Decimal::try_from(*n)
|
||||
.map_err(|e| DecimalMathError::InvalidDecimal(format!("{}: {}", n, e)))
|
||||
}
|
||||
SteelVal::IntV(i) => {
|
||||
Ok(Decimal::from(*i))
|
||||
}
|
||||
_ => Err(DecimalMathError::InvalidDecimal(format!("Unsupported type: {:?}", val)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts a Decimal back to a SteelVal string
|
||||
fn decimal_to_steel_val(decimal: Decimal) -> SteelVal {
|
||||
SteelVal::StringV(decimal.to_string().into())
|
||||
}
|
||||
|
||||
// Basic arithmetic operations
|
||||
pub fn decimal_add(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok((a_dec + b_dec).to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_sub(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok((a_dec - b_dec).to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_mul(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok((a_dec * b_dec).to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_div(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
|
||||
if b_dec.is_zero() {
|
||||
return Err("Division by zero".to_string());
|
||||
}
|
||||
|
||||
Ok((a_dec / b_dec).to_string())
|
||||
}
|
||||
|
||||
// Advanced mathematical functions (requires maths feature)
|
||||
pub fn decimal_pow(base: String, exp: String) -> Result<String, String> {
|
||||
let base_dec = Decimal::from_str(&base).map_err(|e| format!("Invalid decimal '{}': {}", base, e))?;
|
||||
let exp_dec = Decimal::from_str(&exp).map_err(|e| format!("Invalid decimal '{}': {}", exp, e))?;
|
||||
|
||||
base_dec.checked_powd(exp_dec)
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Power operation failed or overflowed".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_sqrt(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.sqrt()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Square root failed (negative number?)".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_ln(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.checked_ln()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Natural log failed (non-positive number?)".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_log10(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.checked_log10()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Log10 failed (non-positive number?)".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_exp(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.checked_exp()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Exponential failed or overflowed".to_string())
|
||||
}
|
||||
|
||||
// Trigonometric functions (input in radians)
|
||||
pub fn decimal_sin(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.checked_sin()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Sine calculation failed or overflowed".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_cos(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.checked_cos()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Cosine calculation failed or overflowed".to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_tan(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
|
||||
a_dec.checked_tan()
|
||||
.map(|result| result.to_string())
|
||||
.ok_or_else(|| "Tangent calculation failed or overflowed".to_string())
|
||||
}
|
||||
|
||||
// Comparison functions
|
||||
pub fn decimal_gt(a: String, b: String) -> Result<bool, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec > b_dec)
|
||||
}
|
||||
|
||||
pub fn decimal_lt(a: String, b: String) -> Result<bool, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec < b_dec)
|
||||
}
|
||||
|
||||
pub fn decimal_eq(a: String, b: String) -> Result<bool, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec == b_dec)
|
||||
}
|
||||
|
||||
// Utility functions
|
||||
pub fn decimal_abs(a: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
Ok(a_dec.abs().to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_round(a: String, places: i32) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
Ok(a_dec.round_dp(places as u32).to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_min(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec.min(b_dec).to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_max(a: String, b: String) -> Result<String, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec.max(b_dec).to_string())
|
||||
}
|
||||
|
||||
pub fn decimal_gte(a: String, b: String) -> Result<bool, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec >= b_dec)
|
||||
}
|
||||
|
||||
pub fn decimal_lte(a: String, b: String) -> Result<bool, String> {
|
||||
let a_dec = Decimal::from_str(&a).map_err(|e| format!("Invalid decimal '{}': {}", a, e))?;
|
||||
let b_dec = Decimal::from_str(&b).map_err(|e| format!("Invalid decimal '{}': {}", b, e))?;
|
||||
Ok(a_dec <= b_dec)
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
// src/steel/server/execution.rs
|
||||
// Updated src/steel/server/execution.rs
|
||||
use steel::steel_vm::engine::Engine;
|
||||
use steel::steel_vm::register_fn::RegisterFn;
|
||||
use steel::rvals::SteelVal;
|
||||
use super::functions::SteelContext;
|
||||
use super::decimal_math::*;
|
||||
use sqlx::PgPool;
|
||||
use std::sync::Arc;
|
||||
use thiserror::Error;
|
||||
@@ -33,6 +34,24 @@ pub fn execute_script(
|
||||
let mut vm = Engine::new();
|
||||
let context = Arc::new(context);
|
||||
|
||||
// Register existing Steel functions
|
||||
register_steel_functions(&mut vm, context.clone());
|
||||
|
||||
// Register all decimal math functions
|
||||
register_decimal_math_functions(&mut vm);
|
||||
|
||||
// Execute script and process results
|
||||
let results = vm.compile_and_run_raw_program(script)
|
||||
.map_err(|e| ExecutionError::RuntimeError(e.to_string()))?;
|
||||
|
||||
// Convert results to target type
|
||||
match target_type {
|
||||
"STRINGS" => process_string_results(results),
|
||||
_ => Err(ExecutionError::UnsupportedType(target_type.into()))
|
||||
}
|
||||
}
|
||||
|
||||
fn register_steel_functions(vm: &mut Engine, context: Arc<SteelContext>) {
|
||||
// Register steel_get_column with row context
|
||||
vm.register_fn("steel_get_column", {
|
||||
let ctx = context.clone();
|
||||
@@ -59,27 +78,101 @@ pub fn execute_script(
|
||||
.map_err(|e| e.to_string())
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Execute script and process results
|
||||
let results = vm.compile_and_run_raw_program(script)
|
||||
.map_err(|e| ExecutionError::RuntimeError(e.to_string()))?;
|
||||
fn register_decimal_math_functions(vm: &mut Engine) {
|
||||
// Basic arithmetic operations
|
||||
vm.register_fn("decimal-add", decimal_add);
|
||||
vm.register_fn("decimal-sub", decimal_sub);
|
||||
vm.register_fn("decimal-mul", decimal_mul);
|
||||
vm.register_fn("decimal-div", decimal_div);
|
||||
|
||||
// Convert results to target type
|
||||
match target_type {
|
||||
"STRINGS" => process_string_results(results),
|
||||
_ => Err(ExecutionError::UnsupportedType(target_type.into()))
|
||||
}
|
||||
// Advanced mathematical functions
|
||||
vm.register_fn("decimal-pow", decimal_pow);
|
||||
vm.register_fn("decimal-sqrt", decimal_sqrt);
|
||||
vm.register_fn("decimal-ln", decimal_ln);
|
||||
vm.register_fn("decimal-log10", decimal_log10);
|
||||
vm.register_fn("decimal-exp", decimal_exp);
|
||||
|
||||
// Trigonometric functions
|
||||
vm.register_fn("decimal-sin", decimal_sin);
|
||||
vm.register_fn("decimal-cos", decimal_cos);
|
||||
vm.register_fn("decimal-tan", decimal_tan);
|
||||
|
||||
// Comparison functions
|
||||
vm.register_fn("decimal-gt", decimal_gt);
|
||||
vm.register_fn("decimal-lt", decimal_lt);
|
||||
vm.register_fn("decimal-eq", decimal_eq);
|
||||
|
||||
// Utility functions
|
||||
vm.register_fn("decimal-abs", decimal_abs);
|
||||
vm.register_fn("decimal-round", decimal_round);
|
||||
vm.register_fn("decimal-min", decimal_min);
|
||||
vm.register_fn("decimal-max", decimal_max);
|
||||
|
||||
// Additional convenience functions
|
||||
vm.register_fn("decimal-zero", || "0".to_string());
|
||||
vm.register_fn("decimal-one", || "1".to_string());
|
||||
vm.register_fn("decimal-pi", || "3.1415926535897932384626433833".to_string());
|
||||
vm.register_fn("decimal-e", || "2.7182818284590452353602874714".to_string());
|
||||
|
||||
// Type conversion helpers
|
||||
vm.register_fn("to-decimal", |s: String| -> Result<String, String> {
|
||||
use rust_decimal::prelude::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
Decimal::from_str(&s)
|
||||
.map(|d| d.to_string())
|
||||
.map_err(|e| format!("Invalid decimal: {}", e))
|
||||
});
|
||||
|
||||
// Financial functions
|
||||
vm.register_fn("decimal-percentage", |amount: String, percentage: String| -> Result<String, String> {
|
||||
use rust_decimal::prelude::*;
|
||||
use std::str::FromStr;
|
||||
|
||||
let amount_dec = Decimal::from_str(&amount)
|
||||
.map_err(|e| format!("Invalid amount: {}", e))?;
|
||||
let percentage_dec = Decimal::from_str(&percentage)
|
||||
.map_err(|e| format!("Invalid percentage: {}", e))?;
|
||||
let hundred = Decimal::from(100);
|
||||
|
||||
Ok((amount_dec * percentage_dec / hundred).to_string())
|
||||
});
|
||||
|
||||
vm.register_fn("decimal-compound", |principal: String, rate: String, time: String| -> Result<String, String> {
|
||||
use rust_decimal::prelude::*;
|
||||
use rust_decimal::MathematicalOps;
|
||||
use std::str::FromStr;
|
||||
|
||||
let principal_dec = Decimal::from_str(&principal)
|
||||
.map_err(|e| format!("Invalid principal: {}", e))?;
|
||||
let rate_dec = Decimal::from_str(&rate)
|
||||
.map_err(|e| format!("Invalid rate: {}", e))?;
|
||||
let time_dec = Decimal::from_str(&time)
|
||||
.map_err(|e| format!("Invalid time: {}", e))?;
|
||||
|
||||
let one = Decimal::ONE;
|
||||
let compound_factor = (one + rate_dec).checked_powd(time_dec)
|
||||
.ok_or("Compound calculation overflow")?;
|
||||
|
||||
Ok((principal_dec * compound_factor).to_string())
|
||||
});
|
||||
}
|
||||
|
||||
fn process_string_results(results: Vec<SteelVal>) -> Result<Value, ExecutionError> {
|
||||
let mut strings = Vec::new();
|
||||
for result in results {
|
||||
if let SteelVal::StringV(s) = result {
|
||||
strings.push(s.to_string());
|
||||
} else {
|
||||
return Err(ExecutionError::TypeConversionError(
|
||||
format!("Expected string, got {:?}", result)
|
||||
));
|
||||
match result {
|
||||
SteelVal::StringV(s) => strings.push(s.to_string()),
|
||||
SteelVal::NumV(n) => strings.push(n.to_string()),
|
||||
SteelVal::IntV(i) => strings.push(i.to_string()),
|
||||
SteelVal::BoolV(b) => strings.push(b.to_string()),
|
||||
_ => {
|
||||
return Err(ExecutionError::TypeConversionError(
|
||||
format!("Expected string-convertible type, got {:?}", result)
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Value::Strings(strings))
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
pub mod execution;
|
||||
pub mod syntax_parser;
|
||||
pub mod functions;
|
||||
pub mod decimal_math;
|
||||
|
||||
pub use execution::*;
|
||||
pub use syntax_parser::*;
|
||||
pub use functions::*;
|
||||
pub use decimal_math::*;
|
||||
|
||||
@@ -1,27 +1,111 @@
|
||||
// src/steel/server/syntax_parser.rs
|
||||
use regex::Regex;
|
||||
use std::collections::HashSet;
|
||||
|
||||
pub struct SyntaxParser {
|
||||
// Existing patterns for column/SQL integration
|
||||
current_table_column_re: Regex,
|
||||
different_table_column_re: Regex,
|
||||
one_to_many_indexed_re: Regex,
|
||||
sql_integration_re: Regex,
|
||||
|
||||
// Simple math operation replacement patterns
|
||||
math_operators: Vec<(Regex, &'static str)>,
|
||||
number_literal_re: Regex,
|
||||
}
|
||||
|
||||
impl SyntaxParser {
|
||||
pub fn new() -> Self {
|
||||
// Define math operator replacements
|
||||
let math_operators = vec![
|
||||
// Basic arithmetic
|
||||
(Regex::new(r"\(\s*\+\s+").unwrap(), "(decimal-add "),
|
||||
(Regex::new(r"\(\s*-\s+").unwrap(), "(decimal-sub "),
|
||||
(Regex::new(r"\(\s*\*\s+").unwrap(), "(decimal-mul "),
|
||||
(Regex::new(r"\(\s*/\s+").unwrap(), "(decimal-div "),
|
||||
|
||||
// Power and advanced operations
|
||||
(Regex::new(r"\(\s*\^\s+").unwrap(), "(decimal-pow "),
|
||||
(Regex::new(r"\(\s*\*\*\s+").unwrap(), "(decimal-pow "),
|
||||
(Regex::new(r"\(\s*pow\s+").unwrap(), "(decimal-pow "),
|
||||
(Regex::new(r"\(\s*sqrt\s+").unwrap(), "(decimal-sqrt "),
|
||||
|
||||
// Logarithmic functions
|
||||
(Regex::new(r"\(\s*ln\s+").unwrap(), "(decimal-ln "),
|
||||
(Regex::new(r"\(\s*log\s+").unwrap(), "(decimal-ln "),
|
||||
(Regex::new(r"\(\s*log10\s+").unwrap(), "(decimal-log10 "),
|
||||
(Regex::new(r"\(\s*exp\s+").unwrap(), "(decimal-exp "),
|
||||
|
||||
// Trigonometric functions
|
||||
(Regex::new(r"\(\s*sin\s+").unwrap(), "(decimal-sin "),
|
||||
(Regex::new(r"\(\s*cos\s+").unwrap(), "(decimal-cos "),
|
||||
(Regex::new(r"\(\s*tan\s+").unwrap(), "(decimal-tan "),
|
||||
|
||||
// Comparison operators
|
||||
(Regex::new(r"\(\s*>\s+").unwrap(), "(decimal-gt "),
|
||||
(Regex::new(r"\(\s*<\s+").unwrap(), "(decimal-lt "),
|
||||
(Regex::new(r"\(\s*=\s+").unwrap(), "(decimal-eq "),
|
||||
(Regex::new(r"\(\s*>=\s+").unwrap(), "(decimal-gte "),
|
||||
(Regex::new(r"\(\s*<=\s+").unwrap(), "(decimal-lte "),
|
||||
|
||||
// Utility functions
|
||||
(Regex::new(r"\(\s*abs\s+").unwrap(), "(decimal-abs "),
|
||||
(Regex::new(r"\(\s*min\s+").unwrap(), "(decimal-min "),
|
||||
(Regex::new(r"\(\s*max\s+").unwrap(), "(decimal-max "),
|
||||
(Regex::new(r"\(\s*round\s+").unwrap(), "(decimal-round "),
|
||||
];
|
||||
|
||||
SyntaxParser {
|
||||
current_table_column_re: Regex::new(r"@(\w+)").unwrap(),
|
||||
different_table_column_re: Regex::new(r"@(\w+)\.(\w+)").unwrap(),
|
||||
one_to_many_indexed_re: Regex::new(r"@(\w+)\[(\d+)\]\.(\w+)").unwrap(),
|
||||
sql_integration_re: Regex::new(r#"@sql\((['"])(.*?)['"]\)"#).unwrap(),
|
||||
|
||||
// FIXED: Match negative numbers and avoid already quoted strings
|
||||
number_literal_re: Regex::new(r#"(?<!")(-?\d+\.?\d*(?:[eE][+-]?\d+)?)(?!")"#).unwrap(),
|
||||
|
||||
math_operators,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse(&self, script: &str, current_table: &str) -> String {
|
||||
let mut transformed = script.to_string();
|
||||
|
||||
// Step 1: Convert all numeric literals to strings (FIXED to handle negative numbers)
|
||||
transformed = self.convert_numbers_to_strings(&transformed);
|
||||
|
||||
// Step 2: Replace math function calls with decimal equivalents (SIMPLIFIED)
|
||||
transformed = self.replace_math_functions(&transformed);
|
||||
|
||||
// Step 3: Handle existing column and SQL integrations (unchanged)
|
||||
transformed = self.process_column_integrations(&transformed, current_table);
|
||||
|
||||
transformed
|
||||
}
|
||||
|
||||
/// Convert all unquoted numeric literals to quoted strings
|
||||
fn convert_numbers_to_strings(&self, script: &str) -> String {
|
||||
// This regex matches numbers that are NOT already inside quotes
|
||||
self.number_literal_re.replace_all(script, |caps: ®ex::Captures| {
|
||||
format!("\"{}\"", &caps[1])
|
||||
}).to_string()
|
||||
}
|
||||
|
||||
/// Replace math function calls with decimal equivalents (SIMPLIFIED)
|
||||
fn replace_math_functions(&self, script: &str) -> String {
|
||||
let mut result = script.to_string();
|
||||
|
||||
// Apply all math operator replacements
|
||||
for (pattern, replacement) in &self.math_operators {
|
||||
result = pattern.replace_all(&result, *replacement).to_string();
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Process existing column and SQL integrations (unchanged logic)
|
||||
fn process_column_integrations(&self, script: &str, current_table: &str) -> String {
|
||||
let mut transformed = script.to_string();
|
||||
|
||||
// Process indexed access first to avoid overlap with relationship matches
|
||||
transformed = self.one_to_many_indexed_re.replace_all(&transformed, |caps: ®ex::Captures| {
|
||||
format!("(steel_get_column_with_index \"{}\" {} \"{}\")",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// tests/mod.rs
|
||||
pub mod tables_data;
|
||||
pub mod common;
|
||||
// pub mod table_definition;
|
||||
pub mod table_definition;
|
||||
|
||||
Reference in New Issue
Block a user