fixed ordering of all things sql

This commit is contained in:
2026-05-14 05:58:38 -04:00
parent ce9c9baac9
commit 2a8b991269
12 changed files with 2334 additions and 1799 deletions

View File

@ -127,7 +127,7 @@ pub fn run_test_case(path: &str, suite_idx: usize, case_idx: usize) -> Result<()
}
}
"merge" => {
let result = test.run_merge(db_unwrapped.unwrap());
let result = test.run_merge(db_unwrapped.unwrap(), path, suite_idx, case_idx);
if let Err(e) = result {
println!("TEST MERGE ERROR FOR '{}': {}", test.description, e);
failures.push(format!(
@ -137,7 +137,7 @@ pub fn run_test_case(path: &str, suite_idx: usize, case_idx: usize) -> Result<()
}
}
"query" => {
let result = test.run_query(db_unwrapped.unwrap());
let result = test.run_query(db_unwrapped.unwrap(), path, suite_idx, case_idx);
if let Err(e) = result {
println!("TEST QUERY ERROR FOR '{}': {}", test.description, e);
failures.push(format!(
@ -160,3 +160,83 @@ pub fn run_test_case(path: &str, suite_idx: usize, case_idx: usize) -> Result<()
Ok(())
}
pub fn extract_uuids(val: &Value, path: &str, map: &mut HashMap<String, String>) {
let uuid_re = regex::Regex::new(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$").unwrap();
match val {
Value::Object(obj) => {
for (k, v) in obj {
let new_path = if path.is_empty() { k.clone() } else { format!("{}.{}", path, k) };
extract_uuids(v, &new_path, map);
}
}
Value::Array(arr) => {
for (i, v) in arr.iter().enumerate() {
let new_path = if path.is_empty() { i.to_string() } else { format!("{}.{}", path, i) };
extract_uuids(v, &new_path, map);
}
}
Value::String(s) => {
if s != "00000000-0000-0000-0000-000000000000" && uuid_re.is_match(s) {
map.insert(s.clone(), path.to_string());
}
}
_ => {}
}
}
pub fn canonicalize_with_map(s: &str, uuid_map: &HashMap<String, String>, gen_map: &mut HashMap<String, usize>) -> String {
let uuid_re = regex::Regex::new(r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}").unwrap();
let s1 = uuid_re.replace_all(s, |caps: &regex::Captures| {
let val = &caps[0];
if val == "00000000-0000-0000-0000-000000000000" {
val.to_string()
} else if let Some(path) = uuid_map.get(val) {
format!("{{{{uuid:{}}}}}", path)
} else {
let next_idx = gen_map.len();
let idx = *gen_map.entry(val.to_string()).or_insert(next_idx);
format!("{{{{uuid:generated_{}}}}}", idx)
}
});
let ts_re = regex::Regex::new(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?(?:Z|\+\d{2}(?::\d{2})?)?").unwrap();
ts_re.replace_all(&s1, "{{timestamp}}").to_string()
}
pub fn update_sql_fixture(path: &str, suite_idx: usize, case_idx: usize, queries: &[String]) {
use crate::tests::formatter::SqlFormatter;
let content = fs::read_to_string(path).unwrap();
let mut file_data: Value = serde_json::from_str(&content).unwrap();
let mut uuid_map = HashMap::new();
if let Some(test_case) = file_data.get(suite_idx).and_then(|s| s.get("tests")).and_then(|t| t.get(case_idx)) {
if let Some(data) = test_case.get("data") {
extract_uuids(data, "data", &mut uuid_map);
}
if let Some(mocks) = test_case.get("mocks") {
extract_uuids(mocks, "mocks", &mut uuid_map);
}
}
let mut gen_map = HashMap::new();
let mut formatted_sql = Vec::new();
for q in queries {
let res = SqlFormatter::format(q);
let mapped_res: Vec<String> = res.into_iter().map(|l| canonicalize_with_map(&l, &uuid_map, &mut gen_map)).collect();
formatted_sql.push(mapped_res);
}
if let Some(expect) = file_data[suite_idx]["tests"][case_idx].get_mut("expect") {
if let Some(obj) = expect.as_object_mut() {
obj.remove("pattern");
obj.insert("sql".to_string(), serde_json::json!(formatted_sql));
}
}
// To preserve original formatting, we just use serde_json pretty output
let formatted_json = serde_json::to_string_pretty(&file_data).unwrap();
fs::write(path, formatted_json).unwrap();
}