tank-tests 0.34.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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
use std::{
    sync::LazyLock,
    time::{SystemTime, UNIX_EPOCH},
};
use tank::{
    AsValue, Entity, Executor, Operand, QueryBuilder, Result, cols, expr,
    stream::{StreamExt, TryStreamExt},
};
use time::{Date, Month, OffsetDateTime, PrimitiveDateTime, Time, UtcOffset};
use tokio::sync::Mutex;

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

#[derive(Entity, Debug, Clone, PartialEq, Eq, Hash)]
pub struct Times {
    pub timestamp_1: PrimitiveDateTime,
    pub timestamp_2: chrono::NaiveDateTime,
    pub date_1: Date,
    pub date_2: NaiveDate,
    pub time_1: time::Time,
    pub time_2: NaiveTime,
}

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

    // Setup
    Times::drop_table(executor, true, false)
        .await
        .expect("Failed to drop Times table");
    Times::create_table(executor, false, true)
        .await
        .expect("Failed to create Times table");

    // Insert times
    let timestamps = [
        // 1980-01-01 00:00:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(1980, Month::January, 1).unwrap(),
                Time::from_hms(0, 0, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(1980, 1, 1).unwrap(),
                NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(1980, Month::January, 1).unwrap(),
            date_2: NaiveDate::from_ymd_opt(1980, 1, 1).unwrap(),
            time_1: Time::from_hms(0, 0, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        },
        // 1987-10-05 14:09:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(1987, Month::October, 5).unwrap(),
                Time::from_hms(14, 9, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(1987, 10, 5).unwrap(),
                NaiveTime::from_hms_opt(14, 9, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(1987, Month::October, 5).unwrap(),
            date_2: NaiveDate::from_ymd_opt(1987, 10, 5).unwrap(),
            time_1: Time::from_hms(14, 9, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(14, 9, 0).unwrap(),
        },
        // 1999-12-31 23:59:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(1999, Month::December, 31).unwrap(),
                Time::from_hms(23, 59, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(1999, 12, 31).unwrap(),
                NaiveTime::from_hms_opt(23, 59, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(1999, Month::December, 31).unwrap(),
            date_2: NaiveDate::from_ymd_opt(1999, 12, 31).unwrap(),
            time_1: Time::from_hms(23, 59, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(23, 59, 0).unwrap(),
        },
        // 2025-01-01 00:00:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(2025, Month::January, 1).unwrap(),
                Time::from_hms(0, 0, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
                NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(2025, Month::January, 1).unwrap(),
            date_2: NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
            time_1: Time::from_hms(0, 0, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        },
        // 1950-06-15 08:30:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(1950, Month::June, 15).unwrap(),
                Time::from_hms(8, 30, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(1950, 6, 15).unwrap(),
                NaiveTime::from_hms_opt(8, 30, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(1950, Month::June, 15).unwrap(),
            date_2: NaiveDate::from_ymd_opt(1950, 6, 15).unwrap(),
            time_1: Time::from_hms(8, 30, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(8, 30, 0).unwrap(),
        },
        // 2038-01-19 03:14:07
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(2038, Month::January, 19).unwrap(),
                Time::from_hms(3, 14, 7).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2038, 1, 19).unwrap(),
                NaiveTime::from_hms_opt(3, 14, 7).unwrap(),
            ),
            date_1: Date::from_calendar_date(2038, Month::January, 19).unwrap(),
            date_2: NaiveDate::from_ymd_opt(2038, 1, 19).unwrap(),
            time_1: Time::from_hms(3, 14, 7).unwrap(),
            time_2: NaiveTime::from_hms_opt(3, 14, 7).unwrap(),
        },
        // 2025-07-19 09:42:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(2025, Month::July, 19).unwrap(),
                Time::from_hms(9, 42, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2025, 7, 19).unwrap(),
                NaiveTime::from_hms_opt(9, 42, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(2025, Month::July, 19).unwrap(),
            date_2: NaiveDate::from_ymd_opt(2025, 7, 19).unwrap(),
            time_1: Time::from_hms(9, 42, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(9, 42, 0).unwrap(),
        },
        // 2050-11-11 18:45:59
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(2050, Month::November, 11).unwrap(),
                Time::from_hms(18, 45, 59).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2050, 11, 11).unwrap(),
                NaiveTime::from_hms_opt(18, 45, 59).unwrap(),
            ),
            date_1: Date::from_calendar_date(2050, Month::November, 11).unwrap(),
            date_2: NaiveDate::from_ymd_opt(2050, 11, 11).unwrap(),
            time_1: Time::from_hms(18, 45, 59).unwrap(),
            time_2: NaiveTime::from_hms_opt(18, 45, 59).unwrap(),
        },
        // 2050-09-09 00:00:00
        Times {
            timestamp_1: PrimitiveDateTime::new(
                Date::from_calendar_date(2050, Month::September, 9).unwrap(),
                Time::from_hms(0, 0, 0).unwrap(),
            ),
            timestamp_2: NaiveDateTime::new(
                NaiveDate::from_ymd_opt(2050, 9, 9).unwrap(),
                NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
            ),
            date_1: Date::from_calendar_date(2050, Month::September, 9).unwrap(),
            date_2: NaiveDate::from_ymd_opt(2050, 9, 9).unwrap(),
            time_1: Time::from_hms(0, 0, 0).unwrap(),
            time_2: NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        },
    ];

    Times::insert_many(executor, &timestamps)
        .await
        .expect("Failed to insert timestamps");

    /*
     * 1987-10-05 14:09:00
     * 1999-12-31 23:59:00
     * 2025-01-01 00:00:00
     * 1950-06-15 08:30:00
     * 1900-01-01 00:00:00
     * 2038-01-19 03:14:07
     * 2025-07-19 09:42:00
     * 2050-11-11 18:45:59
     * 2050-09-09 00:00:00
     */

    // Query timestamp 1
    let mut query_timestamp_1 = executor
        .prepare(
            QueryBuilder::new()
                .select([Times::timestamp_1])
                .from(Times::table())
                .where_expr(expr!(Times::timestamp_2 > ?))
                .order_by(cols!(Times::timestamp_1 DESC))
                .build(&executor.driver()),
        )
        .await
        .expect("Could not prepare the query timestamp 1");
    query_timestamp_1
        .bind(PrimitiveDateTime::new(
            Date::from_calendar_date(1999, Month::December, 31).unwrap(),
            Time::from_hms(23, 59, 59).unwrap(),
        ))
        .expect("Could not bind the timestamp 1");
    let values = executor
        .fetch(&mut query_timestamp_1)
        .and_then(|v| async {
            PrimitiveDateTime::try_from_value(
                v.values
                    .into_iter()
                    .next()
                    .expect("Could not get the first column"),
            )
            .map(|v| v.to_string())
        })
        .try_collect::<Vec<_>>()
        .await
        .expect("Could not collect the values from the stream");
    assert_eq!(
        values,
        [
            "2050-11-11 18:45:59.0",
            "2050-09-09 0:00:00.0",
            "2038-01-19 3:14:07.0",
            "2025-07-19 9:42:00.0",
            "2025-01-01 0:00:00.0",
        ]
    );
    query_timestamp_1
        .clear_bindings()
        .expect("Could not clear the bindings for query timestamp 1");
    query_timestamp_1
        .bind(PrimitiveDateTime::new(
            Date::from_calendar_date(1800, Month::January, 1).unwrap(),
            Time::from_hms(0, 0, 0).unwrap(),
        ))
        .expect("Could not bind the timestamp 1");
    let values = executor
        .fetch(&mut query_timestamp_1)
        .and_then(|v| async {
            NaiveDateTime::try_from_value(
                v.values
                    .into_iter()
                    .next()
                    .expect("Could not get the first column"),
            )
            .map(|v| v.to_string())
        })
        .try_collect::<Vec<_>>()
        .await
        .expect("Could not collect the values from the stream");
    assert_eq!(
        values,
        [
            "2050-11-11 18:45:59",
            "2050-09-09 00:00:00",
            "2038-01-19 03:14:07",
            "2025-07-19 09:42:00",
            "2025-01-01 00:00:00",
            "1999-12-31 23:59:00",
            "1987-10-05 14:09:00",
            "1980-01-01 00:00:00",
            "1950-06-15 08:30:00",
        ]
    );

    // Query timestamp 2
    let mut query_timestamp_2 = executor
        .prepare(
            QueryBuilder::new()
                .select([Times::timestamp_2])
                .from(Times::table())
                .where_expr(expr!(Times::timestamp_1 <= ?))
                .order_by(cols!(Times::timestamp_2 ASC))
                .build(&executor.driver()),
        )
        .await
        .expect("Could not prepare the query timestamp 1");
    query_timestamp_2
        .bind(NaiveDateTime::new(
            NaiveDate::from_ymd_opt(2025, 1, 1).unwrap(),
            NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
        ))
        .expect("Could not bind the timestamp 1");
    let values = executor
        .fetch(&mut query_timestamp_2)
        .and_then(|v| async {
            NaiveDateTime::try_from_value(
                v.values
                    .into_iter()
                    .next()
                    .expect("Could not get the first column"),
            )
            .map(|v| v.to_string())
        })
        .try_collect::<Vec<_>>()
        .await
        .expect("Could not collect the values from the stream");
    assert_eq!(
        values,
        [
            "1950-06-15 08:30:00",
            "1980-01-01 00:00:00",
            "1987-10-05 14:09:00",
            "1999-12-31 23:59:00",
            "2025-01-01 00:00:00",
        ]
    );

    // Query time 1
    let mut query_time_1 = executor
        .prepare(
            QueryBuilder::new()
                .select([Times::time_1])
                .from(Times::table())
                .where_expr(true)
                .order_by(cols!(Times::time_1 DESC))
                .build(&executor.driver()),
        )
        .await
        .expect("Could not prepare the query timestamp 1");
    let values = executor
        .fetch(&mut query_time_1)
        .and_then(|v| async {
            NaiveTime::try_from_value(
                v.values
                    .into_iter()
                    .next()
                    .expect("Could not get the first column"),
            )
            .map(|v| v.to_string())
        })
        .try_collect::<Vec<_>>()
        .await
        .expect("Could not collect the values from the stream");
    assert_eq!(
        values,
        [
            "23:59:00", "18:45:59", "14:09:00", "09:42:00", "08:30:00", "03:14:07", "00:00:00",
            "00:00:00", "00:00:00",
        ]
    );

    // Current timestamp ms
    let before = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis()
        - 2;
    let timestamp_ms = executor
        .fetch(
            QueryBuilder::new()
                .select([&Operand::CurrentTimestampMs])
                .from(Times::table())
                .where_expr(true)
                .limit(Some(1))
                .build(&executor.driver()),
        )
        .map_ok(|v| u128::try_from_value(v.values.into_iter().nth(0).expect("There is no column")))
        .map(Result::flatten)
        .try_collect::<Vec<_>>()
        .await
        .expect("Could not get the current timestamp");
    let after = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap()
        .as_millis()
        + 2;
    let timestamp_ms = timestamp_ms.into_iter().next().unwrap();
    assert!(before <= timestamp_ms, "{before} <= {timestamp_ms}");
    assert!(timestamp_ms <= after, "{timestamp_ms} <= {after}");

    // Negative UTC offsets
    {
        #[derive(Entity, Debug, PartialEq)]
        #[tank(name = "tz_offsets")]
        struct TzOffsets {
            #[tank(primary_key)]
            id: i32,
            ts: OffsetDateTime,
        }

        TzOffsets::drop_table(executor, true, false)
            .await
            .expect("Failed to drop TzOffsets table");
        TzOffsets::create_table(executor, true, true)
            .await
            .expect("Failed to create TzOffsets table");

        // UTC-05:30
        let neg_offset = TzOffsets {
            id: 1,
            ts: OffsetDateTime::new_in_offset(
                Date::from_calendar_date(2025, Month::June, 15).unwrap(),
                Time::from_hms(14, 30, 0).unwrap(),
                UtcOffset::from_hms(-5, -30, 0).unwrap(),
            ),
        };
        neg_offset
            .save(executor)
            .await
            .expect("Failed to save entity with negative UTC offset");

        let found = TzOffsets::find_one(executor, expr!(TzOffsets::id == 1))
            .await
            .expect("Failed to query")
            .expect("Entity with negative offset not found");
        assert_eq!(
            found.ts.unix_timestamp(),
            neg_offset.ts.unix_timestamp(),
            "Timestamp instant mismatch after round-trip with negative offset"
        );

        // UTC+05:45
        let nepal = TzOffsets {
            id: 2,
            ts: OffsetDateTime::new_in_offset(
                Date::from_calendar_date(2025, Month::June, 15).unwrap(),
                Time::from_hms(20, 15, 0).unwrap(),
                UtcOffset::from_hms(5, 45, 0).unwrap(),
            ),
        };
        nepal
            .save(executor)
            .await
            .expect("Failed to save entity with Nepal offset");

        let found = TzOffsets::find_one(executor, expr!(TzOffsets::id == 2))
            .await
            .expect("Failed to query")
            .expect("Entity with Nepal offset not found");
        assert_eq!(
            found.ts.unix_timestamp(),
            nepal.ts.unix_timestamp(),
            "Timestamp instant mismatch after round-trip with Nepal offset"
        );

        #[cfg(not(feature = "disable-old-dates"))]
        {
            let bc_offset = TzOffsets {
                id: 3,
                ts: OffsetDateTime::new_in_offset(
                    Date::from_calendar_date(0, Month::March, 15).unwrap(),
                    Time::from_hms(10, 30, 0).unwrap(),
                    UtcOffset::from_hms(2, 0, 0).unwrap(),
                ),
            };
            bc_offset
                .save(executor)
                .await
                .expect("Failed to save entity with BC date and offset");

            let found = TzOffsets::find_one(executor, expr!(TzOffsets::id == 3))
                .await
                .expect("Failed to query")
                .expect("Entity with BC date not found");
            assert_eq!(
                found.ts.unix_timestamp(),
                bc_offset.ts.unix_timestamp(),
                "Timestamp instant mismatch after round-trip with BC date"
            );

            let bc_negative = TzOffsets {
                id: 4,
                ts: OffsetDateTime::new_in_offset(
                    Date::from_calendar_date(-500, Month::July, 1).unwrap(),
                    Time::from_hms(8, 0, 0).unwrap(),
                    UtcOffset::from_hms(-3, 0, 0).unwrap(),
                ),
            };
            bc_negative
                .save(executor)
                .await
                .expect("Failed to save entity with BC date and negative offset");

            let found = TzOffsets::find_one(executor, expr!(TzOffsets::id == 4))
                .await
                .expect("Failed to query")
                .expect("Entity with BC date and negative offset not found");
            assert_eq!(
                found.ts.unix_timestamp(),
                bc_negative.ts.unix_timestamp(),
                "Timestamp instant mismatch after round-trip with BC date and negative offset"
            );
        }
    }
}