Do you wish to register an account?
An attempt to make a less strongly-typed database abstraction layer for Rust.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.8KB

  1. use stringqb::prelude::*;
  2. #[test]
  3. fn minimal_select_query() {
  4. let mut qb = QueryBuilder::default();
  5. let sql = qb.from("foo").get_compiled_select();
  6. assert_eq!(sql, "SELECT *\nFROM \"foo\"");
  7. }
  8. #[test]
  9. fn select_keys_as_query() {
  10. let mut qb = QueryBuilder::default();
  11. let sql = qb.select("foo as bar, baz").from("a").get_compiled_select();
  12. assert_eq!(sql, "SELECT \"foo\" AS \"bar\",\"baz\"\nFROM \"a\"");
  13. }
  14. #[test]
  15. fn select_keys_query() {
  16. let mut qb = QueryBuilder::default();
  17. let sql = qb.select("a, b, c").from("foo").get_compiled_select();
  18. assert_eq!(sql, "SELECT \"a\",\"b\",\"c\"\nFROM \"foo\"");
  19. }
  20. #[test]
  21. fn select_vec_keys_query() {
  22. let mut qb = QueryBuilder::default();
  23. let sql = qb
  24. .select_vec(vec!["a", "b", "c"])
  25. .from("foo")
  26. .get_compiled_select();
  27. assert_eq!(sql, "SELECT \"a\",\"b\",\"c\"\nFROM \"foo\"");
  28. }
  29. #[test]
  30. #[should_panic]
  31. fn select_without_from() {
  32. let qb = QueryBuilder::default();
  33. qb.get_compiled_select();
  34. }
  35. #[test]
  36. fn select_where() {
  37. let mut qb = QueryBuilder::default();
  38. qb.from("test").r#where("foo", "bar");
  39. let sql = qb.get_compiled_select();
  40. let expected = "SELECT *\nFROM \"test\"\nWHERE \"foo\"=?";
  41. assert_eq!(sql, expected);
  42. }
  43. #[test]
  44. fn select_where_in() {
  45. let mut qb = QueryBuilder::default();
  46. qb.from("test").where_in("foo", vec![0, 1, 2, 3, 4, 5]);
  47. let sql = qb.get_compiled_select();
  48. let expected = "SELECT *\nFROM \"test\"\nWHERE \"foo\" IN (?,?,?,?,?,?) ";
  49. assert_eq!(sql, expected);
  50. }
  51. #[test]
  52. fn basic_insert_query() {
  53. let mut qb = QueryBuilder::default();
  54. qb.set("foo", Box::new("bar"));
  55. let sql = qb.get_compiled_insert("foobar");
  56. assert_eq!(sql, "INSERT INTO \"foobar\" (\"foo\")\nVALUES(?)");
  57. }