Skip to main content

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