toasty-driver-integration-suite 0.5.0

Integration test suite for Toasty database drivers
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use crate::prelude::*;

use toasty_core::driver::{Operation, operation::IsolationLevel, operation::Transaction};

// ===== Basic commit / rollback =====

/// Data created inside a committed transaction is visible afterwards.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn commit_persists_data(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// Data created inside a rolled-back transaction is not visible.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn rollback_discards_data(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Ghost").exec(&mut tx).await?;
    tx.rollback().await?;

    let users = User::all().exec(&mut db).await?;
    assert!(users.is_empty());

    Ok(())
}

/// Dropping a transaction without commit or rollback automatically rolls back.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn drop_without_finalize_rolls_back(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    {
        let mut tx = db.transaction().await?;
        User::create().name("Ghost").exec(&mut tx).await?;
        // tx is dropped here without commit/rollback
    }

    let users = User::all().exec(&mut db).await?;
    assert!(users.is_empty());

    Ok(())
}

/// Multiple operations inside a single transaction are all committed together.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn multiple_ops_in_transaction(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    User::create().name("Bob").exec(&mut tx).await?;
    User::create().name("Carol").exec(&mut tx).await?;
    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 3);

    Ok(())
}

/// Read-your-writes: data created inside a transaction is visible within it
/// before commit.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn read_your_writes(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;

    let users = User::all().exec(&mut tx).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    tx.commit().await?;

    Ok(())
}

/// Updates inside a transaction are committed.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn update_inside_transaction(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut user = User::create().name("Alice").exec(&mut db).await?;

    let mut tx = db.transaction().await?;
    user.update().name("Bob").exec(&mut tx).await?;
    tx.commit().await?;

    let reloaded = User::get_by_id(&mut db, user.id).await?;
    assert_eq!(reloaded.name, "Bob");

    Ok(())
}

/// Updates inside a rolled-back transaction are discarded.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn update_rolled_back(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut user = User::create().name("Alice").exec(&mut db).await?;

    let mut tx = db.transaction().await?;
    user.update().name("Bob").exec(&mut tx).await?;
    tx.rollback().await?;

    let reloaded = User::get_by_id(&mut db, user.id).await?;
    assert_eq!(reloaded.name, "Alice");

    Ok(())
}

/// Deletes inside a rolled-back transaction are discarded.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn delete_rolled_back(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let user = User::create().name("Alice").exec(&mut db).await?;

    let mut tx = db.transaction().await?;
    User::filter_by_id(user.id).delete().exec(&mut tx).await?;
    tx.rollback().await?;

    let reloaded = User::get_by_id(&mut db, user.id).await?;
    assert_eq!(reloaded.name, "Alice");

    Ok(())
}

// ===== Driver operation log =====

/// Verify the driver receives BEGIN, statements, and COMMIT in the right order.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn driver_sees_begin_commit(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.commit().await?;

    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: None,
            read_only: false
        })
    );
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_)); // INSERT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Commit)
    );
    assert!(t.log().is_empty());

    Ok(())
}

/// Verify the driver receives BEGIN and ROLLBACK when rolled back.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn driver_sees_begin_rollback(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.rollback().await?;

    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: None,
            read_only: false
        })
    );
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_)); // INSERT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Rollback)
    );
    assert!(t.log().is_empty());

    Ok(())
}

// ===== Nested transactions (savepoints) =====

/// A committed nested transaction (savepoint) persists when the outer
/// transaction also commits.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn nested_commit_both(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;

    {
        let mut nested = tx.transaction().await?;
        User::create().name("Bob").exec(&mut nested).await?;
        nested.commit().await?;
    }

    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 2);

    Ok(())
}

/// Rolling back a nested transaction discards only its changes; the outer
/// transaction can still commit its own.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn nested_rollback_inner(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;

    {
        let mut nested = tx.transaction().await?;
        User::create().name("Ghost").exec(&mut nested).await?;
        nested.rollback().await?;
    }

    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// Rolling back the outer transaction discards everything, including changes
/// from an already-committed nested transaction.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn nested_rollback_outer(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;

    {
        let mut nested = tx.transaction().await?;
        User::create().name("Bob").exec(&mut nested).await?;
        nested.commit().await?;
    }

    tx.rollback().await?;

    let users = User::all().exec(&mut db).await?;
    assert!(users.is_empty());

    Ok(())
}

/// Dropping a nested transaction without finalize rolls back just that
/// savepoint.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn nested_drop_rolls_back_savepoint(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;

    {
        let mut nested = tx.transaction().await?;
        User::create().name("Ghost").exec(&mut nested).await?;
        // dropped without commit/rollback
    }

    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// Verify the driver log for a nested transaction shows SAVEPOINT / RELEASE
/// SAVEPOINT around the inner work.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn nested_driver_sees_savepoint_ops(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let mut tx = db.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;

    let mut nested = tx.transaction().await?;
    User::create().name("Bob").exec(&mut nested).await?;
    nested.commit().await?;

    tx.commit().await?;

    // BEGIN
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: None,
            read_only: false
        })
    );
    // INSERT Alice
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_));
    // SAVEPOINT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Savepoint(_))
    );
    // INSERT Bob
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_));
    // RELEASE SAVEPOINT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::ReleaseSavepoint(_))
    );
    // COMMIT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Commit)
    );
    assert!(t.log().is_empty());

    Ok(())
}

/// Verify the driver log when a nested transaction is rolled back shows
/// ROLLBACK TO SAVEPOINT.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn nested_driver_sees_rollback_to_savepoint(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let mut tx = db.transaction().await?;

    let mut nested = tx.transaction().await?;
    User::create().name("Ghost").exec(&mut nested).await?;
    nested.rollback().await?;

    tx.commit().await?;

    // BEGIN
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: None,
            read_only: false
        })
    );
    // SAVEPOINT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Savepoint(_))
    );
    // INSERT Ghost
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_));
    // ROLLBACK TO SAVEPOINT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::RollbackToSavepoint(_))
    );
    // COMMIT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Commit)
    );
    assert!(t.log().is_empty());

    Ok(())
}

/// Two sequential nested transactions: first committed, second rolled back.
/// Only data from the first survives.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn two_sequential_nested_transactions(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction().await?;

    {
        let mut nested1 = tx.transaction().await?;
        User::create().name("Alice").exec(&mut nested1).await?;
        nested1.commit().await?;
    }

    {
        let mut nested2 = tx.transaction().await?;
        User::create().name("Ghost").exec(&mut nested2).await?;
        nested2.rollback().await?;
    }

    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

// ===== Statements inside transactions use savepoints for multi-op plans =====

/// When a multi-op statement (e.g. create with association) runs inside an
/// interactive transaction, the engine wraps it in SAVEPOINT/RELEASE instead
/// of BEGIN/COMMIT.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::has_many_belongs_to))]
pub async fn multi_op_inside_tx_uses_savepoints(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let mut tx = db.transaction().await?;
    let user = User::create()
        .name("Alice")
        .todo(Todo::create().title("task"))
        .exec(&mut tx)
        .await?;
    tx.commit().await?;

    // BEGIN (interactive tx)
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: None,
            read_only: false
        })
    );
    // SAVEPOINT (engine wraps the multi-op plan)
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Savepoint(_))
    );
    // INSERT user
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_));
    // INSERT todo
    assert_struct!(t.log().pop_op(), Operation::QuerySql(_));
    // RELEASE SAVEPOINT
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::ReleaseSavepoint(_))
    );
    // COMMIT (interactive tx)
    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Commit)
    );
    assert!(t.log().is_empty());

    // Verify the data landed
    let todos = user.todos().exec(&mut db).await?;
    assert_eq!(todos.len(), 1);
    assert_eq!(todos[0].title, "task");

    Ok(())
}

// ===== TransactionBuilder API =====

/// TransactionBuilder from Db commits data like a regular transaction.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn builder_on_db_commit(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let mut tx = db.transaction_builder().begin().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// TransactionBuilder from Connection commits data like a regular transaction.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn builder_on_connection_commit(t: &mut Test) -> Result<()> {
    let db = setup(t).await;
    let mut conn = db.connection().await?;

    let mut tx = conn.transaction_builder().begin().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.commit().await?;

    let users = User::all().exec(&mut conn).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}

/// TransactionBuilder with isolation level sends the correct option to the driver.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn builder_with_isolation_level(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let mut tx = db
        .transaction_builder()
        .isolation(IsolationLevel::Serializable)
        .begin()
        .await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.commit().await?;

    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: Some(IsolationLevel::Serializable),
            read_only: false
        })
    );

    Ok(())
}

/// TransactionBuilder with read_only sends the correct option to the driver.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn builder_with_read_only(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let tx = db.transaction_builder().read_only(true).begin().await?;
    tx.commit().await?;

    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: None,
            read_only: true
        })
    );

    Ok(())
}

/// TransactionBuilder with both isolation and read_only sends both options.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn builder_with_all_options(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    t.log().clear();

    let tx = db
        .transaction_builder()
        .isolation(IsolationLevel::Serializable)
        .read_only(true)
        .begin()
        .await?;
    tx.commit().await?;

    assert_struct!(
        t.log().pop_op(),
        Operation::Transaction(Transaction::Start {
            isolation: Some(IsolationLevel::Serializable),
            read_only: true
        })
    );

    Ok(())
}

/// TransactionBuilder auto-rolls back on drop just like a regular transaction.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn builder_drop_rolls_back(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    {
        let mut tx = db.transaction_builder().begin().await?;
        User::create().name("Ghost").exec(&mut tx).await?;
    }

    let users = User::all().exec(&mut db).await?;
    assert!(users.is_empty());

    Ok(())
}

/// Calling `.transaction()` through `&mut dyn Executor` works.
#[driver_test(id(ID), requires(sql), scenario(crate::scenarios::two_models))]
pub async fn transaction_via_dyn_executor(t: &mut Test) -> Result<()> {
    let mut db = setup(t).await;

    let executor: &mut dyn toasty::Executor = &mut db;
    let mut tx = executor.transaction().await?;
    User::create().name("Alice").exec(&mut tx).await?;
    tx.commit().await?;

    let users = User::all().exec(&mut db).await?;
    assert_eq!(users.len(), 1);
    assert_eq!(users[0].name, "Alice");

    Ok(())
}