2019-04-12 17:09:59 -04:00
|
|
|
use stringqb::prelude::*;
|
2019-04-11 17:16:35 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn minimal_select_query() {
|
|
|
|
let mut qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
let sql = qb.from("foo").get_compiled_select();
|
|
|
|
|
|
|
|
assert_eq!(sql, "SELECT *\nFROM \"foo\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select_keys_as_query() {
|
|
|
|
let mut qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
let sql = qb.select("foo as bar, baz").from("a").get_compiled_select();
|
|
|
|
|
|
|
|
assert_eq!(sql, "SELECT \"foo\" as \"bar\",\"baz\"\nFROM \"a\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select_keys_query() {
|
|
|
|
let mut qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
let sql = qb.select("a, b, c").from("foo").get_compiled_select();
|
|
|
|
|
|
|
|
assert_eq!(sql, "SELECT \"a\",\"b\",\"c\"\nFROM \"foo\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn select_vec_keys_query() {
|
|
|
|
let mut qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
let sql = qb
|
|
|
|
.select_vec(vec!["a", "b", "c"])
|
|
|
|
.from("foo")
|
|
|
|
.get_compiled_select();
|
|
|
|
|
|
|
|
assert_eq!(sql, "SELECT \"a\",\"b\",\"c\"\nFROM \"foo\"");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn select_without_from() {
|
|
|
|
let qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
qb.get_compiled_select();
|
|
|
|
}
|
|
|
|
|
2019-04-12 17:09:59 -04:00
|
|
|
#[test]
|
|
|
|
fn select_where_in() {
|
|
|
|
let mut qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
qb.from("test")
|
|
|
|
.where_in("foo", vec![
|
|
|
|
Box::new(0),
|
|
|
|
Box::new(1),
|
|
|
|
Box::new(2),
|
|
|
|
Box::new(3),
|
|
|
|
Box::new(4),
|
|
|
|
Box::new(5)
|
|
|
|
]);
|
|
|
|
|
|
|
|
let sql = qb.get_compiled_select();
|
|
|
|
let expected = "SELECT *\nFROM \"test\" WHERE \"foo\" IN (?,?,?,?,?,?) ";
|
|
|
|
|
|
|
|
assert_eq!(sql, expected);
|
|
|
|
}
|
|
|
|
|
2019-04-11 17:16:35 -04:00
|
|
|
#[test]
|
|
|
|
fn basic_insert_query() {
|
|
|
|
let mut qb = QueryBuilder::default();
|
|
|
|
|
|
|
|
qb.set("foo", Box::new("bar"));
|
|
|
|
|
|
|
|
let sql = qb.get_compiled_insert("foobar");
|
|
|
|
|
|
|
|
assert_eq!(sql, "INSERT INTO \"foobar\" (\"foo\")\nVALUES(?)");
|
|
|
|
}
|