fixed test issues with lookups
This commit is contained in:
@ -130,56 +130,69 @@ impl MockExecutor {
|
||||
#[cfg(test)]
|
||||
fn parse_and_match_mocks(sql: &str, mocks: &[Value]) -> Option<Vec<Value>> {
|
||||
let sql_upper = sql.to_uppercase();
|
||||
if !sql_upper.starts_with("SELECT") {
|
||||
if !sql_upper.starts_with("SELECT") && !sql_upper.starts_with("(SELECT") {
|
||||
return None;
|
||||
}
|
||||
|
||||
// 1. Extract table name
|
||||
let table_regex = Regex::new(r#"(?i)\s+FROM\s+(?:[a-zA-Z_]\w*\.)?"?([a-zA-Z_]\w*)"?"#).ok()?;
|
||||
let table = if let Some(caps) = table_regex.captures(sql) {
|
||||
caps.get(1)?.as_str()
|
||||
} else {
|
||||
return None;
|
||||
};
|
||||
let union_regex = Regex::new(r"(?i)\s+UNION\s+").ok()?;
|
||||
let queries: Vec<&str> = union_regex.split(sql).collect();
|
||||
|
||||
// 2. Extract WHERE conditions string
|
||||
let mut where_clause = String::new();
|
||||
if let Some(where_idx) = sql_upper.find(" WHERE ") {
|
||||
let mut where_end = sql_upper.find(" ORDER BY ").unwrap_or(sql_upper.len());
|
||||
if let Some(limit_idx) = sql_upper.find(" LIMIT ") {
|
||||
if limit_idx < where_end {
|
||||
where_end = limit_idx;
|
||||
}
|
||||
}
|
||||
where_clause = sql[where_idx + 7..where_end].to_string();
|
||||
}
|
||||
|
||||
// 3. Find matching mocks
|
||||
let mut matches = Vec::new();
|
||||
let or_regex = Regex::new(r"(?i)\s+OR\s+").ok()?;
|
||||
let and_regex = Regex::new(r"(?i)\s+AND\s+").ok()?;
|
||||
|
||||
for mock in mocks {
|
||||
if let Some(mock_obj) = mock.as_object() {
|
||||
if let Some(t) = mock_obj.get("type") {
|
||||
if t.as_str() != Some(table) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let mock_obj = match mock.as_object() {
|
||||
Some(obj) => obj,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
if where_clause.is_empty() {
|
||||
matches.push(mock.clone());
|
||||
let mock_type = mock_obj.get("type").and_then(|v| v.as_str()).unwrap_or("");
|
||||
|
||||
let mut mock_matched = false;
|
||||
|
||||
for query in &queries {
|
||||
let q_upper = query.to_uppercase();
|
||||
|
||||
// Check if mock type matches the table or any joined tables in this query
|
||||
let table_regex = Regex::new(r#"(?i)\s+(?:FROM|JOIN)\s+(?:[a-zA-Z_]\w*\.)?"?([a-zA-Z_]\w*)"?"#).ok()?;
|
||||
let tables: Vec<String> = table_regex
|
||||
.captures_iter(query)
|
||||
.filter_map(|c| c.get(1).map(|m| m.as_str().to_string()))
|
||||
.collect();
|
||||
|
||||
if !mock_type.is_empty() && !tables.is_empty() && !tables.iter().any(|t| t == mock_type) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let or_parts = or_regex.split(&where_clause);
|
||||
let mut any_branch_matched = false;
|
||||
// Extract WHERE clause
|
||||
let mut where_clause = String::new();
|
||||
if let Some(where_idx) = q_upper.find(" WHERE ") {
|
||||
let mut where_end = q_upper.find(" ORDER BY ").unwrap_or(q_upper.len());
|
||||
if let Some(limit_idx) = q_upper.find(" LIMIT ") {
|
||||
if limit_idx < where_end {
|
||||
where_end = limit_idx;
|
||||
}
|
||||
}
|
||||
where_clause = query[where_idx + 7..where_end].trim_end_matches(')').to_string();
|
||||
}
|
||||
|
||||
if where_clause.is_empty() {
|
||||
mock_matched = true;
|
||||
break;
|
||||
}
|
||||
|
||||
let or_parts = or_regex.split(&where_clause);
|
||||
for or_part in or_parts {
|
||||
let branch_str = or_part.replace('(', "").replace(')', "");
|
||||
let mut branch_matches = true;
|
||||
|
||||
for part in and_regex.split(&branch_str) {
|
||||
let part = part.trim();
|
||||
if part.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if let Some(eq_idx) = part.find('=') {
|
||||
let left = part[..eq_idx]
|
||||
.trim()
|
||||
@ -193,7 +206,7 @@ fn parse_and_match_mocks(sql: &str, mocks: &[Value]) -> Option<Vec<Value>> {
|
||||
Some(Value::String(s)) => s.clone(),
|
||||
Some(Value::Number(n)) => n.to_string(),
|
||||
Some(Value::Bool(b)) => b.to_string(),
|
||||
Some(Value::Null) => "null".to_string(),
|
||||
Some(Value::Null) | None => "null".to_string(),
|
||||
_ => "".to_string(),
|
||||
};
|
||||
if mock_val_str != right {
|
||||
@ -201,19 +214,21 @@ fn parse_and_match_mocks(sql: &str, mocks: &[Value]) -> Option<Vec<Value>> {
|
||||
break;
|
||||
}
|
||||
} else if part.to_uppercase().contains(" IS NULL") {
|
||||
let left = part[..part.to_uppercase().find(" IS NULL").unwrap()]
|
||||
let is_null_idx = part.to_uppercase().find(" IS NULL").unwrap();
|
||||
let left = part[..is_null_idx]
|
||||
.trim()
|
||||
.split('.')
|
||||
.last()
|
||||
.unwrap_or("")
|
||||
.trim_matches('"');
|
||||
|
||||
let mock_val_str = match mock_obj.get(left) {
|
||||
Some(Value::Null) => "null".to_string(),
|
||||
_ => "".to_string(),
|
||||
let is_null_val = match mock_obj.get(left) {
|
||||
Some(Value::Null) | None => true,
|
||||
Some(Value::String(s)) if s.is_empty() => true,
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if mock_val_str != "null" {
|
||||
if !is_null_val {
|
||||
branch_matches = false;
|
||||
break;
|
||||
}
|
||||
@ -221,15 +236,19 @@ fn parse_and_match_mocks(sql: &str, mocks: &[Value]) -> Option<Vec<Value>> {
|
||||
}
|
||||
|
||||
if branch_matches {
|
||||
any_branch_matched = true;
|
||||
mock_matched = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if any_branch_matched {
|
||||
matches.push(mock.clone());
|
||||
if mock_matched {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if mock_matched {
|
||||
matches.push(mock.clone());
|
||||
}
|
||||
}
|
||||
|
||||
Some(matches)
|
||||
|
||||
@ -5,6 +5,14 @@ use serde::{Deserialize, Serialize};
|
||||
use std::sync::Arc;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct Roles {
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub read: Vec<String>,
|
||||
#[serde(default, skip_serializing_if = "Vec::is_empty")]
|
||||
pub write: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
#[serde(default)]
|
||||
pub struct Type {
|
||||
@ -38,6 +46,8 @@ pub struct Type {
|
||||
#[serde(default)]
|
||||
pub field_defaults: IndexMap<String, Value>,
|
||||
pub field_types: Option<Value>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub roles: Option<Roles>,
|
||||
#[serde(default)]
|
||||
pub schemas: IndexMap<String, Arc<Schema>>,
|
||||
}
|
||||
|
||||
@ -762,23 +762,33 @@ impl Merger {
|
||||
if let Some(parent_type) = self.db.types.get(parent_type_name) {
|
||||
if !parent_type.lookup_fields.is_empty() {
|
||||
let mut lookup_complete = true;
|
||||
let mut has_provided_fields = false;
|
||||
for column in &parent_type.lookup_fields {
|
||||
let is_nullable = parent_type.null_fields.contains(column);
|
||||
let val = entity_fields.get(column).or_else(|| {
|
||||
parent_type.field_defaults.get(column)
|
||||
});
|
||||
match val {
|
||||
Some(Value::Null) | None => {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
if !is_nullable {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Some(Value::String(s)) if s.is_empty() => {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
if !is_nullable {
|
||||
lookup_complete = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if entity_fields.contains_key(column) {
|
||||
has_provided_fields = true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if lookup_complete {
|
||||
if lookup_complete && has_provided_fields {
|
||||
lookup_satisfied_keys.push((&parent_type.lookup_fields, parent_type));
|
||||
}
|
||||
}
|
||||
@ -831,7 +841,11 @@ impl Merger {
|
||||
.get(column)
|
||||
.or_else(|| parent_type.field_defaults.get(column))
|
||||
.unwrap_or(&Value::Null);
|
||||
lookup_predicates.push(format!("{}.\"{}\" = {}", t_alias, column, Self::quote_literal(val)));
|
||||
if val.is_null() || val.as_str() == Some("") {
|
||||
lookup_predicates.push(format!("{}.\"{}\" IS NULL", t_alias, column));
|
||||
} else {
|
||||
lookup_predicates.push(format!("{}.\"{}\" = {}", t_alias, column, Self::quote_literal(val)));
|
||||
}
|
||||
}
|
||||
where_parts.push(format!("({})", lookup_predicates.join(" AND ")));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user