Skip to content

Commit 804ce9d

Browse files
committed
test: add live database tests for upsert operations
1 parent dee9eff commit 804ce9d

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

‎tests/_support/Config/Registrar.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ public static function Database(): array
173173
(int) $dbParams['port'],
174174
);
175175
if (! $conn->connect_error) {
176-
$conn->query('CREATE DATABASE IF NOT EXISTS ' . $conn->real_escape_string($dbParams['database']));
176+
$dbName = '`' . str_replace('`', '``', $dbParams['database']) . '`';
177+
$conn->query('CREATE DATABASE IF NOT EXISTS ' . $dbName);
177178
$conn->close();
178179
}
179180
} elseif ($group === 'Postgre') {

‎tests/system/Database/Live/GetVersionTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ public function testGetVersion(): void
3838
$this->db->connID = false;
3939

4040
$version = $this->db->getVersion();
41-
$this->assertMatchesRegularExpression('/\A\d+(\.\d+)*/', $version);
41+
$this->assertMatchesRegularExpression('/\A\d+(\.\d+)*(?:[-+._][a-zA-Z0-9._+-]+)?\z/', $version);
4242
}
4343
}

‎tests/system/Database/Live/UpsertTest.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,31 @@ public function testUpsertBatchCompositeUniqueIndex(): void
636636
$this->dontSeeInDatabase('team_members', ['team_id' => 1, 'person_id' => 33, 'role' => 'mentor']);
637637
}
638638

639+
public function testUpsertBatchPrefersPrimaryKeyOverUniqueIndex(): void
640+
{
641+
$userData = [
642+
[
643+
'id' => 1,
644+
'email' => 'updated_email_for_id1@world.com',
645+
'name' => 'Derek Jones Updated',
646+
'country' => 'US',
647+
],
648+
];
649+
650+
if ($this->db->DBDriver === 'Postgre') {
651+
$sql = $this->db->table('user')->setData($userData)->getCompiledUpsert();
652+
$this->assertStringContainsString('ON CONFLICT ("id")', $sql);
653+
}
654+
655+
$this->db->table('user')->upsertBatch($userData);
656+
657+
$this->seeInDatabase('user', [
658+
'id' => 1,
659+
'email' => 'updated_email_for_id1@world.com',
660+
'name' => 'Derek Jones Updated',
661+
]);
662+
}
663+
639664
public function testSetBatchOneRow(): void
640665
{
641666
$data = [

0 commit comments

Comments
 (0)