use std::error::Error; use sqlgen::prelude::*; #[test] fn select_all() -> Result<(), Box> { let query = SelectQuery::new() .select(SelectList::All)? .from(TableSource::Table { table: Table::new("one"), })? .sql() .unwrap(); assert_eq!("SELECT * FROM one", &query); Ok(()) } #[test] fn select_all_of_table() -> Result<(), Box> { let table = Table::new("one"); let query = SelectQuery::new() .select(SelectList::TableAll { table: table.clone(), })? .from(TableSource::Table { table })? .sql() .unwrap(); assert_eq!("SELECT one.* FROM one", &query); Ok(()) } #[test] fn select_one_columns() -> Result<(), Box> { let table = Table::new("one"); let query = SelectQuery::new() .from(TableSource::Table { table: table.clone(), })? .select(SelectList::TableColumn { table, column: "foo".into(), })? .sql() .unwrap(); assert_eq!("SELECT one.foo FROM one", &query); Ok(()) } #[test] fn select_multiple_columns() -> Result<(), Box> { let table = Table::new("one"); let query = SelectQuery::new() .from(TableSource::Table { table: table.clone(), })? .select(SelectList::TableColumn { table: table.clone(), column: "foo".into(), })? .select(SelectList::TableColumn { table, column: "bar".into(), })? .sql() .unwrap(); assert_eq!("SELECT one.foo, one.bar FROM one", &query); Ok(()) } #[test] fn select_all_with_joined_table() -> Result<(), Box> { let table = Table::new("one"); let second = Table::new("two"); let search_condition = SearchCondition::Predicate { lhs: Expression::Column { table: table.clone(), column: Column::new("id"), }, comparison_operator: ComparisonOperator::Equals, rhs: Expression::Column { table: second.clone(), column: Column::new("id"), }, }; let join = TableSource::JoinedTable { join_type: JoinType::Inner, table_source: Box::new(TableSource::Table { table: second }), on: search_condition, }; let query = SelectQuery::new() .from(TableSource::Table { table: table.clone(), })? .select(SelectList::TableColumn { table: table.clone(), column: "foo".into(), })? .select(SelectList::TableColumn { table, column: "bar".into(), })? .join(join)? .sql() .unwrap(); assert_eq!( "SELECT one.foo, one.bar FROM one INNER JOIN two ON one.id = two.id", &query ); Ok(()) } #[test] fn select_with_where() -> Result<(), Box> { let table = Table::new("one"); let query = SelectQuery::new() .select(SelectList::TableColumn { table: table.clone(), column: Column::new("foo"), })? .from(TableSource::Table { table: table.clone(), })? .r#where(SearchCondition::Predicate { lhs: Expression::Column { table, column: Column::new("foo"), }, comparison_operator: ComparisonOperator::Equals, rhs: Expression::Value(Value::Interger(1)), })? .sql() .unwrap(); assert_eq!("SELECT one.foo FROM one WHERE one.foo = 1", query); Ok(()) } #[test] fn simple_select_with_into_params() -> Result<(), Box> { let query = SelectQuery::new() .select("foo")? .from("one")? .sql() .unwrap(); assert_eq!("SELECT foo FROM one", query); Ok(()) } #[test] fn simple_select_with_into_params_with_multipart_strings() -> Result<(), Box> { let query = SelectQuery::new() .select("one.foo")? .from("one")? .sql() .unwrap(); assert_eq!("SELECT one.foo FROM one", query); Ok(()) } #[test] fn simple_select_with_into_params_with_where_clause() -> Result<(), Box> { let query = SelectQuery::new() .select("one.foo")? .from("one")? .r#where("one.foo = 1")? .sql() .unwrap(); assert_eq!("SELECT one.foo FROM one WHERE one.foo = 1", query); Ok(()) } #[test] fn impl_from_as_ref_checks() -> Result<(), Box> { let select = String::from("one.foo"); let table = String::from("one"); let query = SelectQuery::new() .select(&select)? .from(table)? .r#where(format!("{} = 1", select))? .sql() .unwrap(); assert_eq!("SELECT one.foo FROM one WHERE one.foo = 1", query); Ok(()) } #[test] fn simple_select_with_into_params_join() -> Result<(), Box> { let query = SelectQuery::new() .select("one.*")? .select("two.*")? .from("one")? .inner_join("two")? .on("one.id = two.id")? .sql() .unwrap(); assert_eq!( "SELECT one.*, two.* FROM one INNER JOIN two ON one.id = two.id", query ); Ok(()) } // #[test] // fn simple_select_with_alias() -> Result<(), Box> { // let query = SelectQuery::new() // .select("one.id as [one.id]")? // .from("one")? // .sql()?; // assert_eq!("SELECT one.id as [one.id] from ") // }