added kind to merge notifications, re-instated sql pattern matching
This commit is contained in:
@ -964,7 +964,7 @@ impl Merger {
|
||||
let mut notify_sql = None;
|
||||
if type_obj.historical && change_kind != "replace" {
|
||||
let change_sql = format!(
|
||||
"INSERT INTO agreego.change (\"old\", \"new\", entity_id, id, kind, modified_at, modified_by, entity_type) VALUES ({}, {}, {}, {}, {}, {}, {}, {})",
|
||||
"INSERT INTO agreego.change (\"old\", \"new\", \"entity_id\", \"id\", \"kind\", \"modified_at\", \"modified_by\", \"entity_type\") VALUES ({}, {}, {}, {}, {}, {}, {}, {})",
|
||||
Self::quote_literal(&old_val_obj),
|
||||
Self::quote_literal(&new_val_obj),
|
||||
Self::quote_literal(id_str),
|
||||
|
||||
@ -96,8 +96,10 @@ impl Case {
|
||||
let queries = db.executor.get_queries();
|
||||
if std::env::var("UPDATE_EXPECT").is_ok() {
|
||||
crate::tests::runner::update_sql_fixture(path, suite_idx, case_idx, &queries);
|
||||
Ok(())
|
||||
} else {
|
||||
expect.assert_sql(&queries)
|
||||
}
|
||||
expect.assert_sql(&queries)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
@ -128,8 +130,10 @@ impl Case {
|
||||
let queries = db.executor.get_queries();
|
||||
if std::env::var("UPDATE_EXPECT").is_ok() {
|
||||
crate::tests::runner::update_sql_fixture(path, suite_idx, case_idx, &queries);
|
||||
Ok(())
|
||||
} else {
|
||||
expect.assert_sql(&queries)
|
||||
}
|
||||
expect.assert_sql(&queries)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
pub mod pattern;
|
||||
pub mod sql;
|
||||
pub mod drop;
|
||||
pub mod schema;
|
||||
|
||||
@ -1,132 +0,0 @@
|
||||
use super::Expect;
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Expect {
|
||||
/// Advanced SQL execution assertion algorithm ported from `assert.go`.
|
||||
/// This compares two arrays of strings, one containing {{uuid:name}} or {{timestamp}} placeholders,
|
||||
/// and the other containing actual executed database queries. It ensures that placeholder UUIDs
|
||||
/// are consistently mapped to the same actual UUIDs across all lines, and strictly validates line-by-line sequences.
|
||||
pub fn assert_pattern(&self, actual: &[String]) -> Result<(), String> {
|
||||
let patterns = match &self.sql {
|
||||
Some(s) => s,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
if patterns.len() != actual.len() {
|
||||
return Err(format!(
|
||||
"Length mismatch: expected {} SQL executions, got {}.\nActual Execution Log:\n{}",
|
||||
patterns.len(),
|
||||
actual.len(),
|
||||
actual.join("\n")
|
||||
));
|
||||
}
|
||||
|
||||
let ws_re = Regex::new(r"\s+").unwrap();
|
||||
|
||||
let types = HashMap::from([
|
||||
(
|
||||
"uuid",
|
||||
r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
|
||||
),
|
||||
(
|
||||
"timestamp",
|
||||
r"\d{4}-\d{2}-\d{2}(?:[ T])\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|\+\d{2}(?::\d{2})?)?",
|
||||
),
|
||||
("integer", r"-?\d+"),
|
||||
("float", r"-?\d+\.\d+"),
|
||||
("text", r"(?:''|[^'])*"),
|
||||
("json", r"(?:''|[^'])*"),
|
||||
]);
|
||||
|
||||
let mut seen: HashMap<String, String> = HashMap::new();
|
||||
let system_uuid = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
// Placeholder regex: {{type:name}} or {{type}}
|
||||
let ph_rx = Regex::new(r"\{\{([a-z]+)(?:[:]([^}]+))?\}\}").unwrap();
|
||||
|
||||
let clean_str = |s: &str| -> String {
|
||||
let mut s = ws_re.replace_all(s, " ").into_owned();
|
||||
for token in ["(", ")", ",", "{", "}", "\"", "=", "'"] {
|
||||
s = s.replace(&format!(" {}", token), token);
|
||||
s = s.replace(&format!("{} ", token), token);
|
||||
}
|
||||
s.trim().to_string()
|
||||
};
|
||||
|
||||
for (i, pattern_expect) in patterns.iter().enumerate() {
|
||||
let aline_raw = &actual[i];
|
||||
let aline = clean_str(aline_raw);
|
||||
|
||||
let pattern_str_raw = match pattern_expect {
|
||||
super::SqlExpectation::Single(s) => s.clone(),
|
||||
super::SqlExpectation::Multi(m) => m.join(" "),
|
||||
};
|
||||
|
||||
let pattern_str = clean_str(&pattern_str_raw);
|
||||
|
||||
let mut pp = regex::escape(&pattern_str);
|
||||
pp = pp.replace(r"\{\{", "{{").replace(r"\}\}", "}}");
|
||||
|
||||
let mut cap_names = HashMap::new(); // cg_X -> var_name
|
||||
let mut group_idx = 0;
|
||||
|
||||
let mut final_rx_str = String::new();
|
||||
let mut last_match = 0;
|
||||
|
||||
let pp_clone = pp.clone();
|
||||
for caps in ph_rx.captures_iter(&pp_clone) {
|
||||
let full_match = caps.get(0).unwrap();
|
||||
final_rx_str.push_str(&pp[last_match..full_match.start()]);
|
||||
|
||||
let type_name = caps.get(1).unwrap().as_str();
|
||||
let var_name = caps.get(2).map(|m| m.as_str());
|
||||
|
||||
if let Some(name) = var_name {
|
||||
if let Some(val) = seen.get(name) {
|
||||
final_rx_str.push_str(®ex::escape(val));
|
||||
} else {
|
||||
let type_pattern = types.get(type_name).unwrap_or(&".*?");
|
||||
let cg_name = format!("cg_{}", group_idx);
|
||||
final_rx_str.push_str(&format!("(?P<{}>{})", cg_name, type_pattern));
|
||||
cap_names.insert(cg_name, name.to_string());
|
||||
group_idx += 1;
|
||||
}
|
||||
} else {
|
||||
let type_pattern = types.get(type_name).unwrap_or(&".*?");
|
||||
final_rx_str.push_str(&format!("(?:{})", type_pattern));
|
||||
}
|
||||
|
||||
last_match = full_match.end();
|
||||
}
|
||||
final_rx_str.push_str(&pp[last_match..]);
|
||||
|
||||
let final_rx = match Regex::new(&format!("^{}$", final_rx_str)) {
|
||||
Ok(r) => r,
|
||||
Err(e) => return Err(format!("Bad constructed regex: {} -> {}", final_rx_str, e)),
|
||||
};
|
||||
|
||||
if let Some(captures) = final_rx.captures(&aline) {
|
||||
for (cg_name, var_name) in cap_names {
|
||||
if let Some(m) = captures.name(&cg_name) {
|
||||
let matched_str = m.as_str();
|
||||
if matched_str != system_uuid {
|
||||
seen.insert(var_name, matched_str.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(format!(
|
||||
"Line mismatched at execution sequence {}.\nExpected Pattern: {}\nActual SQL: {}\nRegex used: {}\nVariables Mapped: {:?}",
|
||||
i + 1,
|
||||
pattern_str,
|
||||
aline,
|
||||
final_rx_str,
|
||||
seen
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,9 @@
|
||||
use super::Expect;
|
||||
use regex::Regex;
|
||||
use sqlparser::ast::{Expr, Query, SelectItem, Statement, TableFactor};
|
||||
use sqlparser::dialect::PostgreSqlDialect;
|
||||
use sqlparser::parser::Parser;
|
||||
use std::collections::HashSet;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
impl Expect {
|
||||
pub fn assert_sql(&self, actual: &[String]) -> Result<(), String> {
|
||||
@ -204,4 +205,132 @@ impl Expect {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Advanced SQL execution assertion algorithm ported from `assert.go`.
|
||||
/// This compares two arrays of strings, one containing {{uuid:name}} or {{timestamp}} placeholders,
|
||||
/// and the other containing actual executed database queries. It ensures that placeholder UUIDs
|
||||
/// are consistently mapped to the same actual UUIDs across all lines, and strictly validates line-by-line sequences.
|
||||
pub fn assert_pattern(&self, actual: &[String]) -> Result<(), String> {
|
||||
let patterns = match &self.sql {
|
||||
Some(s) => s,
|
||||
None => return Ok(()),
|
||||
};
|
||||
|
||||
if patterns.len() != actual.len() {
|
||||
return Err(format!(
|
||||
"Length mismatch: expected {} SQL executions, got {}.\nActual Execution Log:\n{}",
|
||||
patterns.len(),
|
||||
actual.len(),
|
||||
actual.join("\n")
|
||||
));
|
||||
}
|
||||
|
||||
let ws_re = Regex::new(r"\s+").unwrap();
|
||||
|
||||
let types = HashMap::from([
|
||||
(
|
||||
"uuid",
|
||||
r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
|
||||
),
|
||||
(
|
||||
"timestamp",
|
||||
r"\d{4}-\d{2}-\d{2}(?:[ T])\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|\+\d{2}(?::\d{2})?)?",
|
||||
),
|
||||
("integer", r"-?\d+"),
|
||||
("float", r"-?\d+\.\d+"),
|
||||
("text", r"(?:''|[^'])*"),
|
||||
("json", r"(?:''|[^'])*"),
|
||||
]);
|
||||
|
||||
let mut seen: HashMap<String, String> = HashMap::new();
|
||||
let system_uuid = "00000000-0000-0000-0000-000000000000";
|
||||
|
||||
// Placeholder regex: {{type:name}} or {{type}}
|
||||
let ph_rx = Regex::new(r"\{\{([a-z]+)(?:[:]([^}]+))?\}\}").unwrap();
|
||||
|
||||
let clean_str = |s: &str| -> String {
|
||||
let mut s = ws_re.replace_all(s, " ").into_owned();
|
||||
for token in ["(", ")", ",", "{", "}", "\"", "=", "'"] {
|
||||
s = s.replace(&format!(" {}", token), token);
|
||||
s = s.replace(&format!("{} ", token), token);
|
||||
}
|
||||
s.trim().to_string()
|
||||
};
|
||||
|
||||
for (i, pattern_expect) in patterns.iter().enumerate() {
|
||||
let aline_raw = &actual[i];
|
||||
let formatted_actual = crate::tests::formatter::SqlFormatter::format(aline_raw).join(" ");
|
||||
let aline = clean_str(&formatted_actual);
|
||||
|
||||
let pattern_str_raw = match pattern_expect {
|
||||
super::SqlExpectation::Single(s) => s.clone(),
|
||||
super::SqlExpectation::Multi(m) => m.join(" "),
|
||||
};
|
||||
|
||||
let pattern_str = clean_str(&pattern_str_raw);
|
||||
|
||||
let mut pp = regex::escape(&pattern_str);
|
||||
pp = pp.replace(r"\{\{", "{{").replace(r"\}\}", "}}");
|
||||
|
||||
let mut cap_names = HashMap::new(); // cg_X -> var_name
|
||||
let mut group_idx = 0;
|
||||
|
||||
let mut final_rx_str = String::new();
|
||||
let mut last_match = 0;
|
||||
|
||||
let pp_clone = pp.clone();
|
||||
for caps in ph_rx.captures_iter(&pp_clone) {
|
||||
let full_match = caps.get(0).unwrap();
|
||||
final_rx_str.push_str(&pp[last_match..full_match.start()]);
|
||||
|
||||
let type_name = caps.get(1).unwrap().as_str();
|
||||
let var_name = caps.get(2).map(|m| m.as_str());
|
||||
|
||||
if let Some(name) = var_name {
|
||||
if let Some(val) = seen.get(name) {
|
||||
final_rx_str.push_str(®ex::escape(val));
|
||||
} else {
|
||||
let type_pattern = types.get(type_name).unwrap_or(&".*?");
|
||||
let cg_name = format!("cg_{}", group_idx);
|
||||
final_rx_str.push_str(&format!("(?P<{}>{})", cg_name, type_pattern));
|
||||
cap_names.insert(cg_name, name.to_string());
|
||||
group_idx += 1;
|
||||
}
|
||||
} else {
|
||||
let type_pattern = types.get(type_name).unwrap_or(&".*?");
|
||||
final_rx_str.push_str(&format!("(?:{})", type_pattern));
|
||||
}
|
||||
|
||||
last_match = full_match.end();
|
||||
}
|
||||
final_rx_str.push_str(&pp[last_match..]);
|
||||
|
||||
let final_rx = match Regex::new(&format!("^{}$", final_rx_str)) {
|
||||
Ok(r) => r,
|
||||
Err(e) => return Err(format!("Bad constructed regex: {} -> {}", final_rx_str, e)),
|
||||
};
|
||||
|
||||
if let Some(captures) = final_rx.captures(&aline) {
|
||||
for (cg_name, var_name) in cap_names {
|
||||
if let Some(m) = captures.name(&cg_name) {
|
||||
let matched_str = m.as_str();
|
||||
if matched_str != system_uuid {
|
||||
seen.insert(var_name, matched_str.to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(format!(
|
||||
"Line mismatched at execution sequence {}.\nExpected Pattern: {}\nActual SQL: {}\nRegex used: {}\nVariables Mapped: {:?}",
|
||||
i + 1,
|
||||
pattern_str,
|
||||
aline,
|
||||
final_rx_str,
|
||||
seen
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user