diff --git a/tests/common/bakery_chain/customer.rs b/tests/common/bakery_chain/customer.rs index a7464082..ce4319ff 100644 --- a/tests/common/bakery_chain/customer.rs +++ b/tests/common/bakery_chain/customer.rs @@ -13,7 +13,7 @@ impl EntityName for Entity { pub struct Model { pub id: i32, pub name: String, - pub notes: String, + pub notes: Option, } #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] diff --git a/tests/crud/create_lineitem.rs b/tests/crud/create_lineitem.rs index 81b4c479..b4d9fc7e 100644 --- a/tests/crud/create_lineitem.rs +++ b/tests/crud/create_lineitem.rs @@ -53,7 +53,7 @@ pub async fn test_create_lineitem(db: &DbConn) { // Customer let customer_kate = customer::ActiveModel { name: Set("Kate".to_owned()), - notes: Set("Loves cheese cake".to_owned()), + notes: Set(Some("Loves cheese cake".to_owned())), ..Default::default() }; let customer_insert_res: InsertResult = Customer::insert(customer_kate) diff --git a/tests/crud/create_order.rs b/tests/crud/create_order.rs index 5b0cd42d..84896d43 100644 --- a/tests/crud/create_order.rs +++ b/tests/crud/create_order.rs @@ -53,7 +53,7 @@ pub async fn test_create_order(db: &DbConn) { // Customer let customer_kate = customer::ActiveModel { name: Set("Kate".to_owned()), - notes: Set("Loves cheese cake".to_owned()), + notes: Set(Some("Loves cheese cake".to_owned())), ..Default::default() }; let customer_insert_res: InsertResult = Customer::insert(customer_kate) diff --git a/tests/crud/mod.rs b/tests/crud/mod.rs index 9fbd7f94..bec87ced 100644 --- a/tests/crud/mod.rs +++ b/tests/crud/mod.rs @@ -88,7 +88,7 @@ pub async fn test_create_baker(db: &DbConn) { pub async fn test_create_customer(db: &DbConn) { let customer_kate = customer::ActiveModel { name: Set("Kate".to_owned()), - notes: Set("Loves cheese cake".to_owned()), + notes: Set(Some("Loves cheese cake".to_owned())), ..Default::default() }; let res: InsertResult = Customer::insert(customer_kate) @@ -104,5 +104,5 @@ pub async fn test_create_customer(db: &DbConn) { assert!(customer.is_some()); let customer_model = customer.unwrap(); assert_eq!(customer_model.name, "Kate"); - assert_eq!(customer_model.notes, "Loves cheese cake"); + assert_eq!(customer_model.notes, Some("Loves cheese cake".to_owned())); } diff --git a/tests/relational_tests.rs b/tests/relational_tests.rs index 2a07db3a..5fd7431b 100644 --- a/tests/relational_tests.rs +++ b/tests/relational_tests.rs @@ -1,3 +1,6 @@ +use chrono::offset::Utc; +use rust_decimal::prelude::*; +use rust_decimal_macros::dec; use sea_orm::{entity::*, query::*, FromQueryResult}; pub mod common; @@ -7,6 +10,7 @@ pub use common::{bakery_chain::*, setup::*, TestContext}; // cargo test --test realtional_tests -- --nocapture async fn main() { test_left_join().await; + test_right_join().await; } pub async fn test_left_join() { @@ -77,3 +81,82 @@ pub async fn test_left_join() { ctx.delete().await; } + +pub async fn test_right_join() { + let ctx = TestContext::new("mysql://root:@localhost", "test_right_join").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 _customer_jim = customer::ActiveModel { + name: Set("Jim".to_owned()), + ..Default::default() + } + .save(&ctx.db) + .await + .expect("could not insert customer"); + + let _order = order::ActiveModel { + bakery_id: Set(Some(bakery.id.clone().unwrap())), + customer_id: Set(Some(customer_kate.id.clone().unwrap())), + total: Set(dec!(15.10)), + 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 select = order::Entity::find() + .right_join(customer::Entity) + .select_only() + .column(customer::Column::Name) + .column_as(order::Column::Total, "order_total") + .filter(customer::Column::Name.contains("Kate")); + + let result = select + .into_model::() + .one(&ctx.db) + .await + .unwrap() + .unwrap(); + assert_eq!(result.order_total, Some(dec!(15.10))); + + let select = order::Entity::find() + .right_join(customer::Entity) + .select_only() + .column(customer::Column::Name) + .column_as(order::Column::Total, "order_total") + .filter(customer::Column::Name.contains("Jim")); + + let result = select + .into_model::() + .one(&ctx.db) + .await + .unwrap() + .unwrap(); + assert_eq!(result.order_total, None); + + ctx.delete().await; +}