diff --git a/tests/bakery_chain_tests.rs b/tests/bakery_chain_tests.rs index c83dc8dcb..a51848e59 100644 --- a/tests/bakery_chain_tests.rs +++ b/tests/bakery_chain_tests.rs @@ -28,4 +28,5 @@ async fn create_entities(db: &DbConn) { crud::test_create_bakery(db).await; crud::test_create_baker(db).await; crud::test_create_customer(db).await; + crud::test_create_cake(db).await; } diff --git a/tests/crud/mod.rs b/tests/crud/mod.rs index 75fba65b0..e058993bf 100644 --- a/tests/crud/mod.rs +++ b/tests/crud/mod.rs @@ -100,3 +100,91 @@ pub async fn test_create_customer(db: &DbConn) { assert_eq!(customer_model.name, "Kate"); assert_eq!(customer_model.notes, "Loves cheese cake"); } + +pub async fn test_create_cake(db: &DbConn) { + 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"); + + 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"); + + 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"); + + let cake: Option = Cake::find_by_id(cake_insert_res.last_insert_id) + .one(db) + .await + .expect("could not find cake"); + + 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"); + + assert!(cake.is_some()); + let cake_model = cake.unwrap(); + assert_eq!(cake_model.name, "Mud Cake"); + assert_eq!(cake_model.price, 10.25); + assert_eq!(cake_model.gluten_free, false); + assert_eq!( + cake_model + .find_related(Bakery) + .one(db) + .await + .expect("Bakery not found") + .unwrap() + .name, + "SeaSide Bakery" + ); + + let related_bakers: Vec = cake_model + .find_related(Baker) + .all(db) + .await + .expect("could not find related bakers"); + assert_eq!(related_bakers.len(), 1); + assert_eq!(related_bakers[0].name, "Baker Bob"); + + let baker: Option = Baker::find_by_id(baker_insert_res.last_insert_id) + .one(db) + .await + .expect("could not find baker"); + + let related_cakes: Vec = baker + .unwrap() + .find_related(Cake) + .all(db) + .await + .expect("could not find related cakes"); + assert_eq!(related_cakes.len(), 1); + assert_eq!(related_cakes[0].name, "Mud Cake") +}