toasty_driver_integration_suite/tests/
bigdecimal.rs1use crate::prelude::*;
2
3use bigdecimal::BigDecimal;
4use std::str::FromStr;
5
6#[driver_test(id(ID))]
7pub async fn ty_bigdecimal(test: &mut Test) -> Result<(), BoxError> {
8 #[derive(Debug, toasty::Model)]
9 struct Item {
10 #[key]
11 #[auto]
12 id: ID,
13 val: BigDecimal,
14 }
15
16 let mut db = test.setup_db(models!(Item)).await;
17
18 let test_values = vec![
19 BigDecimal::from_str("0")?,
20 BigDecimal::from_str("1")?,
21 BigDecimal::from_str("-1")?,
22 BigDecimal::from_str("123.456")?,
23 BigDecimal::from_str("-123.456")?,
24 BigDecimal::from_str("0.0000000001")?, BigDecimal::from_str("-0.0000000001")?, BigDecimal::from_str("99999999999999999999.99999999999999999999")?, BigDecimal::from_str("-99999999999999999999.99999999999999999999")?, BigDecimal::from_str("3.141592653589793238462643383279")?, ];
30
31 for val in &test_values {
32 let created = Item::create().val(val.clone()).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_bigdecimal_as_text(test: &mut Test) -> Result<(), BoxError> {
41 #[derive(Debug, toasty::Model)]
42 struct Item {
43 #[key]
44 #[auto]
45 id: ID,
46
47 #[column(type = text)]
48 val: BigDecimal,
49 }
50
51 let mut db = test.setup_db(models!(Item)).await;
52
53 let test_values = vec![
54 BigDecimal::from_str("0")?,
55 BigDecimal::from_str("123.456")?,
56 BigDecimal::from_str("-123.456")?,
57 BigDecimal::from_str("99999999999999999999.99999999999999999999")?,
58 ];
59
60 for val in &test_values {
61 let created = Item::create().val(val.clone()).exec(&mut db).await?;
62 let read = Item::get_by_id(&mut db, &created.id).await?;
63 assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
64 }
65 Ok(())
66}
67
68#[driver_test(
69 id(ID),
70 requires(and(bigdecimal_implemented, decimal_arbitrary_precision))
71)]
72pub async fn ty_bigdecimal_as_numeric_arbitrary_precision(test: &mut Test) -> Result<(), BoxError> {
73 #[derive(Debug, toasty::Model)]
74 struct Item {
75 #[key]
76 #[auto]
77 id: ID,
78 #[column(type = numeric)]
79 val: BigDecimal,
80 }
81
82 let mut db = test.setup_db(models!(Item)).await;
83
84 let test_values = vec![
85 BigDecimal::from_str("0")?,
86 BigDecimal::from_str("123.456")?,
87 BigDecimal::from_str("-123.456")?,
88 BigDecimal::from_str("99999999999999999999.99999999999999999999")?,
89 BigDecimal::from_str("3.141592653589793238462643383279")?,
90 ];
91
92 for val in &test_values {
93 let created = Item::create().val(val.clone()).exec(&mut db).await?;
94 let read = Item::get_by_id(&mut db, &created.id).await?;
95 assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
96 }
97 Ok(())
98}
99
100#[driver_test(id(ID), requires(and(native_decimal, bigdecimal_implemented)))]
101pub async fn ty_bigdecimal_as_numeric_fixed_precision(test: &mut Test) -> Result<(), BoxError> {
102 #[derive(Debug, toasty::Model)]
103 struct Item {
104 #[key]
105 #[auto]
106 id: ID,
107 #[column(type = numeric(38, 20))]
108 val: BigDecimal,
109 }
110
111 let mut db = test.setup_db(models!(Item)).await;
112
113 let test_values = vec![
114 BigDecimal::from_str("0")?,
115 BigDecimal::from_str("123.456")?,
116 BigDecimal::from_str("-123.456")?,
117 BigDecimal::from_str("123456789012345678.12345678901234567890")?, BigDecimal::from_str("3.14159265358979323846")?,
119 ];
120
121 for val in &test_values {
122 let created = Item::create().val(val.clone()).exec(&mut db).await?;
123 let read = Item::get_by_id(&mut db, &created.id).await?;
124 assert_eq!(read.val, *val, "Round-trip failed for: {}", val);
125 }
126 Ok(())
127}