tank-tests 0.36.0

Test suide for drivers of Tank: the Rust data layer. This is intended to be used by drivers to implement common unit tests.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#![allow(unused_imports)]
use crate::silent_logs;
use core::f64;
use std::{pin::pin, sync::LazyLock};
use tank::{
    Driver, DynQuery, Entity, Executor, Interval, QueryBuilder, QueryResult, RawQuery,
    RowsAffected, SqlWriter, expr, stream::StreamExt,
};
use time::{Date, Month, Time};
use tokio::sync::Mutex;

static MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

// Safe max value
const F32_MAX: f32 = 3.4e+38_f32;

#[derive(Entity)]
struct Limits {
    #[tank(primary_key)]
    id: usize,
    boolean: bool,
    int8: i8,
    uint8: u8,
    int16: i16,
    uint16: u16,
    int32: i32,
    uint32: u32,
    int64: i64,
    #[cfg(not(feature = "disable-large-integers"))]
    uint64: u64,
    #[cfg(not(feature = "disable-large-integers"))]
    int128: i128,
    #[cfg(not(feature = "disable-large-integers"))]
    uint128: u128,
    float32: f32,
    float64: f64,
    time: Time,
    date: Date,
    #[cfg(not(feature = "disable-intervals"))]
    interval: Interval,
}

pub async fn limits(executor: &mut impl Executor) {
    let _lock = MUTEX.lock().await;

    // Setup
    silent_logs! {
        // Silent logs for Valkey/Redis
        Limits::drop_table(executor, true, false)
            .await
            .expect("Failed to drop SimpleNullFields table");
    }
    Limits::create_table(executor, true, true)
        .await
        .expect("Failed to create SimpleNullFields table");

    // Minimals
    Limits::delete_many(executor, expr!(id == 1))
        .await
        .expect("Failed to clear the Limits table");
    let minimals = Limits {
        id: 1,
        boolean: false,
        int8: -127,
        uint8: 0,
        int16: -32_768,
        uint16: 0,
        int32: -2_147_483_648,
        uint32: 0,
        int64: -9_223_372_036_854_775_808,
        #[cfg(not(feature = "disable-large-integers"))]
        uint64: 0,
        #[cfg(not(feature = "disable-large-integers"))]
        int128: -170_141_183_460_469_231_731_687_303_715_884_105_728,
        #[cfg(not(feature = "disable-large-integers"))]
        uint128: 0,
        float32: f32::MIN_POSITIVE,
        #[cfg(not(feature = "disable-infinity"))]
        float64: f64::NEG_INFINITY,
        #[cfg(feature = "disable-infinity")]
        float64: f64::MIN,
        time: Time::from_hms(0, 0, 0).expect("All zero must be correct time"),
        #[cfg(not(feature = "disable-old-dates"))]
        date: Date::from_calendar_date(-2000, Month::January, 01)
            .expect("Very old date must be correct"),
        #[cfg(feature = "disable-old-dates")]
        date: Date::from_calendar_date(1970, Month::January, 01)
            .expect("Very old date must be correct"),
        #[cfg(not(feature = "disable-intervals"))]
        interval: Interval::from_micros(1),
    };
    Limits::insert_one(executor, &minimals)
        .await
        .expect("Failed to save minimals entity");
    let loaded = Limits::find_one(executor, expr!(id == 1))
        .await
        .expect("Failed to query simple 2")
        .expect("Failed to find simple 2");
    assert_eq!(loaded.id, 1);
    assert_eq!(loaded.boolean, false);
    assert_eq!(loaded.int8, -127);
    assert_eq!(loaded.uint8, 0);
    assert_eq!(loaded.int16, -32_768);
    assert_eq!(loaded.uint16, 0);
    assert_eq!(loaded.int32, -2_147_483_648);
    assert_eq!(loaded.uint32, 0);
    assert_eq!(loaded.int64, -9_223_372_036_854_775_808);
    #[cfg(not(feature = "disable-large-integers"))]
    assert_eq!(loaded.uint64, 0);
    #[cfg(not(feature = "disable-large-integers"))]
    assert_eq!(
        loaded.int128,
        -170_141_183_460_469_231_731_687_303_715_884_105_728
    );
    #[cfg(not(feature = "disable-large-integers"))]
    assert_eq!(loaded.uint128, 0);
    assert!((loaded.float32 - f32::MIN_POSITIVE).abs() < 0.0001);
    #[cfg(not(feature = "disable-infinity"))]
    assert_eq!(loaded.float64, f64::NEG_INFINITY);
    #[cfg(feature = "disable-infinity")]
    assert_eq!(loaded.float64, f64::MIN);
    assert_eq!(loaded.time, Time::from_hms(0, 0, 0).unwrap());
    #[cfg(not(feature = "disable-old-dates"))]
    assert_eq!(
        loaded.date,
        Date::from_calendar_date(-2000, Month::January, 01).unwrap()
    );
    #[cfg(feature = "disable-old-dates")]
    assert_eq!(
        loaded.date,
        Date::from_calendar_date(1970, Month::January, 01).unwrap()
    );
    #[cfg(not(feature = "disable-intervals"))]
    assert_eq!(loaded.interval, Interval::from_micros(1));

    // Maximals
    Limits::delete_many(executor, expr!(Limits::id == 1))
        .await
        .expect("Failed to clear the Limits table");
    let maximals = Limits {
        id: 2,
        boolean: true,
        int8: 127,
        uint8: 255,
        int16: 32_767,
        uint16: 65_535,
        int32: 2_147_483_647,
        uint32: 4_294_967_295,
        int64: 9_223_372_036_854_775_807,
        #[cfg(not(feature = "disable-large-integers"))]
        uint64: 18_446_744_073_709_551_615,
        #[cfg(not(feature = "disable-large-integers"))]
        int128: 170_141_183_460_469_231_731_687_303_715_884_105_727,
        #[cfg(not(feature = "disable-large-integers"))]
        uint128: 340_282_366_920_938_463_463_374_607_431_768_211_455,
        float32: F32_MAX,
        #[cfg(not(feature = "disable-infinity"))]
        float64: f64::INFINITY,
        #[cfg(feature = "disable-infinity")]
        float64: f64::MAX,
        time: Time::from_hms_micro(23, 59, 59, 999_999)
            .expect("Close to midnight time must be correct"),
        date: Date::from_calendar_date(9999, Month::December, 31)
            .expect("Very old date must be correct"),
        #[cfg(all(
            not(feature = "disable-intervals"),
            not(feature = "disable-large-intervals"),
        ))]
        interval: Interval::from_years(1_000_000),
        #[cfg(all(
            not(feature = "disable-intervals"),
            feature = "disable-large-intervals"
        ))]
        interval: Interval::from_days(31) + Interval::from_mins(5),
    };
    Limits::insert_one(executor, &maximals)
        .await
        .expect("Failed to save maximals entity");
    let loaded = Limits::find_one(executor, expr!(Limits::id == 2))
        .await
        .expect("Failed to query simple 2")
        .expect("Failed to find simple 2");
    assert_eq!(loaded.id, 2);
    assert_eq!(loaded.boolean, true);
    assert_eq!(loaded.int8, 127);
    assert_eq!(loaded.uint8, 255);
    assert_eq!(loaded.int16, 32_767);
    assert_eq!(loaded.uint16, 65_535);
    assert_eq!(loaded.int32, 2_147_483_647);
    assert_eq!(loaded.uint32, 4_294_967_295);
    assert_eq!(loaded.int64, 9_223_372_036_854_775_807);
    #[cfg(not(feature = "disable-large-integers"))]
    assert_eq!(loaded.uint64, 18_446_744_073_709_551_615);
    #[cfg(not(feature = "disable-large-integers"))]
    assert_eq!(
        loaded.int128,
        170_141_183_460_469_231_731_687_303_715_884_105_727
    );
    #[cfg(not(feature = "disable-large-integers"))]
    assert_eq!(
        loaded.uint128,
        340_282_366_920_938_463_463_374_607_431_768_211_455
    );
    assert!((loaded.float32 - F32_MAX).abs() < 0.001);
    #[cfg(not(feature = "disable-infinity"))]
    assert_eq!(loaded.float64, f64::INFINITY);
    #[cfg(feature = "disable-infinity")]
    assert_eq!(loaded.float64, f64::MAX);
    assert!(
        (loaded.time - Time::from_hms_micro(23, 59, 59, 999_999).unwrap()).abs()
            < time::Duration::milliseconds(1),
    );
    assert_eq!(
        loaded.date,
        Date::from_calendar_date(9999, Month::December, 31).unwrap()
    );
    #[cfg(all(
        not(feature = "disable-intervals"),
        not(feature = "disable-large-intervals"),
    ))]
    assert_eq!(loaded.interval, Interval::from_years(1_000_000));
    #[cfg(all(
        not(feature = "disable-intervals"),
        feature = "disable-large-intervals"
    ))]
    assert_eq!(
        loaded.interval,
        Interval::from_days(31) + Interval::from_mins(5)
    );

    // Small negative values (tests sign-extension when varint is fewer than 16 bytes)
    #[cfg(not(feature = "disable-large-integers"))]
    {
        macro_rules! test_small_negative {
            ($executor:expr, $id:literal, $val:expr) => {{
                Limits::delete_many($executor, expr!(id == $id))
                    .await
                    .expect("Failed to clear for small negatives test");
                let entry = Limits {
                    id: $id,
                    boolean: false,
                    int8: -1,
                    uint8: 0,
                    int16: -1,
                    uint16: 0,
                    int32: -1,
                    uint32: 0,
                    int64: -1,
                    uint64: 0,
                    int128: $val,
                    uint128: 0,
                    float32: 0.0,
                    float64: 0.0,
                    time: Time::from_hms(0, 0, 0).unwrap(),
                    date: Date::from_calendar_date(2000, Month::January, 01).unwrap(),
                    #[cfg(not(feature = "disable-intervals"))]
                    interval: Interval::ZERO,
                };
                Limits::insert_one($executor, &entry).await.expect(concat!(
                    "Failed to insert small negative i128 = ",
                    stringify!($val)
                ));
                let loaded = Limits::find_one($executor, expr!(id == $id))
                    .await
                    .expect(concat!(
                        "Failed to query small negative i128 = ",
                        stringify!($val)
                    ))
                    .expect(concat!(
                        "Failed to find small negative i128 = ",
                        stringify!($val)
                    ));
                assert_eq!(
                    loaded.int128, $val,
                    concat!(
                        "Small negative i128 round-trip failed for ",
                        stringify!($val)
                    )
                );
            }};
        }
        test_small_negative!(executor, 10, -1_i128);
        test_small_negative!(executor, 11, -128_i128);
        test_small_negative!(executor, 12, -1000_i128);
        test_small_negative!(executor, 13, -100_000_i128);
        Limits::delete_many(executor, expr!(id == 10)).await.ok();
        Limits::delete_many(executor, expr!(id == 11)).await.ok();
        Limits::delete_many(executor, expr!(id == 12)).await.ok();
        Limits::delete_many(executor, expr!(id == 13)).await.ok();
    }

    // Multiple statements
    #[cfg(not(feature = "disable-multiple-statements"))]
    {
        let mut query = DynQuery::default();
        let writer = executor.driver().sql_writer();
        writer.write_delete::<Limits>(&mut query, true);
        writer.write_insert(&mut query, &[minimals, maximals], false);
        writer.write_select(
            &mut query,
            &QueryBuilder::new()
                .select(Limits::columns())
                .from(Limits::table())
                .where_expr(expr!(!Limits::boolean)),
        );
        writer.write_select(
            &mut query,
            &QueryBuilder::new()
                .select(Limits::columns())
                .from(Limits::table())
                .where_expr(expr!(Limits::boolean)),
        );
        let mut stream = pin!(executor.run(query));
        let Some(Ok(QueryResult::Affected(RowsAffected { rows_affected, .. }))) =
            stream.next().await
        else {
            panic!("Could not get the result of the first statement");
        };
        if let Some(rows_affected) = rows_affected {
            assert_eq!(rows_affected, 1, "Should have deleted one row");
        }
        let Some(Ok(QueryResult::Affected(RowsAffected { rows_affected, .. }))) =
            stream.next().await
        else {
            panic!("Could not get the result of the second statement");
        };
        if let Some(rows_affected) = rows_affected {
            assert_eq!(rows_affected, 2, "Should have inserted two rows");
        }
        let Some(Ok(QueryResult::Row(row))) = stream.next().await else {
            panic!("Could not get the row of the third statement");
        };
        let loaded = Limits::from_row(row).expect("Could not decode the Limits from minimals row");
        assert_eq!(loaded.boolean, false);
        assert_eq!(loaded.int8, -127);
        assert_eq!(loaded.uint8, 0);
        assert_eq!(loaded.int16, -32_768);
        assert_eq!(loaded.uint16, 0);
        assert_eq!(loaded.int32, -2_147_483_648);
        assert_eq!(loaded.uint32, 0);
        assert_eq!(loaded.int64, -9_223_372_036_854_775_808);
        #[cfg(not(feature = "disable-large-integers"))]
        assert_eq!(loaded.uint64, 0);
        #[cfg(not(feature = "disable-large-integers"))]
        assert_eq!(
            loaded.int128,
            -170_141_183_460_469_231_731_687_303_715_884_105_728
        );
        #[cfg(not(feature = "disable-large-integers"))]
        assert_eq!(loaded.uint128, 0);
        assert!((loaded.float32 - f32::MIN_POSITIVE).abs() < 0.0001);
        #[cfg(not(feature = "disable-infinity"))]
        assert_eq!(loaded.float64, f64::NEG_INFINITY);
        #[cfg(feature = "disable-infinity")]
        assert_eq!(loaded.float64, f64::MIN);
        assert_eq!(loaded.time, Time::from_hms(0, 0, 0).unwrap());
        assert_eq!(
            loaded.date,
            Date::from_calendar_date(-2000, Month::January, 01).unwrap()
        );
        let Some(Ok(QueryResult::Row(row))) = stream.next().await else {
            panic!("Could not get the row of the third statement");
        };
        let loaded = Limits::from_row(row).expect("Could not decode the Limits from maximals row");
        assert_eq!(loaded.boolean, true);
        assert_eq!(loaded.int8, 127);
        assert_eq!(loaded.uint8, 255);
        assert_eq!(loaded.int16, 32_767);
        assert_eq!(loaded.uint16, 65_535);
        assert_eq!(loaded.int32, 2_147_483_647);
        assert_eq!(loaded.uint32, 4_294_967_295);
        assert_eq!(loaded.int64, 9_223_372_036_854_775_807);
        #[cfg(not(feature = "disable-large-integers"))]
        assert_eq!(loaded.uint64, 18_446_744_073_709_551_615);
        #[cfg(not(feature = "disable-large-integers"))]
        assert_eq!(
            loaded.int128,
            170_141_183_460_469_231_731_687_303_715_884_105_727
        );
        #[cfg(not(feature = "disable-large-integers"))]
        assert_eq!(
            loaded.uint128,
            340_282_366_920_938_463_463_374_607_431_768_211_455
        );
        assert!((loaded.float32 - F32_MAX).abs() < 1E35 && loaded.float32 > 1E38);
        #[cfg(not(feature = "disable-infinity"))]
        assert_eq!(loaded.float64, f64::INFINITY);
        #[cfg(feature = "disable-infinity")]
        assert_eq!(loaded.float64, f64::MIN);
        assert_eq!(
            loaded.time,
            Time::from_hms_micro(23, 59, 59, 999_999).unwrap()
        );
        assert_eq!(
            loaded.date,
            Date::from_calendar_date(9999, Month::December, 31).unwrap()
        );
        #[cfg(all(
            not(feature = "disable-intervals"),
            not(feature = "disable-large-intervals"),
        ))]
        assert_eq!(loaded.interval, Interval::from_years(1_000_000));
        #[cfg(all(
            not(feature = "disable-intervals"),
            feature = "disable-large-intervals"
        ))]
        assert_eq!(
            loaded.interval,
            Interval::from_days(31) + Interval::from_mins(5)
        );
    }
}