Skip to content

Commit

Permalink
test(transaction): add some test cases for nested transactions
Browse files Browse the repository at this point in the history
Test whether we can start two nested transactions ...

1. ... complete them and see the result in the database
2. ... rollback the outer one after completing the inner one,
   and see that the result is _not_ in the database.
3. ... roll back the inner one and see if the result
      of the outer one is in the database after completing.
  • Loading branch information
Moritz Küttel committed Apr 27, 2024
1 parent 747108f commit c346098
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions tests/system/Database/Live/TransactionDBDebugTrueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,4 +234,90 @@ public function testTransBegin(): void

$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
}

public function testNestedTransactions(): void
{
$builder = $this->db->table('job');

$this->db->transStart() or $this->fail('Failed to start transaction');

$jobData = [
'name' => 'Grocery Sales',
'description' => 'Fresh!',
];
$builder->insert($jobData);

$this->db->transStart() or $this->fail('Failed to start inner transaction');

$jobData = [
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);

$this->db->transComplete() or $this->fail('Failed to complete inner transaction');

$this->db->transComplete() or $this->fail('Failed to complete outer transaction');

$this->seeInDatabase('job', ['name' => 'Grocery Sales']);
$this->seeInDatabase('job', ['name' => 'Comedian']);
}


public function testNestedTransactionsRollbackOuter(): void
{
$builder = $this->db->table('job');

$this->db->transStart() or $this->fail('Failed to start transaction');

$jobData = [
'name' => 'Grocery Sales',
'description' => 'Fresh!',
];
$builder->insert($jobData);

$this->db->transStart() or $this->fail('Failed to start inner transaction');

$jobData = [
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);

$this->db->transComplete() or $this->fail('Failed to complete inner transaction');

$this->db->transRollback() or $this->fail('Failed to rollback outer transaction');

$this->dontSeeInDatabase('job', ['name' => 'Grocery Sales']);
$this->dontSeeInDatabase('job', ['name' => 'Comedian']);
}


public function testNestedTransactionsRollbackInner(): void
{
$builder = $this->db->table('job');

$this->db->transStart() or $this->fail('Failed to start transaction');

$jobData = [
'name' => 'Grocery Sales',
'description' => 'Fresh!',
];
$builder->insert($jobData);

$this->db->transStart() or $this->fail('Failed to start inner transaction');

$jobData = [
'name' => 'Comedian',
'description' => 'Theres something in your teeth',
];
$builder->insert($jobData);

$this->db->transRollback() or $this->fail('Failed to rollback inner transaction');

$this->db->transComplete() or $this->fail('Failed to complete outer transaction');

$this->seeInDatabase('job', ['name' => 'Grocery Sales']);
$this->dontSeeInDatabase('job', ['name' => 'Comedian']);
}
}

0 comments on commit c346098

Please sign in to comment.