(db: &DatabaseConnection)
| 18 | } |
| 19 | |
| 20 | pub async fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr> { |
| 21 | fn trunc_dec_scale(mut model: pi::Model) -> pi::Model { |
| 22 | model.decimal = model.decimal.trunc_with_scale(3); |
| 23 | model.big_decimal = model.big_decimal.with_scale(3); |
| 24 | model.decimal_opt = model.decimal_opt.map(|decimal| decimal.trunc_with_scale(3)); |
| 25 | model.big_decimal_opt = model |
| 26 | .big_decimal_opt |
| 27 | .map(|big_decimal| big_decimal.with_scale(3)); |
| 28 | model |
| 29 | } |
| 30 | |
| 31 | let pi = trunc_dec_scale(pi::Model { |
| 32 | id: 1, |
| 33 | decimal: rust_dec(3.1415926536), |
| 34 | big_decimal: BigDecimal::from_str("3.1415926536").unwrap(), |
| 35 | decimal_opt: None, |
| 36 | big_decimal_opt: None, |
| 37 | }); |
| 38 | |
| 39 | let res = trunc_dec_scale(pi.clone().into_active_model().insert(db).await?); |
| 40 | |
| 41 | let model = trunc_dec_scale(Pi::find().one(db).await?.unwrap()); |
| 42 | assert_eq!(model, res); |
| 43 | assert_eq!(model, pi.clone()); |
| 44 | |
| 45 | let res = trunc_dec_scale( |
| 46 | pi::ActiveModel { |
| 47 | decimal_opt: Set(Some(rust_dec(3.1415926536))), |
| 48 | big_decimal_opt: Set(Some(BigDecimal::from_str("3.1415926536").unwrap())), |
| 49 | ..pi.clone().into_active_model() |
| 50 | } |
| 51 | .update(db) |
| 52 | .await?, |
| 53 | ); |
| 54 | |
| 55 | let model = trunc_dec_scale(Pi::find().one(db).await?.unwrap()); |
| 56 | assert_eq!(model, res); |
| 57 | assert_eq!( |
| 58 | model, |
| 59 | trunc_dec_scale(pi::Model { |
| 60 | id: 1, |
| 61 | decimal: rust_dec(3.1415926536), |
| 62 | big_decimal: BigDecimal::from_str("3.1415926536").unwrap(), |
| 63 | decimal_opt: Some(rust_dec(3.1415926536)), |
| 64 | big_decimal_opt: Some(BigDecimal::from_str("3.1415926536").unwrap()), |
| 65 | }) |
| 66 | ); |
| 67 | |
| 68 | Ok(()) |
| 69 | } |
no test coverage detected