| 320 | |
| 321 | #[sea_orm_macros::test] |
| 322 | pub async fn transaction_with_active_model_behaviour() -> Result<(), DbErr> { |
| 323 | let ctx = TestContext::new("transaction_with_active_model_behaviour_test").await; |
| 324 | create_tables(&ctx.db).await?; |
| 325 | |
| 326 | if let Ok(txn) = ctx.db.begin().await { |
| 327 | assert_eq!( |
| 328 | cake::ActiveModel { |
| 329 | name: Set("Cake with invalid price".to_owned()), |
| 330 | price: Set(rust_dec(0)), |
| 331 | gluten_free: Set(false), |
| 332 | ..Default::default() |
| 333 | } |
| 334 | .save(&txn) |
| 335 | .await, |
| 336 | Err(DbErr::Custom( |
| 337 | "[before_save] Invalid Price, insert: true".to_owned() |
| 338 | )) |
| 339 | ); |
| 340 | |
| 341 | assert_eq!(cake::Entity::find().all(&txn).await?.len(), 0); |
| 342 | |
| 343 | assert_eq!( |
| 344 | cake::ActiveModel { |
| 345 | name: Set("Cake with invalid price".to_owned()), |
| 346 | price: Set(rust_dec(-10)), |
| 347 | gluten_free: Set(false), |
| 348 | ..Default::default() |
| 349 | } |
| 350 | .save(&txn) |
| 351 | .await, |
| 352 | Err(DbErr::Custom( |
| 353 | "[after_save] Invalid Price, insert: true".to_owned() |
| 354 | )) |
| 355 | ); |
| 356 | |
| 357 | assert_eq!(cake::Entity::find().all(&txn).await?.len(), 1); |
| 358 | |
| 359 | let readonly_cake_1 = cake::ActiveModel { |
| 360 | name: Set("Readonly cake (err_on_before_delete)".to_owned()), |
| 361 | price: Set(rust_dec(10)), |
| 362 | gluten_free: Set(true), |
| 363 | ..Default::default() |
| 364 | } |
| 365 | .save(&txn) |
| 366 | .await?; |
| 367 | |
| 368 | assert_eq!(cake::Entity::find().all(&txn).await?.len(), 2); |
| 369 | |
| 370 | assert_eq!( |
| 371 | readonly_cake_1.delete(&txn).await.err(), |
| 372 | Some(DbErr::Custom( |
| 373 | "[before_delete] Cannot be deleted".to_owned() |
| 374 | )) |
| 375 | ); |
| 376 | |
| 377 | assert_eq!(cake::Entity::find().all(&txn).await?.len(), 2); |
| 378 | |
| 379 | let readonly_cake_2 = cake::ActiveModel { |