Test right_join

This commit is contained in:
Sam Samai 2021-07-15 10:52:36 +10:00
parent 85f512d412
commit c1eddc4887
5 changed files with 88 additions and 5 deletions

View File

@ -13,7 +13,7 @@ impl EntityName for Entity {
pub struct Model {
pub id: i32,
pub name: String,
pub notes: String,
pub notes: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]

View File

@ -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)

View File

@ -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)

View File

@ -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()));
}

View File

@ -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<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;
}