use super::super::*;
#[test]
fn test_parse_position_basic() {
let result = Parser::parse_sql("SELECT POSITION('lo' IN 'hello');");
if result.is_err() {
eprintln!("Parse error: {:?}", result.as_ref().err());
}
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select_stmt) => {
assert_eq!(select_stmt.select_list.len(), 1);
match &select_stmt.select_list[0] {
vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
vibesql_ast::Expression::Position { substring, string, character_unit: _ } => {
assert_eq!(
**substring,
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("lo")
))
);
assert_eq!(
**string,
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("hello")
))
);
}
_ => panic!("Expected Position expression, got {:?}", expr),
},
_ => panic!("Expected Expression select item"),
}
}
_ => panic!("Expected SELECT statement"),
}
}
#[test]
fn test_parse_position_substring_found() {
let result = Parser::parse_sql("SELECT POSITION('world' IN 'hello world');");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select_stmt) => {
assert_eq!(select_stmt.select_list.len(), 1);
match &select_stmt.select_list[0] {
vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
vibesql_ast::Expression::Position { substring, string, character_unit: _ } => {
assert_eq!(
**substring,
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("world")
))
);
assert_eq!(
**string,
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("hello world")
))
);
}
_ => panic!("Expected Position expression"),
},
_ => panic!("Expected Expression select item"),
}
}
_ => panic!("Expected SELECT statement"),
}
}
#[test]
fn test_parse_position_with_column() {
let result = Parser::parse_sql("SELECT POSITION('x' IN name) FROM users;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select_stmt) => {
assert_eq!(select_stmt.select_list.len(), 1);
match &select_stmt.select_list[0] {
vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
vibesql_ast::Expression::Position { substring, string, character_unit: _ } => {
assert_eq!(
**substring,
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("x")
))
);
assert_eq!(
**string,
vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("name", false)
)
);
}
_ => panic!("Expected Position expression"),
},
_ => panic!("Expected Expression select item"),
}
}
_ => panic!("Expected SELECT statement"),
}
}
#[test]
fn test_parse_position_both_columns() {
let result = Parser::parse_sql("SELECT position(needle IN haystack) FROM test;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select_stmt) => {
assert_eq!(select_stmt.select_list.len(), 1);
match &select_stmt.select_list[0] {
vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
vibesql_ast::Expression::Position { substring, string, character_unit: _ } => {
assert_eq!(
**substring,
vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("needle", false)
)
);
assert_eq!(
**string,
vibesql_ast::Expression::ColumnRef(
vibesql_ast::ColumnIdentifier::simple("haystack", false)
)
);
}
_ => panic!("Expected Position expression"),
},
_ => panic!("Expected Expression select item"),
}
}
_ => panic!("Expected SELECT statement"),
}
}
#[test]
fn test_parse_position_with_function() {
let result = Parser::parse_sql("SELECT POSITION('a' IN LOWER(name)) FROM users;");
assert!(result.is_ok());
let stmt = result.unwrap();
match stmt {
vibesql_ast::Statement::Select(select_stmt) => {
assert_eq!(select_stmt.select_list.len(), 1);
match &select_stmt.select_list[0] {
vibesql_ast::SelectItem::Expression { expr, .. } => match expr {
vibesql_ast::Expression::Position { substring, string, character_unit: _ } => {
assert_eq!(
**substring,
vibesql_ast::Expression::Literal(vibesql_types::SqlValue::Varchar(
arcstr::ArcStr::from("a")
))
);
match &**string {
vibesql_ast::Expression::Function { name, args, character_unit: _ } => {
assert_eq!(name, "lower");
assert_eq!(args.len(), 1);
}
_ => panic!("Expected Function expression in string"),
}
}
_ => panic!("Expected Position expression"),
},
_ => panic!("Expected Expression select item"),
}
}
_ => panic!("Expected SELECT statement"),
}
}