Skip to main content

tank_tests/
arrays1.rs

1#![allow(dead_code)]
2#![allow(unused_imports)]
3use std::{borrow::Cow, pin::pin, sync::LazyLock};
4use tank::{
5    Driver, DynQuery, Entity, Executor, Query, QueryBuilder, QueryResult, RawQuery, SqlWriter,
6    cols, stream::TryStreamExt,
7};
8use tokio::sync::Mutex;
9
10static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
11
12#[derive(Entity, Debug, PartialEq)]
13struct Arrays1 {
14    #[cfg(not(feature = "disable-intervals"))]
15    aa: [time::Duration; 3],
16    bb: Option<[[f32; 4]; 2]>,
17    cc: [[[[i8; 1]; 1]; 3]; 1],
18    dd: [[[[[i64; 3]; 1]; 1]; 2]; 1],
19}
20
21#[derive(Entity, Debug, PartialEq)]
22struct Arrays2 {
23    alpha: [i32; 5],
24    bravo: [[u8; 2]; 3],
25    charlie: [[[bool; 2]; 1]; 2],
26    delta: [[[[f64; 2]; 2]; 1]; 1],
27    echo: [[[Cow<'static, str>; 2]; 2]; 1],
28}
29
30pub async fn arrays1(executor: &mut impl Executor) {
31    let _ = MUTEX.lock().await;
32
33    // Setup
34    Arrays1::drop_table(executor, true, false)
35        .await
36        .expect("Failed to drop Arrays1 table");
37    Arrays2::drop_table(executor, true, false)
38        .await
39        .expect("Failed to drop Arrays2 table");
40    Arrays1::create_table(executor, false, true)
41        .await
42        .expect("Failed to create Arrays1 table");
43    Arrays2::create_table(executor, false, true)
44        .await
45        .expect("Failed to create Arrays2 table");
46
47    Arrays1::insert_many(
48        executor,
49        &[
50            Arrays1 {
51                #[cfg(not(feature = "disable-intervals"))]
52                aa: [
53                    time::Duration::seconds(1),
54                    time::Duration::seconds(2),
55                    time::Duration::seconds(3),
56                ],
57                bb: None,
58                cc: [[[[10]], [[20]], [[30]]]],
59                dd: [[[[[100, 200, 300]]], [[[400, 500, 600]]]]],
60            },
61            Arrays1 {
62                #[cfg(not(feature = "disable-intervals"))]
63                aa: [
64                    time::Duration::milliseconds(150),
65                    time::Duration::milliseconds(250),
66                    time::Duration::milliseconds(350),
67                ],
68                bb: [[9.9, 8.8, 7.7, 6.6], [5.5, 4.4, 3.3, 2.2]].into(),
69                cc: [[[[1]], [[2]], [[3]]]],
70                dd: [[[[[7, 8, 9]]], [[[10, 11, 12]]]]],
71            },
72        ],
73    )
74    .await
75    .expect("Could not insert Arrays1 values");
76    {
77        let mut stream = pin!(Arrays1::find_many(executor, true, None));
78        while let Some(value) = stream
79            .try_next()
80            .await
81            .expect("Failed to retrieve the value")
82        {
83            if value.bb.is_none() {
84                assert_eq!(
85                    value,
86                    Arrays1 {
87                        #[cfg(not(feature = "disable-intervals"))]
88                        aa: [
89                            time::Duration::seconds(1),
90                            time::Duration::seconds(2),
91                            time::Duration::seconds(3),
92                        ],
93                        bb: None,
94                        cc: [[[[10]], [[20]], [[30]]]],
95                        dd: [[[[[100, 200, 300]]], [[[400, 500, 600]]]]],
96                    }
97                );
98            } else {
99                assert_eq!(
100                    value,
101                    Arrays1 {
102                        #[cfg(not(feature = "disable-intervals"))]
103                        aa: [
104                            time::Duration::milliseconds(150),
105                            time::Duration::milliseconds(250),
106                            time::Duration::milliseconds(350),
107                        ],
108                        bb: [[9.9, 8.8, 7.7, 6.6], [5.5, 4.4, 3.3, 2.2]].into(),
109                        cc: [[[[1]], [[2]], [[3]]]],
110                        dd: [[[[[7, 8, 9]]], [[[10, 11, 12]]]]],
111                    }
112                );
113            }
114        }
115    }
116
117    // Multiple statements
118    #[cfg(not(feature = "disable-multiple-statements"))]
119    {
120        let mut query = DynQuery::default();
121        let writer = executor.driver().sql_writer();
122        writer.write_drop_table::<Arrays1>(&mut query, true);
123        writer.write_create_table::<Arrays1>(&mut query, true);
124        writer.write_drop_table::<Arrays2>(&mut query, true);
125        writer.write_create_table::<Arrays2>(&mut query, true);
126        let value = Arrays1 {
127            #[cfg(not(feature = "disable-intervals"))]
128            aa: [
129                time::Duration::seconds(1),
130                time::Duration::seconds(2),
131                time::Duration::seconds(3),
132            ],
133            bb: [
134                [1.1, -2.2, -3.3, 4.4],
135                [5.5, f32::INFINITY, f32::NEG_INFINITY, 8.8],
136            ]
137            .into(),
138            cc: [[[[1]], [[2]], [[3]]]],
139            dd: [[[[[10, 20, 30]]], [[[40, 50, 60]]]]],
140        };
141        writer.write_insert(&mut query, &[value], false);
142        writer.write_select(
143            &mut query,
144            &QueryBuilder::new()
145                .select(cols!(*))
146                .from(Arrays1::table())
147                .where_expr(true),
148        );
149        let value = Arrays2 {
150            alpha: [1, 2, 3, 4, 5],
151            bravo: [[10, 11], [12, 13], [14, 15]],
152            charlie: [[[true, false]], [[false, true]]],
153            delta: [[[[1.1, 1.2], [2.1, 2.2]]]],
154            echo: [[
155                [Cow::Borrowed("hello"), Cow::Owned("world".to_string())],
156                [Cow::Owned("foo".to_string()), Cow::Borrowed("bar")],
157            ]],
158        };
159        writer.write_insert(&mut query, &[value], false);
160        writer.write_select(
161            &mut query,
162            &QueryBuilder::new()
163                .select([Arrays2::alpha, Arrays2::bravo, Arrays2::charlie])
164                .from(Arrays2::table())
165                .where_expr(true),
166        );
167        writer.write_select(
168            &mut query,
169            &QueryBuilder::new()
170                .select([Arrays2::delta, Arrays2::echo])
171                .from(Arrays2::table())
172                .where_expr(true),
173        );
174        let rows = pin!(executor.run(query).try_filter_map(|v| async move {
175            Ok(match v {
176                QueryResult::Row(v) => Some(v),
177                QueryResult::Affected(..) => None,
178            })
179        }));
180        let rows = rows
181            .try_collect::<Vec<_>>()
182            .await
183            .expect("Could not collect the rows");
184
185        let value = Arrays1::from_row(rows[0].clone()).expect("First must be Arrays1");
186        assert_eq!(
187            value,
188            Arrays1 {
189                #[cfg(not(feature = "disable-intervals"))]
190                aa: [
191                    time::Duration::seconds(1),
192                    time::Duration::seconds(2),
193                    time::Duration::seconds(3),
194                ],
195                bb: [
196                    [1.1, -2.2, -3.3, 4.4],
197                    [5.5, f32::INFINITY, f32::NEG_INFINITY, 8.8],
198                ]
199                .into(),
200                cc: [[[[1]], [[2]], [[3]]]],
201                dd: [[[[[10, 20, 30]]], [[[40, 50, 60]]]]],
202            },
203        );
204    }
205}