tank_tests/
simple.rs

1#![allow(unused_imports)]
2use rust_decimal::{Decimal, prelude::FromPrimitive};
3use std::{
4    borrow::Cow,
5    cell::{Cell, RefCell},
6    i128,
7    pin::pin,
8    sync::{Arc, LazyLock},
9};
10use tank::{
11    Driver, Entity, Executor, FixedDecimal, Query, QueryResult, RowsAffected, SqlWriter,
12    stream::TryStreamExt,
13};
14use time::{Date, Time, macros::date};
15use tokio::sync::Mutex;
16use uuid::Uuid;
17
18#[derive(Debug, Entity, PartialEq)]
19pub struct SimpleFields {
20    #[tank(primary_key)]
21    alpha: u8,
22    bravo: Option<i32>,
23    charlie: Option<i16>,
24    delta: Option<u64>,
25    echo: Option<Uuid>,
26    #[cfg(not(feature = "disable-large-integers"))]
27    foxtrot: Option<i128>,
28    golf: Option<Time>,
29    hotel: Option<Cow<'static, str>>,
30    india: Box<Option<char>>,
31    juliet: Option<bool>,
32    kilo: Option<u32>,
33    lima: Arc<Option<f32>>,
34    mike: Option<Date>,
35    november: Option<Cell<FixedDecimal<4, 2>>>,
36    oscar: Option<RefCell<FixedDecimal<8, 3>>>,
37    papa: Option<FixedDecimal<20, 1>>,
38}
39static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
40
41pub async fn simple<E: Executor>(executor: &mut E) {
42    let _lock = MUTEX.lock().await;
43
44    // Setup
45    SimpleFields::drop_table(executor, true, false)
46        .await
47        .expect("Failed to drop SimpleNullFields table");
48    SimpleFields::create_table(executor, true, true)
49        .await
50        .expect("Failed to create SimpleNullFields table");
51
52    // Simple 1
53    SimpleFields::delete_many(executor, &true)
54        .await
55        .expect("Failed to clear the SimpleNullFields table");
56    let entity = SimpleFields {
57        alpha: 1,
58        bravo: 777.into(),
59        charlie: (-2).into(),
60        delta: 9876543210.into(),
61        echo: None,
62        #[cfg(not(feature = "disable-large-integers"))]
63        foxtrot: i128::MAX.into(),
64        golf: Time::from_hms(12, 0, 10).unwrap().into(),
65        hotel: Some("Hello world!".into()),
66        india: Box::new(None),
67        juliet: true.into(),
68        kilo: None,
69        lima: Arc::new(Some(3.14)),
70        mike: None,
71        november: None,
72        oscar: None,
73        papa: Some(Decimal::from_f32(45.2).unwrap().into()),
74    };
75    entity
76        .save(executor)
77        .await
78        .expect("Failed to save simple 1");
79    let entity = SimpleFields::find_one(executor, &true)
80        .await
81        .expect("Failed to query simple 1")
82        .expect("Failed to find simple 1");
83    assert_eq!(entity.alpha, 1);
84    assert_eq!(entity.bravo, Some(777));
85    assert_eq!(entity.charlie, Some(-2));
86    assert_eq!(entity.delta, Some(9876543210));
87    assert_eq!(entity.echo, None);
88    #[cfg(not(feature = "disable-large-integers"))]
89    assert_eq!(
90        entity.foxtrot,
91        Some(170_141_183_460_469_231_731_687_303_715_884_105_727)
92    );
93    assert_eq!(entity.golf, Some(Time::from_hms(12, 0, 10).unwrap()));
94    assert_eq!(entity.hotel, Some("Hello world!".into()));
95    assert_eq!(*entity.india, None);
96    assert_eq!(entity.juliet, Some(true));
97    assert_eq!(entity.kilo, None);
98    assert_eq!(*entity.lima, Some(3.14));
99    assert_eq!(entity.mike, None);
100    assert_eq!(entity.november, None);
101    assert_eq!(entity.oscar, None);
102    assert_eq!(entity.papa, Some(Decimal::from_f32(45.2).unwrap().into()));
103
104    // Simple 1 - multiple statements
105    #[cfg(not(feature = "disable-multiple-statements"))]
106    {
107        let writer = executor.driver().sql_writer();
108        let mut query = String::new();
109        writer.write_delete::<SimpleFields>(&mut query, &false); // Does not delete anything
110        writer.write_select(
111            &mut query,
112            SimpleFields::columns(),
113            SimpleFields::table(),
114            &true,
115            None,
116        );
117        {
118            let mut stream = pin!(executor.run(query));
119            let result = stream
120                .try_next()
121                .await
122                .expect("Could not process the first query correctly");
123            let Some(QueryResult::Affected(RowsAffected { rows_affected, .. })) = result else {
124                panic!("Unexpected result type:\n{result:#?}");
125            };
126            if let Some(rows_affected) = rows_affected {
127                assert_eq!(rows_affected, 0);
128            }
129            let result = stream
130                .try_next()
131                .await
132                .expect("Could not process the second query correctly");
133            let Some(QueryResult::Row(row)) = result else {
134                panic!("Unexpected result type:\n{result:#?}");
135            };
136            let value =
137                SimpleFields::from_row(row).expect("Could not decode the row into SimpleFields");
138            assert_eq!(
139                value,
140                SimpleFields {
141                    alpha: 1,
142                    bravo: 777.into(),
143                    charlie: (-2).into(),
144                    delta: 9876543210.into(),
145                    echo: None,
146                    #[cfg(not(feature = "disable-large-integers"))]
147                    foxtrot: i128::MAX.into(),
148                    golf: Time::from_hms(12, 0, 10).unwrap().into(),
149                    hotel: Some("Hello world!".into()),
150                    india: Box::new(None),
151                    juliet: true.into(),
152                    kilo: None,
153                    lima: Arc::new(Some(3.14)),
154                    mike: None,
155                    november: None,
156                    oscar: None,
157                    papa: Some(Decimal::from_f32(45.2).unwrap().into()),
158                }
159            );
160        }
161    }
162
163    // Simple 2
164    SimpleFields::delete_many(executor, &true)
165        .await
166        .expect("Failed to clear the SimpleNullFields table");
167    let entity = SimpleFields {
168        alpha: 255,
169        bravo: None,
170        charlie: None,
171        delta: None,
172        echo: Some(Uuid::parse_str("5e915574-bb30-4430-98cf-c5854f61fbbd").unwrap()),
173        #[cfg(not(feature = "disable-large-integers"))]
174        foxtrot: None,
175        golf: None,
176        hotel: None,
177        india: Box::new(None),
178        juliet: None,
179        kilo: 4294967295.into(),
180        lima: Arc::new(None),
181        mike: date!(2025 - 09 - 07).into(),
182        november: Cell::new(Decimal::from_f32(1.5).unwrap().into()).into(),
183        oscar: RefCell::new(Decimal::from_f32(5080.6244).unwrap().into()).into(),
184        papa: None,
185    };
186    entity
187        .save(executor)
188        .await
189        .expect("Failed to save simple 2");
190    let entity = SimpleFields::find_one(executor, &true)
191        .await
192        .expect("Failed to query simple 2")
193        .expect("Failed to find simple 2");
194    assert_eq!(entity.alpha, 255);
195    assert_eq!(entity.bravo, None);
196    assert_eq!(entity.charlie, None);
197    assert_eq!(entity.delta, None);
198    assert_eq!(
199        entity.echo,
200        Some(Uuid::parse_str("5e915574-bb30-4430-98cf-c5854f61fbbd").unwrap())
201    );
202    #[cfg(not(feature = "disable-large-integers"))]
203    assert_eq!(entity.foxtrot, None);
204    assert_eq!(entity.golf, None);
205    assert_eq!(entity.hotel, None);
206    assert_eq!(*entity.india, None);
207    assert_eq!(entity.juliet, None);
208    assert_eq!(entity.kilo, Some(4294967295));
209    assert_eq!(*entity.lima, None);
210    assert_eq!(entity.mike, Some(date!(2025 - 09 - 07)));
211    assert_eq!(
212        entity.november,
213        Some(Cell::new(Decimal::from_f32(1.5).unwrap().into()))
214    );
215    assert_eq!(
216        entity.oscar,
217        Some(RefCell::new(Decimal::from_f32(5080.6244).unwrap().into()))
218    );
219    assert_eq!(entity.papa, None);
220
221    // Simple 2 - multiple statements
222    #[cfg(not(feature = "disable-multiple-statements"))]
223    {
224        let writer = executor.driver().sql_writer();
225        let mut query = String::new();
226        writer.write_delete::<SimpleFields>(&mut query, &true);
227        writer.write_insert(&mut query, [&entity], false);
228        writer.write_select(
229            &mut query,
230            SimpleFields::columns(),
231            SimpleFields::table(),
232            &true,
233            None,
234        );
235        {
236            let mut stream = pin!(executor.run(query));
237            let Ok(Some(QueryResult::Affected(RowsAffected { rows_affected, .. }))) =
238                stream.try_next().await
239            else {
240                panic!("Could not process the first query correctly")
241            };
242            if let Some(rows_affected) = rows_affected {
243                assert_eq!(rows_affected, 1);
244            }
245            let Ok(Some(QueryResult::Affected(RowsAffected { rows_affected, .. }))) =
246                stream.try_next().await
247            else {
248                panic!("Could not process the first query correctly")
249            };
250            if let Some(rows_affected) = rows_affected {
251                assert_eq!(rows_affected, 1);
252            }
253            let Ok(Some(QueryResult::Row(row))) = stream.try_next().await else {
254                panic!("Could not process the second query correctly")
255            };
256            let value =
257                SimpleFields::from_row(row).expect("Could not decode the row into SimpleFields");
258            assert_eq!(
259                value,
260                SimpleFields {
261                    alpha: 255,
262                    bravo: None,
263                    charlie: None,
264                    delta: None,
265                    echo: Some(Uuid::parse_str("5e915574-bb30-4430-98cf-c5854f61fbbd").unwrap()),
266                    #[cfg(not(feature = "disable-large-integers"))]
267                    foxtrot: None,
268                    golf: None,
269                    hotel: None,
270                    india: Box::new(None),
271                    juliet: None,
272                    kilo: 4294967295.into(),
273                    lima: Arc::new(None),
274                    mike: date!(2025 - 09 - 07).into(),
275                    november: Cell::new(Decimal::from_f32(1.5).unwrap().into()).into(),
276                    oscar: RefCell::new(Decimal::from_f32(5080.6244).unwrap().into()).into(),
277                    papa: None,
278                }
279            );
280        }
281    }
282}