From 1dcca498376bb0de4950e5bab2a055b26ec3656c Mon Sep 17 00:00:00 2001 From: Sam Samai Date: Fri, 16 Jul 2021 17:45:55 +1000 Subject: [PATCH] Test having --- tests/relational_tests.rs | 116 +++++++++++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/tests/relational_tests.rs b/tests/relational_tests.rs index 3e9222fa..e0fb5ba4 100644 --- a/tests/relational_tests.rs +++ b/tests/relational_tests.rs @@ -14,8 +14,15 @@ async fn main() { let right_join_fut = test_right_join(); let inner_join_fut = test_inner_join(); let group_by_fut = test_group_by(); + let having_fut = test_having(); - join!(left_join_fut, right_join_fut, inner_join_fut, group_by_fut); + join!( + left_join_fut, + right_join_fut, + inner_join_fut, + group_by_fut, + having_fut + ); } pub async fn test_left_join() { @@ -344,4 +351,111 @@ pub async fn test_group_by() { .max(kate_order_2.total.clone().unwrap()) ) ); + ctx.delete().await; +} + +pub async fn test_having() { + // customers with orders with total equal to $100 + let ctx = TestContext::new("mysql://root:@localhost", "test_having").await; + + let bakery = bakery::ActiveModel { + name: Set("SeaSide Bakery".to_owned()), + profit_margin: Set(10.4), + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert bakery"); + + let customer_kate = customer::ActiveModel { + name: Set("Kate".to_owned()), + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert customer"); + + let kate_order_1 = order::ActiveModel { + bakery_id: Set(Some(bakery.id.clone().unwrap())), + customer_id: Set(Some(customer_kate.id.clone().unwrap())), + total: Set(dec!(100.00)), + placed_at: Set(Utc::now().naive_utc()), + + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert order"); + + let _kate_order_2 = order::ActiveModel { + bakery_id: Set(Some(bakery.id.clone().unwrap())), + customer_id: Set(Some(customer_kate.id.clone().unwrap())), + total: Set(dec!(12.00)), + placed_at: Set(Utc::now().naive_utc()), + + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert order"); + + let customer_bob = customer::ActiveModel { + name: Set("Bob".to_owned()), + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert customer"); + + let _bob_order_1 = order::ActiveModel { + bakery_id: Set(Some(bakery.id.clone().unwrap())), + customer_id: Set(Some(customer_bob.id.clone().unwrap())), + total: Set(dec!(50.0)), + placed_at: Set(Utc::now().naive_utc()), + + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert order"); + + let _bob_order_2 = order::ActiveModel { + bakery_id: Set(Some(bakery.id.clone().unwrap())), + customer_id: Set(Some(customer_bob.id.clone().unwrap())), + total: Set(dec!(50.0)), + placed_at: Set(Utc::now().naive_utc()), + + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert order"); + + #[derive(Debug, FromQueryResult)] + struct SelectResult { + name: String, + order_total: Option, + } + + let results = customer::Entity::find() + .inner_join(order::Entity) + .select_only() + .column(customer::Column::Name) + .column_as(order::Column::Total, "order_total") + .group_by(customer::Column::Name) + .group_by(order::Column::Total) + .having(order::Column::Total.eq(dec!(100.00))) + .into_model::() + .all(&ctx.db) + .await + .unwrap(); + + assert_eq!(results.len(), 1); + assert_eq!(results[0].name, customer_kate.name.clone().unwrap()); + assert_eq!( + results[0].order_total, + Some(kate_order_1.total.clone().unwrap()) + ); + + ctx.delete().await; }