Test right_join
This commit is contained in:
parent
85f512d412
commit
c1eddc4887
@ -13,7 +13,7 @@ impl EntityName for Entity {
|
|||||||
pub struct Model {
|
pub struct Model {
|
||||||
pub id: i32,
|
pub id: i32,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub notes: String,
|
pub notes: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||||
|
@ -53,7 +53,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
|
|||||||
// Customer
|
// Customer
|
||||||
let customer_kate = customer::ActiveModel {
|
let customer_kate = customer::ActiveModel {
|
||||||
name: Set("Kate".to_owned()),
|
name: Set("Kate".to_owned()),
|
||||||
notes: Set("Loves cheese cake".to_owned()),
|
notes: Set(Some("Loves cheese cake".to_owned())),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let customer_insert_res: InsertResult = Customer::insert(customer_kate)
|
let customer_insert_res: InsertResult = Customer::insert(customer_kate)
|
||||||
|
@ -53,7 +53,7 @@ pub async fn test_create_order(db: &DbConn) {
|
|||||||
// Customer
|
// Customer
|
||||||
let customer_kate = customer::ActiveModel {
|
let customer_kate = customer::ActiveModel {
|
||||||
name: Set("Kate".to_owned()),
|
name: Set("Kate".to_owned()),
|
||||||
notes: Set("Loves cheese cake".to_owned()),
|
notes: Set(Some("Loves cheese cake".to_owned())),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let customer_insert_res: InsertResult = Customer::insert(customer_kate)
|
let customer_insert_res: InsertResult = Customer::insert(customer_kate)
|
||||||
|
@ -88,7 +88,7 @@ pub async fn test_create_baker(db: &DbConn) {
|
|||||||
pub async fn test_create_customer(db: &DbConn) {
|
pub async fn test_create_customer(db: &DbConn) {
|
||||||
let customer_kate = customer::ActiveModel {
|
let customer_kate = customer::ActiveModel {
|
||||||
name: Set("Kate".to_owned()),
|
name: Set("Kate".to_owned()),
|
||||||
notes: Set("Loves cheese cake".to_owned()),
|
notes: Set(Some("Loves cheese cake".to_owned())),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let res: InsertResult = Customer::insert(customer_kate)
|
let res: InsertResult = Customer::insert(customer_kate)
|
||||||
@ -104,5 +104,5 @@ pub async fn test_create_customer(db: &DbConn) {
|
|||||||
assert!(customer.is_some());
|
assert!(customer.is_some());
|
||||||
let customer_model = customer.unwrap();
|
let customer_model = customer.unwrap();
|
||||||
assert_eq!(customer_model.name, "Kate");
|
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()));
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
|
use chrono::offset::Utc;
|
||||||
|
use rust_decimal::prelude::*;
|
||||||
|
use rust_decimal_macros::dec;
|
||||||
use sea_orm::{entity::*, query::*, FromQueryResult};
|
use sea_orm::{entity::*, query::*, FromQueryResult};
|
||||||
|
|
||||||
pub mod common;
|
pub mod common;
|
||||||
@ -7,6 +10,7 @@ pub use common::{bakery_chain::*, setup::*, TestContext};
|
|||||||
// cargo test --test realtional_tests -- --nocapture
|
// cargo test --test realtional_tests -- --nocapture
|
||||||
async fn main() {
|
async fn main() {
|
||||||
test_left_join().await;
|
test_left_join().await;
|
||||||
|
test_right_join().await;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn test_left_join() {
|
pub async fn test_left_join() {
|
||||||
@ -77,3 +81,82 @@ pub async fn test_left_join() {
|
|||||||
|
|
||||||
ctx.delete().await;
|
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<Decimal>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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::<SelectResult>()
|
||||||
|
.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::<SelectResult>()
|
||||||
|
.one(&ctx.db)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(result.order_total, None);
|
||||||
|
|
||||||
|
ctx.delete().await;
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user