sea-orm-sync 2.0.0-rc.40

🐚 The sync version of SeaORM
Documentation
#![allow(unused_imports, dead_code)]

pub mod common;

use common::{TestContext, features::*, setup::*};
use pretty_assertions::assert_eq;
use sea_orm::{DatabaseConnection, entity::prelude::*, entity::*};
use std::str::FromStr;

#[sea_orm_macros::test]
#[cfg(feature = "with-bigdecimal")]
fn main() -> Result<(), DbErr> {
    let ctx = TestContext::new("pi_tests");
    create_pi_table(&ctx.db)?;
    create_and_update_pi(&ctx.db)?;
    ctx.delete();

    Ok(())
}

#[cfg(feature = "with-bigdecimal")]
pub fn create_and_update_pi(db: &DatabaseConnection) -> Result<(), DbErr> {
    use pi::Entity as Pi;

    fn trunc_dec_scale(mut model: pi::Model) -> pi::Model {
        model.decimal = model.decimal.trunc_with_scale(3);
        model.big_decimal = model.big_decimal.with_scale(3);
        model.decimal_opt = model.decimal_opt.map(|decimal| decimal.trunc_with_scale(3));
        model.big_decimal_opt = model
            .big_decimal_opt
            .map(|big_decimal| big_decimal.with_scale(3));
        model
    }

    let pi = trunc_dec_scale(pi::Model {
        id: 1,
        decimal: rust_dec(3.1415926536),
        big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),
        decimal_opt: None,
        big_decimal_opt: None,
    });

    let res = trunc_dec_scale(pi.clone().into_active_model().insert(db)?);

    let model = trunc_dec_scale(Pi::find().one(db)?.unwrap());
    assert_eq!(model, res);
    assert_eq!(model, pi.clone());

    let res = trunc_dec_scale(
        pi::ActiveModel {
            decimal_opt: Set(Some(rust_dec(3.1415926536))),
            big_decimal_opt: Set(Some(BigDecimal::from_str("3.1415926536").unwrap())),
            ..pi.clone().into_active_model()
        }
        .update(db)?,
    );

    let model = trunc_dec_scale(Pi::find().one(db)?.unwrap());
    assert_eq!(model, res);
    assert_eq!(
        model,
        trunc_dec_scale(pi::Model {
            id: 1,
            decimal: rust_dec(3.1415926536),
            big_decimal: BigDecimal::from_str("3.1415926536").unwrap(),
            decimal_opt: Some(rust_dec(3.1415926536)),
            big_decimal_opt: Some(BigDecimal::from_str("3.1415926536").unwrap()),
        })
    );

    Ok(())
}