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
413
414
415
416
417
418
419
420
421
422
423
424
425
#![allow(unused_imports)]
use std::{collections::HashSet, pin::pin, sync::LazyLock};
use tank::{
    AsValue, Dataset, Driver, DynQuery, Entity, Executor, Query, QueryBuilder, QueryResult, Row,
    SqlWriter, Value, cols, expr, join,
    stream::{StreamExt, TryStreamExt},
};
use tokio::sync::Mutex;
use uuid::Uuid;

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

#[derive(Entity, Clone, PartialEq, Debug)]
#[tank(schema = "testing", name = "authors")]
pub struct Author {
    #[tank(primary_key, name = "author_id")]
    pub id: Uuid,
    pub name: String,
    pub country: String,
    pub books_published: Option<u16>,
}

#[derive(Entity, Clone, PartialEq, Debug)]
#[tank(schema = "testing", name = "books", primary_key = (Self::title, Self::author))]
pub struct Book {
    #[cfg(not(feature = "disable-arrays"))]
    pub isbn: [u8; 13],
    #[tank(column_type = (mysql = "VARCHAR(255)"))]
    pub title: String,
    /// Main author
    #[tank(references = Author::id)]
    pub author: Uuid,
    #[tank(references = Author::id)]
    pub co_author: Option<Uuid>,
    pub year: i32,
}

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

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

    // Author objects
    let authors = vec![
        Author {
            id: Uuid::parse_str("f938f818-0a40-4ce3-8fbc-259ac252a1b5").unwrap(),
            name: "J.K. Rowling".into(),
            country: "UK".into(),
            books_published: 24.into(),
        },
        Author {
            id: Uuid::parse_str("a73bc06a-ff89-44b9-a62f-416ebe976285").unwrap(),
            name: "J.R.R. Tolkien".into(),
            country: "USA".into(),
            books_published: 6.into(),
        },
        Author {
            id: Uuid::parse_str("6b2f56a1-316d-42b9-a8ba-baca42c5416c").unwrap(),
            name: "Dmitrij Gluchovskij".into(),
            country: "Russia".into(),
            books_published: 7.into(),
        },
        Author {
            id: Uuid::parse_str("d3d3d3d3-d3d3-d3d3-d3d3-d3d3d3d3d3d3").unwrap(),
            name: "Linus Torvalds".into(),
            country: "Finland".into(),
            books_published: None,
        },
    ];
    let rowling_id = authors[0].id.clone();
    let tolkien_id = authors[1].id.clone();
    let gluchovskij_id = authors[2].id.clone();

    // Book objects
    let books = vec![
        Book {
            #[cfg(not(feature = "disable-arrays"))]
            isbn: [9, 7, 8, 0, 7, 4, 7, 5, 3, 2, 6, 9, 9],
            title: "Harry Potter and the Philosopher's Stone".into(),
            author: rowling_id,
            co_author: None,
            year: 1937,
        },
        Book {
            #[cfg(not(feature = "disable-arrays"))]
            isbn: [9, 7, 8, 0, 7, 4, 7, 5, 9, 1, 0, 5, 4],
            title: "Harry Potter and the Deathly Hallows".into(),
            author: rowling_id,
            co_author: None,
            year: 2007,
        },
        Book {
            #[cfg(not(feature = "disable-arrays"))]
            isbn: [9, 7, 8, 0, 6, 1, 8, 2, 6, 0, 3, 0, 0],
            title: "The Hobbit".into(),
            author: tolkien_id,
            co_author: None,
            year: 1996,
        },
        Book {
            #[cfg(not(feature = "disable-arrays"))]
            isbn: [9, 7, 8, 5, 1, 7, 0, 5, 9, 6, 7, 8, 2],
            title: "Metro 2033".into(),
            author: gluchovskij_id,
            co_author: None,
            year: 2002,
        },
        Book {
            #[cfg(not(feature = "disable-arrays"))]
            isbn: [9, 7, 8, 0, 0, 2, 3, 4, 5, 6, 7, 8, 9],
            title: "Hogwarts 2033".into(),
            author: rowling_id,
            co_author: gluchovskij_id.into(),
            year: 2026,
        },
    ];

    // Insert
    let result = Author::insert_many(executor, authors.iter())
        .await
        .expect("Failed to insert authors");
    if let Some(affected) = result.rows_affected {
        assert_eq!(affected, 4);
    }
    let result = Book::insert_many(executor, books.iter())
        .await
        .expect("Failed to insert books");
    if let Some(affected) = result.rows_affected {
        assert_eq!(affected, 5);
    }

    // Find authors
    let id = Uuid::parse_str("f938f818-0a40-4ce3-8fbc-259ac252a1b5")
        .unwrap()
        .as_value();
    let author = Author::find_one(executor, expr!(Author::id == #id))
        .await
        .expect("Failed to query author by pk");
    assert_eq!(
        author,
        Some(Author {
            id: Uuid::parse_str("f938f818-0a40-4ce3-8fbc-259ac252a1b5").unwrap(),
            name: "J.K. Rowling".into(),
            country: "UK".into(),
            books_published: 24.into(),
        })
    );

    let author = Author::find_one(executor, expr!(Author::name == "Linus Torvalds"))
        .await
        .expect("Failed to query author by pk");
    assert_eq!(
        author,
        Some(Author {
            id: Uuid::parse_str("d3d3d3d3-d3d3-d3d3-d3d3-d3d3d3d3d3d3").unwrap(),
            name: "Linus Torvalds".into(),
            country: "Finland".into(),
            books_published: None,
        })
    );

    #[cfg(not(feature = "disable-joins"))]
    {
        // Get books before 2000
        #[derive(Entity, PartialEq, Debug)]
        struct BookAuthorResult {
            #[tank(name = "title")]
            book: String,
            #[tank(name = "name")]
            author: String,
        }
        let result = executor
            .fetch(
                QueryBuilder::new()
                    .select(cols!(B.title, A.name))
                    .from(join!(Book B JOIN Author A ON B.author == A.author_id))
                    .where_expr(expr!(B.year < 2000))
                    .order_by(cols!(B.title DESC))
                    .build(&executor.driver()),
            )
            .map_ok(BookAuthorResult::from_row)
            .map(Result::flatten)
            .try_collect::<Vec<_>>()
            .await
            .expect("Failed to query books and authors joined");
        assert_eq!(
            result,
            [
                BookAuthorResult {
                    book: "The Hobbit".into(),
                    author: "J.R.R. Tolkien".into()
                },
                BookAuthorResult {
                    book: "Harry Potter and the Philosopher's Stone".into(),
                    author: "J.K. Rowling".into(),
                },
            ]
        );

        // Get all books with their authors
        let dataset = join!(
            Book B LEFT JOIN Author A1 ON B.author == A1.author_id
                LEFT JOIN Author A2 ON B.co_author == A2.author_id
        );
        let result = executor
            .fetch(
                QueryBuilder::new()
                    .select(cols!(B.title, A1.name as author, A2.name as co_author))
                    .from(dataset)
                    .where_expr(true)
                    .build(&executor.driver()),
            )
            .try_collect::<Vec<Row>>()
            .await
            .expect("Failed to query books and authors joined")
            .into_iter()
            .map(|row| {
                let mut iter = row.values.into_iter();
                (
                    match iter.next().unwrap() {
                        Value::Varchar(Some(v)) => v,
                        Value::Unknown(Some(v)) => v.into(),
                        v => panic!("Expected 1st value to be non null varchar, found {v:?}"),
                    },
                    match iter.next().unwrap() {
                        Value::Varchar(Some(v)) => v,
                        Value::Unknown(Some(v)) => v.into(),
                        v => panic!("Expected 2nd value to be non null varchar, found {v:?}"),
                    },
                    match iter.next().unwrap() {
                        Value::Varchar(Some(v)) => Some(v),
                        Value::Unknown(Some(v)) => Some(v.into()),
                        Value::Varchar(None) | Value::Null => None,
                        v => panic!(
                            "Expected 3rd value to be a Some(Value::Varchar(..)) | Value::Unknown(Some(..)) | Some(Value::Null)), found {v:?}",
                        ),
                    },
                )
            })
            .collect::<HashSet<_>>();
        assert_eq!(
            result,
            HashSet::from_iter([
                (
                    "Harry Potter and the Philosopher's Stone".into(),
                    "J.K. Rowling".into(),
                    None
                ),
                (
                    "Harry Potter and the Deathly Hallows".into(),
                    "J.K. Rowling".into(),
                    None
                ),
                ("The Hobbit".into(), "J.R.R. Tolkien".into(), None),
                ("Metro 2033".into(), "Dmitrij Gluchovskij".into(), None),
                (
                    "Hogwarts 2033".into(),
                    "J.K. Rowling".into(),
                    Some("Dmitrij Gluchovskij".into())
                ),
            ])
        );

        // Get book and author pairs
        #[derive(Entity, PartialEq, Eq, Hash, Debug)]
        struct Books {
            pub title: Option<String>,
            pub author: Option<String>,
        }
        let books = executor
            .fetch(
                QueryBuilder::new()
                    .select(cols!(Book::title, Author::name as author, Book::year))
                    .from(join!(Book JOIN Author ON Book::author == Author::id))
                    .where_expr(true)
                    .build(&executor.driver()),
            )
            .and_then(|row| async { Books::from_row(row) })
            .try_collect::<HashSet<_>>()
            .await
            .expect("Could not return the books");
        assert_eq!(
            books,
            HashSet::from_iter([
                Books {
                    title: Some("Harry Potter and the Philosopher's Stone".into()),
                    author: Some("J.K. Rowling".into())
                },
                Books {
                    title: Some("Harry Potter and the Deathly Hallows".into()),
                    author: Some("J.K. Rowling".into())
                },
                Books {
                    title: Some("The Hobbit".into()),
                    author: Some("J.R.R. Tolkien".into())
                },
                Books {
                    title: Some("Metro 2033".into()),
                    author: Some("Dmitrij Gluchovskij".into())
                },
                Books {
                    title: Some("Hogwarts 2033".into()),
                    author: Some("J.K. Rowling".into())
                },
            ])
        );
    }

    #[cfg(not(feature = "disable-references"))]
    {
        // Insert book violating referential integrity
        use crate::silent_logs;
        let book = Book {
            #[cfg(not(feature = "disable-arrays"))]
            isbn: [9, 7, 8, 1, 7, 3, 3, 5, 6, 1, 0, 8, 0],
            title: "My book".into(),
            author: Uuid::parse_str("c18c04b4-1aae-48a3-9814-9b70f7a38315").unwrap(),
            co_author: None,
            year: 2025,
        };
        silent_logs! {
            assert!(
                book.save(executor).await.is_err(),
                "Must fail to save book violating referential integrity"
            );
        }
    }

    #[cfg(not(feature = "disable-ordering"))]
    {
        // Authors names alphabetical order
        let authors = executor
            .fetch(
                QueryBuilder::new()
                    .select([Author::name])
                    .from(Author::table())
                    .where_expr(true)
                    .order_by(cols!(Author::name ASC))
                    .build(&executor.driver()),
            )
            .and_then(|row| async move { AsValue::try_from_value((*row.values)[0].clone()) })
            .try_collect::<Vec<String>>()
            .await
            .expect("Could not return the ordered names of the authors");
        assert_eq!(
            authors,
            vec![
                "Dmitrij Gluchovskij".to_string(),
                "J.K. Rowling".to_string(),
                "J.R.R. Tolkien".to_string(),
                "Linus Torvalds".to_string(),
            ]
        )
    }

    // Multiple statements
    #[cfg(not(feature = "disable-multiple-statements"))]
    {
        let mut query = DynQuery::default();
        let writer = executor.driver().sql_writer();
        writer.write_select(
            &mut query,
            &QueryBuilder::new()
                .select(Book::columns())
                .from(Book::table())
                .where_expr(expr!(Book::title == "Metro 2033"))
                .limit(Some(1)),
        );
        writer.write_select(
            &mut query,
            &QueryBuilder::new()
                .select(Book::columns())
                .from(Book::table())
                .where_expr(expr!(Book::title == "Harry Potter and the Deathly Hallows"))
                .limit(Some(1)),
        );
        let mut stream = pin!(executor.run(query));
        let Some(Ok(QueryResult::Row(row))) = stream.next().await else {
            panic!("Could not get the first row")
        };
        let book = Book::from_row(row).expect("Could not get the book from row");
        assert_eq!(
            book,
            Book {
                #[cfg(not(feature = "disable-arrays"))]
                isbn: [9, 7, 8, 5, 1, 7, 0, 5, 9, 6, 7, 8, 2],
                title: "Metro 2033".into(),
                author: gluchovskij_id,
                co_author: None,
                year: 2002,
            }
        );
        let Some(Ok(QueryResult::Row(row))) = stream.next().await else {
            panic!("Could not get the second row")
        };
        let book = Book::from_row(row).expect("Could not get the book from row");
        assert_eq!(
            book,
            Book {
                #[cfg(not(feature = "disable-arrays"))]
                isbn: [9, 7, 8, 0, 7, 4, 7, 5, 9, 1, 0, 5, 4],
                title: "Harry Potter and the Deathly Hallows".into(),
                author: rowling_id,
                co_author: None,
                year: 2007,
            }
        );
        assert!(
            stream.next().await.is_none(),
            "The stream should return only two rows"
        )
    }
}