Skip to content

Commit

Permalink
Test create order
Browse files Browse the repository at this point in the history
  • Loading branch information
samsamai committed Jul 8, 2021
1 parent 1ecc848 commit 00e4f32
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tests/bakery_chain/order.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rust_decimal::prelude::*;
use sea_orm::entity::prelude::*;

#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
Expand All @@ -12,7 +13,7 @@ impl EntityName for Entity {
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel)]
pub struct Model {
pub id: i32,
pub total: f32,
pub total: Decimal,
pub bakery_id: Option<i32>,
pub customer_id: Option<i32>,
pub placed_at: String,
Expand Down
1 change: 1 addition & 0 deletions tests/bakery_chain_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ async fn create_entities(db: &DbConn) {
crud::test_create_customer(db).await;
crud::test_create_cake(db).await;
crud::create_lineitem::test_create_lineitem(db).await;
crud::create_order::test_create_order(db).await;
}
122 changes: 122 additions & 0 deletions tests/crud/create_order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
pub use super::*;
use rust_decimal_macros::dec;

pub async fn test_create_order(db: &DbConn) {
// Bakery
let seaside_bakery = bakery::ActiveModel {
name: Set("SeaSide Bakery".to_owned()),
profit_margin: Set(10.4),
..Default::default()
};
let bakery_insert_res: InsertResult = Bakery::insert(seaside_bakery)
.exec(db)
.await
.expect("could not insert bakery");

// Baker
let baker_bob = baker::ActiveModel {
name: Set("Baker Bob".to_owned()),
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
..Default::default()
};
let baker_insert_res: InsertResult = Baker::insert(baker_bob)
.exec(db)
.await
.expect("could not insert baker");

// Cake
let mud_cake = cake::ActiveModel {
name: Set("Mud Cake".to_owned()),
price: Set(10.25),
gluten_free: Set(false),
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
..Default::default()
};

let cake_insert_res: InsertResult = Cake::insert(mud_cake)
.exec(db)
.await
.expect("could not insert cake");

// Cake_Baker
let cake_baker = cakes_bakers::ActiveModel {
cake_id: Set(cake_insert_res.last_insert_id as i32),
baker_id: Set(baker_insert_res.last_insert_id as i32),
..Default::default()
};
let _cake_baker_res: InsertResult = CakesBakers::insert(cake_baker)
.exec(db)
.await
.expect("could not insert cake_baker");

// Customer
let customer_kate = customer::ActiveModel {
name: Set("Kate".to_owned()),
notes: Set("Loves cheese cake".to_owned()),
..Default::default()
};
let customer_insert_res: InsertResult = Customer::insert(customer_kate)
.exec(db)
.await
.expect("could not insert customer");

// Order
let order_1 = order::ActiveModel {
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
customer_id: Set(Some(customer_insert_res.last_insert_id as i32)),
total: Set(dec!(15.10)),
placed_at: Set("placeholder".to_string()),
..Default::default()
};
let order_insert_res: InsertResult = Order::insert(order_1)
.exec(db)
.await
.expect("could not insert order");

// Lineitem
let lineitem_1 = lineitem::ActiveModel {
cake_id: Set(Some(cake_insert_res.last_insert_id as i32)),
order_id: Set(Some(order_insert_res.last_insert_id as i32)),
price: Set(dec!(7.55)),
quantity: Set(2),
..Default::default()
};
let _lineitem_insert_res: InsertResult = Lineitem::insert(lineitem_1)
.exec(db)
.await
.expect("could not insert lineitem");

let order: Option<order::Model> = Order::find_by_id(order_insert_res.last_insert_id)
.one(db)
.await
.expect("could not find order");

assert!(order.is_some());
let order_model = order.unwrap();
assert_eq!(order_model.total, dec!(15.10));

let customer: Option<customer::Model> = Customer::find_by_id(order_model.customer_id)
.one(db)
.await
.expect("could not find customer");

let customer_model = customer.unwrap();
assert_eq!(customer_model.name, "Kate");

let bakery: Option<bakery::Model> = Bakery::find_by_id(order_model.bakery_id)
.one(db)
.await
.expect("could not find bakery");

let bakery_model = bakery.unwrap();
assert_eq!(bakery_model.name, "SeaSide Bakery");

let related_lineitems: Vec<lineitem::Model> = order_model
.find_related(Lineitem)
.all(db)
.await
.expect("could not find related lineitems");
assert_eq!(related_lineitems.len(), 1);
assert_eq!(related_lineitems[0].price, dec!(7.55));
assert_eq!(related_lineitems[0].quantity, 2);
}
1 change: 1 addition & 0 deletions tests/crud/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use sea_orm::{entity::*, DbConn, InsertResult};
pub use super::bakery_chain::*;

pub mod create_lineitem;
pub mod create_order;

pub async fn test_create_bakery(db: &DbConn) {
let seaside_bakery = bakery::ActiveModel {
Expand Down

0 comments on commit 00e4f32

Please sign in to comment.