| 138 | |
| 139 | #[sea_orm_macros::test] |
| 140 | pub async fn transaction_begin_commit() -> Result<(), DbErr> { |
| 141 | let ctx = TestContext::new("transaction_begin_commit_test").await; |
| 142 | create_tables(&ctx.db).await?; |
| 143 | |
| 144 | assert_eq!(bakery::Entity::find().all(&ctx.db).await?.len(), 0); |
| 145 | |
| 146 | { |
| 147 | // Transaction begin in this scope |
| 148 | let txn = ctx.db.begin().await?; |
| 149 | |
| 150 | bakery::ActiveModel { |
| 151 | name: Set("SeaSide Bakery".to_owned()), |
| 152 | profit_margin: Set(10.4), |
| 153 | ..Default::default() |
| 154 | } |
| 155 | .save(&txn) |
| 156 | .await?; |
| 157 | |
| 158 | assert_eq!(bakery::Entity::find().all(&txn).await?.len(), 1); |
| 159 | |
| 160 | bakery::ActiveModel { |
| 161 | name: Set("Top Bakery".to_owned()), |
| 162 | profit_margin: Set(15.0), |
| 163 | ..Default::default() |
| 164 | } |
| 165 | .save(&txn) |
| 166 | .await?; |
| 167 | |
| 168 | assert_eq!(bakery::Entity::find().all(&txn).await?.len(), 2); |
| 169 | |
| 170 | // Commit changes before the end of scope |
| 171 | txn.commit().await?; |
| 172 | } |
| 173 | |
| 174 | assert_eq!(bakery::Entity::find().all(&ctx.db).await?.len(), 2); |
| 175 | |
| 176 | ctx.delete().await; |
| 177 | Ok(()) |
| 178 | } |
| 179 | |
| 180 | #[sea_orm_macros::test] |
| 181 | pub async fn transaction_begin_rollback() -> Result<(), DbErr> { |