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