toasty_driver_integration_suite/tests/
decimal.rs

1use crate::prelude::*;
2
3use rust_decimal::Decimal;
4use std::str::FromStr;
5
6#[driver_test(id(ID))]
7pub async fn ty_decimal(test: &mut Test) -> Result<(), BoxError> {
8    #[derive(Debug, toasty::Model)]
9    struct Item {
10        #[key]
11        #[auto]
12        id: ID,
13        val: Decimal,
14    }
15
16    let mut db = test.setup_db(models!(Item)).await;
17
18    let test_values = vec![
19        Decimal::from_str("0")?,
20        Decimal::from_str("1")?,
21        Decimal::from_str("-1")?,
22        Decimal::from_str("123.456")?,
23        Decimal::from_str("-123.456")?,
24        Decimal::from_str("0.0000000001")?,  // Small positive
25        Decimal::from_str("-0.0000000001")?, // Small negative
26        Decimal::from_str("99999999999999999999.99999999")?, // Large with precision
27        Decimal::from_str("-99999999999999999999.99999999")?, // Large negative with precision
28        Decimal::from_str("3.141592653589793238")?, // Pi approximation
29    ];
30
31    for val in &test_values {
32        let created = Item::create().val(*val).exec(&mut db).await?;
33        let read = Item::get_by_id(&mut db, &created.id).await?;
34        assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
35    }
36    Ok(())
37}
38
39#[driver_test(id(ID))]
40pub async fn ty_decimal_as_text(test: &mut Test) -> Result<(), BoxError> {
41    use rust_decimal::Decimal;
42    use std::str::FromStr;
43
44    #[derive(Debug, toasty::Model)]
45    struct Item {
46        #[key]
47        #[auto]
48        id: ID,
49        #[column(type = text)]
50        val: Decimal,
51    }
52
53    let mut db = test.setup_db(models!(Item)).await;
54
55    let test_values = vec![
56        Decimal::from_str("0")?,
57        Decimal::from_str("123.456")?,
58        Decimal::from_str("-123.456")?,
59        Decimal::from_str("99999999999999999999.99999999")?,
60    ];
61
62    for val in &test_values {
63        let created = Item::create().val(*val).exec(&mut db).await?;
64        let read = Item::get_by_id(&mut db, &created.id).await?;
65        assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
66    }
67    Ok(())
68}
69
70#[driver_test(id(ID), requires(decimal_arbitrary_precision))]
71pub async fn ty_decimal_as_numeric_arbitrary_precision(test: &mut Test) -> Result<(), BoxError> {
72    #[derive(Debug, toasty::Model)]
73    struct Item {
74        #[key]
75        #[auto]
76        id: ID,
77        #[column(type = numeric)]
78        val: Decimal,
79    }
80
81    let mut db = test.setup_db(models!(Item)).await;
82
83    let test_values = vec![
84        Decimal::from_str("0")?,
85        Decimal::from_str("123.456")?,
86        Decimal::from_str("-123.456")?,
87        Decimal::from_str("99999999999999999999.99999999")?,
88        Decimal::from_str("3.141592653589793238")?,
89    ];
90
91    for val in &test_values {
92        let created = Item::create().val(*val).exec(&mut db).await?;
93        let read = Item::get_by_id(&mut db, &created.id).await?;
94        assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
95    }
96    Ok(())
97}
98
99#[driver_test(id(ID), requires(native_decimal))]
100pub async fn ty_decimal_as_numeric_fixed_precision(test: &mut Test) -> Result<(), BoxError> {
101    #[derive(Debug, toasty::Model)]
102    struct Item {
103        #[key]
104        #[auto]
105        id: ID,
106        #[column(type = numeric(28, 10))]
107        val: Decimal,
108    }
109
110    let mut db = test.setup_db(models!(Item)).await;
111
112    let test_values = vec![
113        Decimal::from_str("0")?,
114        Decimal::from_str("123.456")?,
115        Decimal::from_str("-123.456")?,
116        Decimal::from_str("123456789012345678.1234567890")?, // Within precision(28,10)
117        Decimal::from_str("3.1415926535")?,
118    ];
119
120    for val in &test_values {
121        let created = Item::create().val(*val).exec(&mut db).await?;
122        let read = Item::get_by_id(&mut db, &created.id).await?;
123        assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
124    }
125    Ok(())
126}