| 179 | |
| 180 | #[sea_orm_macros::test] |
| 181 | pub async fn transaction_begin_rollback() -> Result<(), DbErr> { |
| 182 | let ctx = TestContext::new("transaction_begin_rollback_test").await; |
| 183 | create_tables(&ctx.db).await?; |
| 184 | |
| 185 | assert_eq!(bakery::Entity::find().all(&ctx.db).await?.len(), 0); |
| 186 | |
| 187 | { |
| 188 | // Transaction begin in this scope |
| 189 | let txn = ctx.db.begin().await?; |
| 190 | |
| 191 | bakery::ActiveModel { |
| 192 | name: Set("SeaSide Bakery".to_owned()), |
| 193 | profit_margin: Set(10.4), |
| 194 | ..Default::default() |
| 195 | } |
| 196 | .save(&txn) |
| 197 | .await?; |
| 198 | |
| 199 | assert_eq!(bakery::Entity::find().all(&txn).await?.len(), 1); |
| 200 | |
| 201 | bakery::ActiveModel { |
| 202 | name: Set("Top Bakery".to_owned()), |
| 203 | profit_margin: Set(15.0), |
| 204 | ..Default::default() |
| 205 | } |
| 206 | .save(&txn) |
| 207 | .await?; |
| 208 | |
| 209 | assert_eq!(bakery::Entity::find().all(&txn).await?.len(), 2); |
| 210 | |
| 211 | // Rollback changes before the end of scope |
| 212 | txn.rollback().await?; |
| 213 | } |
| 214 | |
| 215 | assert_eq!(bakery::Entity::find().all(&ctx.db).await?.len(), 0); |
| 216 | |
| 217 | ctx.delete().await; |
| 218 | Ok(()) |
| 219 | } |
| 220 | |
| 221 | #[sea_orm_macros::test] |
| 222 | pub async fn transaction_closure_commit() -> Result<(), DbErr> { |