Skip to main content

tank_tests/
decimals.rs

1#![allow(unused_imports)]
2use crate::silent_logs;
3use tank::{Entity, Executor, FixedDecimal, Result, expr, stream::TryStreamExt};
4
5#[derive(Entity, Debug, PartialEq)]
6#[tank(schema = "testing", name = "high_scale_decimals")]
7pub struct HighScaleDecimal {
8    #[tank(primary_key)]
9    pub id: i32,
10    pub value: FixedDecimal<38, 30>,
11}
12
13pub async fn decimals(executor: &mut impl Executor) -> Result<()> {
14    silent_logs! {
15        HighScaleDecimal::drop_table(executor, true, false).await?;
16    }
17    HighScaleDecimal::create_table(executor, true, false).await?;
18    let entity = HighScaleDecimal {
19        id: 1,
20        value: FixedDecimal::from("1.23".parse::<rust_decimal::Decimal>().unwrap()),
21    };
22    entity.save(executor).await?;
23
24    let loaded = HighScaleDecimal::find_one(executor, entity.primary_key_expr())
25        .await?
26        .expect("The inserted high-scale decimal should be found");
27    assert_eq!(
28        loaded, entity,
29        "The high-scale decimal changed on round-trip"
30    );
31    assert_eq!(
32        loaded.value.0.normalize(),
33        entity.value.0.normalize(),
34        "The high-scale decimal value changed on round-trip"
35    );
36    Ok(())
37}