use spg_engine::{Engine, QueryResult};
use spg_storage::Value;
fn one_row(r: QueryResult) -> Vec<Value<'static>> {
match r {
QueryResult::Rows { rows, .. } => {
assert_eq!(rows.len(), 1);
rows.into_iter().next().unwrap().values
}
_ => panic!(),
}
}
fn int(e: &mut Engine, sql: &str) -> i32 {
let row = one_row(
e.execute(sql)
.unwrap_or_else(|err| panic!("{sql}: {err:?}")),
);
match &row[0] {
Value::Int(n) => *n,
Value::BigInt(n) => *n as i32,
other => panic!("expected Int, got {other:?}"),
}
}
#[test]
fn strpos_found_at_start() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', 'he')"), 1);
}
#[test]
fn strpos_found_in_middle() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', 'll')"), 3);
}
#[test]
fn strpos_found_at_end() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', 'o')"), 5);
}
#[test]
fn strpos_returns_first_match_only() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', 'l')"), 3);
}
#[test]
fn strpos_not_found_returns_zero() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', 'XYZ')"), 0);
}
#[test]
fn strpos_needle_longer_than_haystack_returns_zero() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hi', 'hello')"), 0);
}
#[test]
fn strpos_empty_needle_returns_one() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', '')"), 1);
}
#[test]
fn strpos_empty_haystack_nonempty_needle_returns_zero() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('', 'a')"), 0);
}
#[test]
fn strpos_both_empty_returns_one() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('', '')"), 1);
}
#[test]
fn strpos_unicode_haystack_codepoint_position() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('日本語', '本')"), 2);
}
#[test]
fn strpos_unicode_at_end() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('日本語', '語')"), 3);
}
#[test]
fn strpos_unicode_substring() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('日本語', '本語')"), 2);
}
#[test]
fn strpos_unicode_not_found() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('日本語', '中')"), 0);
}
#[test]
fn strpos_ascii_in_unicode_haystack() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('日aXa日', 'X')"), 3);
}
#[test]
fn strpos_is_case_sensitive() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('HELLO', 'l')"), 0);
}
#[test]
fn strpos_null_haystack_returns_null() {
let mut e = Engine::new();
let row = one_row(e.execute("SELECT strpos(NULL, 'a')").unwrap());
assert_eq!(row[0], Value::Null);
}
#[test]
fn strpos_null_needle_returns_null() {
let mut e = Engine::new();
let row = one_row(e.execute("SELECT strpos('a', NULL)").unwrap());
assert_eq!(row[0], Value::Null);
}
#[test]
fn strpos_one_arg_errors() {
let mut e = Engine::new();
assert!(e.execute("SELECT strpos('a')").is_err());
}
#[test]
fn strpos_three_args_errors() {
let mut e = Engine::new();
assert!(e.execute("SELECT strpos('a', 'b', 'c')").is_err());
}
#[test]
fn strpos_arg_order_differs_from_position() {
let mut e = Engine::new();
assert_eq!(int(&mut e, "SELECT strpos('hello', 'll')"), 3);
assert_eq!(int(&mut e, "SELECT position('ll', 'hello')"), 3);
}
#[test]
fn strpos_numeric_args_coerced() {
let mut e = Engine::new();
let m = e
.execute("SELECT strpos(12345, '3')")
.expect_err("PG has no strpos(integer, text)")
.to_string();
assert!(
m.contains("function strpos(integer, text) does not exist"),
"{m}"
);
}
#[test]
fn strpos_inside_where() {
let mut e = Engine::new();
e.execute("CREATE TABLE u (id INT NOT NULL, email TEXT NOT NULL)")
.unwrap();
e.execute("INSERT INTO u VALUES (1, 'alice@example.com'), (2, 'no-at-here')")
.unwrap();
let r = e
.execute("SELECT id FROM u WHERE strpos(email, '@') > 0")
.unwrap();
let QueryResult::Rows { rows, .. } = r else {
panic!()
};
assert_eq!(rows.len(), 1);
assert_eq!(rows[0].values[0], Value::Int(1));
}
#[test]
fn strpos_inside_insert_values() {
let mut e = Engine::new();
e.execute("CREATE TABLE u (pos INT NOT NULL)").unwrap();
e.execute("INSERT INTO u VALUES (strpos('hello', 'll'))")
.unwrap();
let row = one_row(e.execute("SELECT pos FROM u").unwrap());
assert_eq!(row[0], Value::Int(3));
}
#[test]
fn strpos_column_type_is_int() {
let mut e = Engine::new();
let r = e.execute("SELECT strpos('a', 'a')").unwrap();
let QueryResult::Rows { columns, .. } = r else {
panic!()
};
assert_eq!(columns[0].ty, spg_storage::DataType::Int);
}