stringqb/tests/integration_test.rs

84 lines
1.8 KiB
Rust
Raw Normal View History

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();
2019-07-19 12:45:18 -04:00
assert_eq!(sql, "SELECT \"foo\" AS \"bar\",\"baz\"\nFROM \"a\"");
2019-04-11 17:16:35 -04:00
}
#[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-17 14:10:45 -04:00
#[test]
fn select_where() {
let mut qb = QueryBuilder::default();
2019-04-22 08:59:40 -04:00
qb.from("test").r#where("foo", "bar");
2019-04-17 14:10:45 -04:00
let sql = qb.get_compiled_select();
2019-07-19 12:45:18 -04:00
let expected = "SELECT *\nFROM \"test\"\nWHERE \"foo\"=?";
2019-04-17 14:10:45 -04:00
assert_eq!(sql, expected);
}
2019-04-12 17:09:59 -04:00
#[test]
fn select_where_in() {
let mut qb = QueryBuilder::default();
2019-04-17 12:58:05 -04:00
qb.from("test").where_in("foo", vec![0, 1, 2, 3, 4, 5]);
2019-04-12 17:09:59 -04:00
let sql = qb.get_compiled_select();
2019-07-19 12:45:18 -04:00
let expected = "SELECT *\nFROM \"test\"\nWHERE \"foo\" IN (?,?,?,?,?,?) ";
2019-04-12 17:09:59 -04:00
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(?)");
}