|
| 1 | +#![allow(unused_imports, dead_code)] |
| 2 | + |
| 3 | +pub mod common; |
| 4 | + |
| 5 | +pub use common::{TestContext, features::*, setup::*}; |
| 6 | +use pretty_assertions::assert_eq; |
| 7 | +use sea_orm::{DatabaseConnection, entity::prelude::*, entity::*}; |
| 8 | +use serde_json::json; |
| 9 | + |
| 10 | +#[sea_orm_macros::test] |
| 11 | +async fn main() -> Result<(), DbErr> { |
| 12 | + let ctx = TestContext::new("update_without_returning_tests").await; |
| 13 | + create_repository_table(&ctx.db).await?; |
| 14 | + create_edit_log_table(&ctx.db).await?; |
| 15 | + update_without_returning(&ctx.db).await?; |
| 16 | + update_without_returning_record_not_updated(&ctx.db).await?; |
| 17 | + ctx.delete().await; |
| 18 | + |
| 19 | + Ok(()) |
| 20 | +} |
| 21 | + |
| 22 | +// `update_without_returning` should update the row, run `before_save`, and |
| 23 | +// intentionally skip `after_save`. |
| 24 | +pub async fn update_without_returning(db: &DatabaseConnection) -> Result<(), DbErr> { |
| 25 | + let model = repository::Model { |
| 26 | + id: "uwr-001".to_owned(), |
| 27 | + owner: "GC".to_owned(), |
| 28 | + name: "G.C.".to_owned(), |
| 29 | + description: None, |
| 30 | + }; |
| 31 | + |
| 32 | + // Instance insert runs `before_save` + `after_save` (edit_log id 1 and 2). |
| 33 | + model.clone().into_active_model().insert(db).await?; |
| 34 | + |
| 35 | + let updated = repository::ActiveModel { |
| 36 | + description: Set(Some("updated".to_owned())), |
| 37 | + ..model.clone().into_active_model() |
| 38 | + }; |
| 39 | + |
| 40 | + let res = updated.update_without_returning(db).await?; |
| 41 | + assert_eq!(res.rows_affected, 1); |
| 42 | + |
| 43 | + // The row is actually updated. |
| 44 | + assert_eq!( |
| 45 | + Repository::find_by_id("uwr-001".to_owned()).one(db).await?, |
| 46 | + Some(repository::Model { |
| 47 | + description: Some("updated".to_owned()), |
| 48 | + ..model |
| 49 | + }) |
| 50 | + ); |
| 51 | + |
| 52 | + // `before_save` ran for the update (id 3), but `after_save` did NOT. |
| 53 | + assert_eq!( |
| 54 | + edit_log::Entity::find().all(db).await?, |
| 55 | + [ |
| 56 | + edit_log::Model { |
| 57 | + id: 1, |
| 58 | + action: "before_save".into(), |
| 59 | + values: json!({ |
| 60 | + "description": null, |
| 61 | + "id": "uwr-001", |
| 62 | + "name": "G.C.", |
| 63 | + "owner": "GC", |
| 64 | + }), |
| 65 | + }, |
| 66 | + edit_log::Model { |
| 67 | + id: 2, |
| 68 | + action: "after_save".into(), |
| 69 | + values: json!({ |
| 70 | + "description": null, |
| 71 | + "id": "uwr-001", |
| 72 | + "name": "G.C.", |
| 73 | + "owner": "GC", |
| 74 | + }), |
| 75 | + }, |
| 76 | + edit_log::Model { |
| 77 | + id: 3, |
| 78 | + action: "before_save".into(), |
| 79 | + values: json!({ |
| 80 | + "description": "updated", |
| 81 | + "id": "uwr-001", |
| 82 | + "name": "G.C.", |
| 83 | + "owner": "GC", |
| 84 | + }), |
| 85 | + }, |
| 86 | + ] |
| 87 | + ); |
| 88 | + |
| 89 | + Ok(()) |
| 90 | +} |
| 91 | + |
| 92 | +// Updating a row that does not exist returns `RecordNotUpdated`. |
| 93 | +pub async fn update_without_returning_record_not_updated( |
| 94 | + db: &DatabaseConnection, |
| 95 | +) -> Result<(), DbErr> { |
| 96 | + let missing = repository::ActiveModel { |
| 97 | + id: Set("does-not-exist".to_owned()), |
| 98 | + owner: Set("GC".to_owned()), |
| 99 | + name: Set("G.C.".to_owned()), |
| 100 | + description: Set(Some("nope".to_owned())), |
| 101 | + }; |
| 102 | + |
| 103 | + let res = missing.update_without_returning(db).await; |
| 104 | + assert_eq!(res.map(|_| ()), Err(DbErr::RecordNotUpdated)); |
| 105 | + |
| 106 | + Ok(()) |
| 107 | +} |
0 commit comments