tank_tests/
complex.rs

1#![allow(unused_imports)]
2use std::{
3    collections::{BTreeMap, LinkedList, VecDeque},
4    sync::LazyLock,
5    time::Duration,
6};
7use tank::{Entity, Executor};
8use tokio::sync::Mutex;
9
10#[derive(Default)]
11struct TankUnsupported {
12    field: i32,
13}
14#[derive(Entity)]
15struct ComplexNullFields {
16    #[cfg(not(feature = "disable-arrays"))]
17    first: Option<[Option<f64>; 8]>,
18    #[cfg(all(
19        not(feature = "disable-lists"),
20        not(feature = "disable-intervals"),
21        not(feature = "disable-nested-collections"),
22    ))]
23    second: Option<Vec<Option<Duration>>>,
24    third: Option<Box<[u8]>>,
25    #[cfg(all(
26        not(feature = "disable-arrays"),
27        not(feature = "disable-maps"),
28        not(feature = "disable-nested-collections"),
29    ))]
30    fourth: Option<Box<BTreeMap<String, Option<[Option<i128>; 3]>>>>,
31    #[cfg(all(
32        not(feature = "disable-lists"),
33        not(feature = "disable-maps"),
34        not(feature = "disable-nested-collections"),
35    ))]
36    fifth: LinkedList<Option<VecDeque<Option<BTreeMap<i32, Option<i32>>>>>>,
37    #[tank(ignore)]
38    sixth: TankUnsupported,
39}
40impl Default for ComplexNullFields {
41    fn default() -> Self {
42        Self {
43            #[cfg(not(feature = "disable-arrays"))]
44            first: None,
45            #[cfg(all(
46                not(feature = "disable-lists"),
47                not(feature = "disable-intervals"),
48                not(feature = "disable-nested-collections"),
49            ))]
50            second: None,
51            third: None,
52            #[cfg(all(
53                not(feature = "disable-arrays"),
54                not(feature = "disable-maps"),
55                not(feature = "disable-nested-collections"),
56            ))]
57            fourth: None,
58            #[cfg(all(
59                not(feature = "disable-lists"),
60                not(feature = "disable-maps"),
61                not(feature = "disable-nested-collections"),
62            ))]
63            fifth: Default::default(),
64            sixth: TankUnsupported { field: 777 },
65        }
66    }
67}
68static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));
69
70pub async fn complex<E: Executor>(executor: &mut E) {
71    let _lock = MUTEX.lock().await;
72
73    // Setup
74    ComplexNullFields::drop_table(executor, true, false)
75        .await
76        .expect("Failed to drop ComplexNullFields table");
77    ComplexNullFields::create_table(executor, true, true)
78        .await
79        .expect("Failed to create ComplexNullFields table");
80
81    // Complex 1
82    ComplexNullFields::delete_many(executor, &true)
83        .await
84        .expect("Failed to clear the ComplexNullFields table");
85    let entity = ComplexNullFields {
86        #[cfg(not(feature = "disable-arrays"))]
87        first: None,
88        #[cfg(all(
89            not(feature = "disable-lists"),
90            not(feature = "disable-intervals"),
91            not(feature = "disable-nested-collections"),
92        ))]
93        second: Some(vec![
94            None,
95            None,
96            Duration::from_millis(15).into(),
97            Duration::from_micros(22).into(),
98            None,
99            Duration::from_micros(99).into(),
100            Duration::from_micros(0).into(),
101            Duration::from_secs(24).into(),
102            None,
103            None,
104            None,
105        ]),
106        third: Some(Box::new([0x75, 0xAA, 0x30, 0x77])),
107        #[cfg(all(
108            not(feature = "disable-arrays"),
109            not(feature = "disable-maps"),
110            not(feature = "disable-nested-collections"),
111        ))]
112        fourth: Some(Box::new(BTreeMap::from_iter([
113            ("aa".into(), Some([19314.into(), 241211.into(), None])),
114            (
115                "bb".into(),
116                Some([165536.into(), 23311090.into(), 30001.into()]),
117            ),
118            ("cc".into(), None),
119            ("dd".into(), Some([None, None, None])),
120            ("ee".into(), Some([None, 777.into(), None])),
121        ]))),
122        #[cfg(all(
123            not(feature = "disable-lists"),
124            not(feature = "disable-maps"),
125            not(feature = "disable-nested-collections"),
126        ))]
127        fifth: LinkedList::from_iter([]),
128        sixth: Default::default(),
129    };
130    ComplexNullFields::insert_one(executor, &entity)
131        .await
132        .expect("Failed to save complex 1");
133    let entity = ComplexNullFields::find_one(executor, &true)
134        .await
135        .expect("Failed to query complex 1")
136        .expect("Failed to find complex 1");
137    #[cfg(not(feature = "disable-arrays"))]
138    assert_eq!(entity.first, None);
139    #[cfg(all(
140        not(feature = "disable-lists"),
141        not(feature = "disable-intervals"),
142        not(feature = "disable-nested-collections"),
143    ))]
144    assert_eq!(
145        entity.second,
146        Some(vec![
147            None,
148            None,
149            Duration::from_millis(15).into(),
150            Duration::from_micros(22).into(),
151            None,
152            Duration::from_micros(99).into(),
153            Duration::from_micros(0).into(),
154            Duration::from_secs(24).into(),
155            None,
156            None,
157            None,
158        ])
159    );
160    assert_eq!(*entity.third.unwrap(), [0x75, 0xAA, 0x30, 0x77]);
161    #[cfg(all(
162        not(feature = "disable-arrays"),
163        not(feature = "disable-maps"),
164        not(feature = "disable-nested-collections"),
165    ))]
166    assert_eq!(
167        *entity.fourth.unwrap(),
168        BTreeMap::from_iter([
169            ("aa".into(), Some([19314.into(), 241211.into(), None])),
170            (
171                "bb".into(),
172                Some([165536.into(), 23311090.into(), 30001.into()]),
173            ),
174            ("cc".into(), None),
175            ("dd".into(), Some([None, None, None])),
176            ("ee".into(), Some([None, 777.into(), None])),
177        ])
178    );
179    #[cfg(all(
180        not(feature = "disable-lists"),
181        not(feature = "disable-maps"),
182        not(feature = "disable-nested-collections"),
183    ))]
184    assert_eq!(entity.fifth.len(), 0);
185
186    // Complex 2
187    ComplexNullFields::delete_many(executor, &true)
188        .await
189        .expect("Failed to clear the ComplexNullFields table");
190    let entity = ComplexNullFields {
191        #[cfg(not(feature = "disable-arrays"))]
192        first: Some([
193            0.5.into(),
194            None,
195            (-99.5).into(),
196            100.0.into(),
197            0.0.into(),
198            #[cfg(not(feature = "disable-infinity"))]
199            f64::NEG_INFINITY.into(),
200            #[cfg(feature = "disable-infinity")]
201            f64::MIN.into(),
202            None,
203            777.777.into(),
204        ]),
205        #[cfg(all(
206            not(feature = "disable-lists"),
207            not(feature = "disable-intervals"),
208            not(feature = "disable-nested-collections"),
209        ))]
210        second: None,
211        third: None,
212        #[cfg(all(
213            not(feature = "disable-arrays"),
214            not(feature = "disable-maps"),
215            not(feature = "disable-nested-collections"),
216        ))]
217        fourth: None,
218        #[cfg(all(
219            not(feature = "disable-lists"),
220            not(feature = "disable-maps"),
221            not(feature = "disable-nested-collections"),
222        ))]
223        fifth: LinkedList::from_iter([
224            None,
225            None,
226            None,
227            None,
228            None,
229            Some(
230                vec![
231                    Some(BTreeMap::from_iter([
232                        (1, Some(11)),
233                        (2, Some(22)),
234                        (3, None),
235                        (4, None),
236                        (5, Some(55)),
237                    ])),
238                    None,
239                ]
240                .into(),
241            ),
242            None,
243        ]),
244        sixth: Default::default(),
245    };
246    ComplexNullFields::insert_one(executor, &entity)
247        .await
248        .expect("Failed to save complex 2");
249    let loaded = ComplexNullFields::find_one(executor, &true)
250        .await
251        .expect("Failed to query complex 2")
252        .expect("Failed to find complex 2");
253    #[cfg(not(feature = "disable-arrays"))]
254    assert_eq!(loaded.first, entity.first);
255    #[cfg(all(
256        not(feature = "disable-lists"),
257        not(feature = "disable-intervals"),
258        not(feature = "disable-nested-collections"),
259    ))]
260    assert_eq!(loaded.second, None);
261    assert_eq!(loaded.third, None);
262    #[cfg(all(
263        not(feature = "disable-arrays"),
264        not(feature = "disable-maps"),
265        not(feature = "disable-nested-collections"),
266    ))]
267    assert_eq!(loaded.fourth, None);
268    #[cfg(all(
269        not(feature = "disable-lists"),
270        not(feature = "disable-maps"),
271        not(feature = "disable-nested-collections"),
272    ))]
273    assert_eq!(loaded.fifth, entity.fifth);
274    assert_eq!(loaded.sixth.field, 777);
275}