Skip to content

Commit

Permalink
Add test resetSequence() for SQLite. (#19895)
Browse files Browse the repository at this point in the history
* Add test `resetSequence()`.

* add more test.
  • Loading branch information
terabytesoftw committed Jul 17, 2023
1 parent 4352b87 commit 76d6345
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/framework/db/sqlite/CommandTest.php
Expand Up @@ -7,6 +7,8 @@

namespace yiiunit\framework\db\sqlite;

use yii\db\sqlite\Schema;

/**
* @group db
* @group sqlite
Expand Down Expand Up @@ -109,4 +111,60 @@ public function batchInsertSqlProvider()

return $parent;
}

public function testResetSequence()
{
$db = $this->getConnection();

if ($db->getTableSchema('reset_sequence', true) !== null) {
$db->createCommand()->dropTable('reset_sequence')->execute();
}

// create table reset_sequence
$db->createCommand()->createTable(
'reset_sequence',
[
'id' => Schema::TYPE_PK,
'description' => Schema::TYPE_TEXT,
]
)->execute();

// ensure auto increment is working
$db->createCommand()->insert('reset_sequence', ['description' => 'test'])->execute();
$this->assertEquals(1, $db->createCommand('SELECT MAX([[id]]) FROM {{reset_sequence}}')->queryScalar());

// remove all records
$db->createCommand()->delete('reset_sequence')->execute();
$this->assertEquals(0, $db->createCommand('SELECT COUNT(*) FROM {{reset_sequence}}')->queryScalar());

// counter should be reset to 1
$db->createCommand()->resetSequence('reset_sequence')->execute();
$db->createCommand()->insert('reset_sequence', ['description' => 'test'])->execute();
$this->assertEquals(1, $db->createCommand('SELECT COUNT(*) FROM {{reset_sequence}}')->queryScalar());
$this->assertEquals(1, $db->createCommand('SELECT MAX([[id]]) FROM {{reset_sequence}}')->queryScalar());

// counter should be reset to 5, so next record gets ID 5
$db->createCommand()->resetSequence('reset_sequence', 5)->execute();
$db->createCommand()->insert('reset_sequence', ['description' => 'test'])->execute();
$this->assertEquals(2, $db->createCommand('SELECT COUNT(*) FROM {{reset_sequence}}')->queryScalar());
$this->assertEquals(5, $db->createCommand('SELECT MAX([[id]]) FROM {{reset_sequence}}')->queryScalar());
}

public function testResetSequenceExceptionTableNoExist()
{
$this->expectException('yii\base\InvalidArgumentException');
$this->expectExceptionMessage('Table not found: no_exist_table');

$db = $this->getConnection();
$db->createCommand()->resetSequence('no_exist_table', 5)->execute();
}

public function testResetSequenceExceptionSquenceNoExist()
{
$this->expectException('yii\base\InvalidArgumentException');
$this->expectExceptionMessage("There is not sequence associated with table 'type'.");

$db = $this->getConnection();
$db->createCommand()->resetSequence('type', 5)->execute();
}
}

0 comments on commit 76d6345

Please sign in to comment.