tank-tests 0.39.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
#![allow(unused_imports)]
use std::{collections::HashMap, pin::pin, sync::LazyLock};
use tank::{
    AsValue, Connection, Driver, DynQuery, Entity, Error, Executor, QueryBuilder, QueryResult,
    Result, RowsAffected, SqlWriter, Transaction, Value, cols, expr, join,
    stream::{StreamExt, TryStreamExt},
};
use tokio::sync::Mutex;
use uuid::Uuid;

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

#[derive(Debug, PartialEq, Clone)]
pub struct Notes(pub String);
pub struct NotesWrap(pub Notes);

impl AsValue for NotesWrap {
    fn as_empty_value() -> Value {
        Value::Varchar(None)
    }
    fn as_value(self) -> Value {
        Value::Varchar(Some(self.0.0.into()))
    }
    fn try_from_value(value: Value) -> Result<Self> {
        match value.try_as(&Value::Varchar(None)) {
            Ok(Value::Varchar(Some(s))) => Ok(NotesWrap(Notes(s.to_string()))),
            _ => Err(Error::msg("Expected Varchar for Notes")),
        }
    }
}
impl From<Notes> for NotesWrap {
    fn from(v: Notes) -> Self {
        NotesWrap(v)
    }
}
impl From<NotesWrap> for Notes {
    fn from(v: NotesWrap) -> Self {
        v.0
    }
}

#[derive(Entity, Debug, PartialEq)]
#[tank(
    schema = "army",
    name = "deployments",
    primary_key = (Self::unit_id, Self::region),
)]
struct EntityExample {
    unit_id: Uuid,
    #[tank(clustering_key)]
    region: String,
    #[tank(name = "callsign")]
    callsign: String,
    casualties: i32,
    #[tank(conversion_type = NotesWrap)]
    metadata: Notes,
    #[tank(ignore)]
    transient_cache: HashMap<String, String>,
}

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

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

    let uid1 = Uuid::new_v4();
    let uid2 = Uuid::new_v4();
    let uid3 = Uuid::new_v4();

    let entity1 = EntityExample {
        unit_id: uid1,
        region: "North".into(),
        callsign: "Alpha-1".into(),
        casualties: 0,
        metadata: Notes("mission-1".into()),
        transient_cache: HashMap::new(),
    };
    EntityExample::insert_one(executor, &entity1)
        .await
        .expect("Failed to insert one");

    EntityExample::insert_many(
        executor,
        &[EntityExample {
            unit_id: uid2,
            region: "South".into(),
            callsign: "Bravo-2".into(),
            casualties: 3,
            metadata: Notes("mission-2".into()),
            transient_cache: HashMap::new(),
        }],
    )
    .await
    .expect("Failed to insert many");

    executor
        .append(&[EntityExample {
            unit_id: uid3,
            region: "East".into(),
            callsign: "Charlie-3".into(),
            casualties: 5,
            metadata: Notes("mission-3".into()),
            transient_cache: HashMap::new(),
        }])
        .await
        .expect("append");

    let entity = EntityExample::find_one(executor, entity1.primary_key_expr())
        .await
        .expect("Failed to query find one")
        .expect("Expected one result");
    assert_eq!(entity.callsign, "Alpha-1");
    assert_eq!(entity.metadata, Notes("mission-1".into()));

    #[cfg(not(feature = "disable-scanning"))]
    {
        let mut n = 0usize;
        let mut stream = pin!(EntityExample::find_many(
            executor,
            expr!(EntityExample::casualties > 0),
            Some(100),
        ));
        while let Some(entity) = stream.try_next().await.expect("stream next") {
            assert!(entity.casualties > 0);
            n += 1;
        }
        assert_eq!(n, 2);
    }
    #[cfg(feature = "disable-scanning")]
    {
        let mut n = 0usize;
        let mut stream = pin!(EntityExample::find_many(executor, true, Some(100)));
        while stream.try_next().await.expect("stream next").is_some() {
            n += 1;
        }
        assert_eq!(n, 3);
    }

    let uid = uid1;
    let entities = EntityExample::find_many(executor, expr!(EntityExample::unit_id == #uid), None)
        .try_collect::<Vec<EntityExample>>()
        .await
        .expect("collect by uid");
    assert_eq!(entities.len(), 1);
    assert_eq!(entities[0].callsign, "Alpha-1");

    let mut alpha = EntityExample::find_one(executor, entity1.primary_key_expr())
        .await
        .expect("find for save")
        .expect("present for save");
    alpha.casualties = 7;
    alpha.save(executor).await.expect("save");
    let saved = EntityExample::find_one(executor, entity1.primary_key_expr())
        .await
        .expect("find after save")
        .expect("present after save");
    assert_eq!(saved.casualties, 7);

    let bravo = EntityExample::find_one(
        executor,
        expr!(EntityExample::unit_id == #uid2 && EntityExample::region == "South"),
    )
    .await
    .expect("find Bravo for delete")
    .expect("Bravo present");
    bravo.delete(executor).await.expect("entity delete");
    assert!(
        EntityExample::find_one(
            executor,
            expr!(EntityExample::unit_id == #uid2 && EntityExample::region == "South")
        )
        .await
        .expect("find after delete")
        .is_none(),
        "Bravo-2 must be gone after delete"
    );

    let uid4 = Uuid::new_v4();
    EntityExample::insert_one(
        executor,
        &EntityExample {
            unit_id: uid4,
            region: "West".into(),
            callsign: "Delta-4".into(),
            casualties: 0,
            metadata: Notes("tmp".into()),
            transient_cache: HashMap::new(),
        },
    )
    .await
    .expect("insert Delta-4");

    #[cfg(not(feature = "disable-scanning"))]
    {
        EntityExample::delete_many(executor, expr!(EntityExample::casualties == 0))
            .await
            .expect("delete_many literal");
        assert!(
            EntityExample::find_one(
                executor,
                expr!(EntityExample::unit_id == #uid4 && EntityExample::region == "West")
            )
            .await
            .expect("find Delta-4 after delete_many")
            .is_none(),
            "Delta-4 (casualties=0) must be gone after delete_many"
        );
    }
    #[cfg(feature = "disable-scanning")]
    {
        EntityExample::delete_many(
            executor,
            expr!(EntityExample::unit_id == #uid4 && EntityExample::region == "West"),
        )
        .await
        .expect("delete Delta-4 by PK");
    }

    let uid = uid3;
    EntityExample::delete_many(executor, expr!(EntityExample::unit_id == #uid))
        .await
        .expect("delete_many variable");
    assert!(
        EntityExample::find_one(
            executor,
            expr!(EntityExample::unit_id == #uid3 && EntityExample::region == "East")
        )
        .await
        .expect("find Charlie after delete_many")
        .is_none(),
        "Charlie-3 must be gone after delete_many"
    );

    let uid = uid1;
    let _ = EntityExample::find_many(executor, expr!(EntityExample::unit_id == #uid), None)
        .try_collect::<Vec<_>>()
        .await
        .expect("expr uid == #uid");

    #[cfg(not(feature = "disable-scanning"))]
    {
        let _ = EntityExample::find_many(executor, expr!(EntityExample::casualties == 0), None)
            .try_collect::<Vec<_>>()
            .await
            .expect("expr casualties == 0");

        let _ = EntityExample::find_many(executor, expr!(EntityExample::casualties >= 10), None)
            .try_collect::<Vec<_>>()
            .await
            .expect("expr casualties >= 10");

        let _ = EntityExample::find_many(
            executor,
            expr!(EntityExample::region == "North" || EntityExample::region == "South"),
            None,
        )
        .try_collect::<Vec<_>>()
        .await
        .expect("expr OR");

        let _ = EntityExample::find_many(
            executor,
            expr!(EntityExample::callsign == "Alpha%" as LIKE),
            None,
        )
        .try_collect::<Vec<_>>()
        .await
        .expect("expr LIKE");

        let _ = EntityExample::find_many(
            executor,
            expr!(EntityExample::callsign != "Alpha%" as LIKE),
            None,
        )
        .try_collect::<Vec<_>>()
        .await
        .expect("expr NOT LIKE");
    }

    let uid5 = Uuid::new_v4();
    EntityExample::insert_one(
        executor,
        &EntityExample {
            unit_id: uid5,
            region: "PrepRegion".into(),
            callsign: "Echo-5".into(),
            casualties: 20,
            metadata: Notes("prep".into()),
            transient_cache: HashMap::new(),
        },
    )
    .await
    .expect("insert Echo-5");

    let mut query =
        EntityExample::prepare_find(executor, expr!(EntityExample::unit_id == ?), Some(50))
            .await
            .expect("prepare_find");
    query.bind(uid5).expect("bind uid5");
    let prepared_results = executor
        .fetch(&mut query)
        .map_ok(|row| EntityExample::from_row(row).unwrap())
        .try_collect::<Vec<EntityExample>>()
        .await
        .expect("fetch prepared");
    assert_eq!(prepared_results.len(), 1);
    assert_eq!(prepared_results[0].callsign, "Echo-5");

    query.clear_bindings().expect("clear bindings");
    query.bind(Uuid::nil()).expect("bind nil");
    let empty_results = executor
        .fetch(&mut query)
        .map_ok(|row| EntityExample::from_row(row).unwrap())
        .try_collect::<Vec<EntityExample>>()
        .await
        .expect("fetch prepared reuse");
    assert!(empty_results.is_empty(), "nil UUID must return nothing");

    #[cfg(not(feature = "disable-scanning"))]
    {
        let results = executor
            .fetch(
                QueryBuilder::new()
                    .select(EntityExample::columns())
                    .from(EntityExample::table())
                    .where_expr(expr!(EntityExample::casualties > 0))
                    .order_by(cols!(EntityExample::casualties DESC))
                    .limit(Some(50))
                    .build(&executor.driver()),
            )
            .map_ok(|row| EntityExample::from_row(row).unwrap())
            .try_collect::<Vec<_>>()
            .await
            .expect("query builder ordering");
        assert!(!results.is_empty());
        if results.len() > 1 {
            assert!(results[0].casualties >= results[1].casualties);
        }
    }

    let mut raw_query = executor
        .prepare(
            QueryBuilder::new()
                .select(EntityExample::columns())
                .from(EntityExample::table())
                .where_expr(expr!(EntityExample::unit_id == ?))
                .build(&executor.driver()),
        )
        .await
        .expect("prepare raw query");

    raw_query.bind(uid5).expect("bind uid5 raw");
    let raw_results: Vec<EntityExample> = executor
        .fetch(&mut raw_query)
        .map_ok(|row| EntityExample::from_row(row).unwrap())
        .try_collect()
        .await
        .expect("raw prepared fetch");
    assert_eq!(raw_results.len(), 1);

    raw_query.clear_bindings().expect("clear raw bindings");
    raw_query.bind(Uuid::nil()).expect("bind nil raw");
    let raw_empty: Vec<EntityExample> = executor
        .fetch(&mut raw_query)
        .map_ok(|row| EntityExample::from_row(row).unwrap())
        .try_collect()
        .await
        .expect("raw prepared reuse");
    assert!(raw_empty.is_empty());

    #[cfg(not(feature = "disable-multiple-statements"))]
    {
        let writer = executor.driver().sql_writer();
        let mut query = DynQuery::default();
        writer.write_select(
            &mut query,
            &QueryBuilder::new()
                .select(EntityExample::columns())
                .from(EntityExample::table())
                .where_expr(true)
                .limit(Some(10)),
        );
        let results: Vec<QueryResult> = executor
            .run(query)
            .try_collect()
            .await
            .expect("sqlwriter run");
        assert!(
            results.iter().any(|r| matches!(r, QueryResult::Row(..))),
            "SqlWriter must stream at least one Row"
        );
    }

    #[cfg(not(feature = "disable-transactions"))]
    {
        let uid6 = Uuid::new_v4();
        let echo = EntityExample {
            unit_id: uid6,
            region: "TxRegion".into(),
            callsign: "Foxtrot-6".into(),
            casualties: 0,
            metadata: Notes("tx".into()),
            transient_cache: HashMap::new(),
        };

        let mut tx = executor.begin().await.expect("begin tx");

        echo.save(&mut tx).await.expect("save in tx");

        EntityExample::delete_many(&mut tx, expr!(EntityExample::unit_id == #uid5))
            .await
            .expect("delete_many in tx");

        tx.commit().await.expect("commit tx");

        assert!(
            EntityExample::find_one(executor, echo.primary_key_expr())
                .await
                .expect("find after commit")
                .is_some(),
            "entity saved in committed transaction must be visible"
        );

        assert!(
            EntityExample::find_one(
                executor,
                expr!(EntityExample::unit_id == #uid5 && EntityExample::region == "PrepRegion")
            )
            .await
            .expect("find Echo-5 after commit")
            .is_none(),
            "entity deleted in committed transaction must be gone"
        );
    }
}