db2 driver updates
This commit is contained in:
@@ -0,0 +1,223 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Db2;
|
||||
|
||||
class UpsertTest extends Db2TestCase
|
||||
{
|
||||
private string $tableName = '';
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->tableName = 'DB2_TEST.DB2T_' .
|
||||
strtoupper(bin2hex(random_bytes(6)));
|
||||
|
||||
$this->db()->statement("
|
||||
CREATE TABLE {$this->tableName} (
|
||||
ID INTEGER NOT NULL,
|
||||
CODE VARCHAR(30) NOT NULL,
|
||||
NAME VARCHAR(100) NOT NULL,
|
||||
STATUS VARCHAR(20) NOT NULL,
|
||||
PRIMARY KEY (ID),
|
||||
CONSTRAINT UQ_CODE UNIQUE (CODE)
|
||||
)
|
||||
");
|
||||
|
||||
$this->db()->table($this->tableName)->insert([
|
||||
'ID' => 1,
|
||||
'CODE' => 'A001',
|
||||
'NAME' => 'Original',
|
||||
'STATUS' => 'active',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
try {
|
||||
if ($this->tableName !== '') {
|
||||
$this->db()->statement(
|
||||
"DROP TABLE {$this->tableName}"
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
||||
|
||||
public function test_upsert_inserts_new_row(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->upsert(
|
||||
[[
|
||||
'ID' => 2,
|
||||
'CODE' => 'A002',
|
||||
'NAME' => 'Second',
|
||||
'STATUS' => 'active',
|
||||
]],
|
||||
['ID'],
|
||||
['NAME', 'STATUS']
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'Second',
|
||||
$this->db()->table($this->tableName)
|
||||
->where('ID', 2)->first()->NAME
|
||||
);
|
||||
}
|
||||
|
||||
public function test_upsert_updates_existing_row(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->upsert(
|
||||
[[
|
||||
'ID' => 1,
|
||||
'CODE' => 'A001',
|
||||
'NAME' => 'Updated',
|
||||
'STATUS' => 'inactive',
|
||||
]],
|
||||
['ID'],
|
||||
['NAME', 'STATUS']
|
||||
);
|
||||
|
||||
$row = $this->db()->table($this->tableName)
|
||||
->where('ID', 1)->first();
|
||||
|
||||
$this->assertSame('Updated', $row->NAME);
|
||||
$this->assertSame('inactive', $row->STATUS);
|
||||
}
|
||||
|
||||
public function test_upsert_multiple_rows(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->upsert(
|
||||
[
|
||||
[
|
||||
'ID' => 1,
|
||||
'CODE' => 'A001',
|
||||
'NAME' => 'Changed',
|
||||
'STATUS' => 'active',
|
||||
],
|
||||
[
|
||||
'ID' => 2,
|
||||
'CODE' => 'A002',
|
||||
'NAME' => 'New',
|
||||
'STATUS' => 'active',
|
||||
],
|
||||
],
|
||||
['ID'],
|
||||
['NAME']
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
2,
|
||||
$this->db()->table($this->tableName)->count()
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'Changed',
|
||||
$this->db()->table($this->tableName)
|
||||
->where('ID', 1)->first()->NAME
|
||||
);
|
||||
}
|
||||
|
||||
public function test_upsert_preserves_unselected_columns(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->upsert(
|
||||
[[
|
||||
'ID' => 1,
|
||||
'CODE' => 'A001',
|
||||
'NAME' => 'Changed',
|
||||
'STATUS' => 'inactive',
|
||||
]],
|
||||
['ID'],
|
||||
['NAME']
|
||||
);
|
||||
|
||||
$row = $this->db()->table($this->tableName)
|
||||
->where('ID', 1)->first();
|
||||
|
||||
$this->assertSame('Changed', $row->NAME);
|
||||
$this->assertSame('active', $row->STATUS);
|
||||
}
|
||||
|
||||
public function test_insert_or_ignore_inserts_new_row(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->insertOrIgnore([
|
||||
'ID' => 2,
|
||||
'CODE' => 'A002',
|
||||
'NAME' => 'Second',
|
||||
'STATUS' => 'active',
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
2,
|
||||
$this->db()->table($this->tableName)->count()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_insert_or_ignore_skips_duplicate(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->insertOrIgnore([
|
||||
'ID' => 1,
|
||||
'CODE' => 'A001',
|
||||
'NAME' => 'Duplicate',
|
||||
'STATUS' => 'inactive',
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
'Original',
|
||||
$this->db()->table($this->tableName)
|
||||
->where('ID', 1)->first()->NAME
|
||||
);
|
||||
}
|
||||
|
||||
public function test_insert_or_ignore_multiple_rows(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->insertOrIgnore([
|
||||
[
|
||||
'ID' => 1,
|
||||
'CODE' => 'A001',
|
||||
'NAME' => 'Duplicate',
|
||||
'STATUS' => 'inactive',
|
||||
],
|
||||
[
|
||||
'ID' => 2,
|
||||
'CODE' => 'A002',
|
||||
'NAME' => 'New',
|
||||
'STATUS' => 'active',
|
||||
],
|
||||
]);
|
||||
|
||||
$this->assertSame(
|
||||
2,
|
||||
$this->db()->table($this->tableName)->count()
|
||||
);
|
||||
}
|
||||
|
||||
public function test_update_or_insert(): void
|
||||
{
|
||||
$this->db()->table($this->tableName)->updateOrInsert(
|
||||
['ID' => 1],
|
||||
['NAME' => 'Updated']
|
||||
);
|
||||
|
||||
$this->db()->table($this->tableName)->updateOrInsert(
|
||||
['ID' => 2],
|
||||
[
|
||||
'CODE' => 'A002',
|
||||
'NAME' => 'Inserted',
|
||||
'STATUS' => 'active',
|
||||
]
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'Updated',
|
||||
$this->db()->table($this->tableName)
|
||||
->where('ID', 1)->first()->NAME
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
'Inserted',
|
||||
$this->db()->table($this->tableName)
|
||||
->where('ID', 2)->first()->NAME
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user