Commit cdf89cb5 authored by catch's avatar catch
Browse files

Issue #3185400 by mondrake, alexpott, daffie: Test upsert return value and...

Issue #3185400 by mondrake, alexpott, daffie: Test upsert return value and ensure that they are consistent regardless of database type

(cherry picked from commit d8ab4a9e)
parent e232dfae
...@@ -320,7 +320,9 @@ function database_test_schema() { ...@@ -320,7 +320,9 @@ function database_test_schema() {
'fields' => [ 'fields' => [
'id' => [ 'id' => [
'description' => 'Simple unique ID.', 'description' => 'Simple unique ID.',
'type' => 'int', // Using a serial as an ID properly tests
// \Drupal\Core\Database\Driver\pgsql\Upsert.
'type' => 'serial',
'not null' => TRUE, 'not null' => TRUE,
], ],
'update' => [ 'update' => [
......
...@@ -67,7 +67,8 @@ public function testUpsertWithKeywords() { ...@@ -67,7 +67,8 @@ public function testUpsertWithKeywords() {
// Add a new row. // Add a new row.
$upsert->values([ $upsert->values([
'id' => 2, // Test a non sequence ID for better testing of the default return value.
'id' => 3,
'update' => 'Update value 2', 'update' => 'Update value 2',
]); ]);
...@@ -79,6 +80,10 @@ public function testUpsertWithKeywords() { ...@@ -79,6 +80,10 @@ public function testUpsertWithKeywords() {
$result = $upsert->execute(); $result = $upsert->execute();
$this->assertIsInt($result); $this->assertIsInt($result);
// The upsert returns the number of rows affected. For MySQL the return
// value is 3 because the affected-rows value per row is 1 if the row is
// inserted as a new row, 2 if an existing row is updated. See
// https://dev.mysql.com/doc/c-api/8.0/en/mysql-affected-rows.html.
$this->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.'); $this->assertGreaterThanOrEqual(2, $result, 'The result of the upsert operation should report that at least two rows were affected.');
$num_records_after = $this->connection->query('SELECT COUNT(*) FROM {select}')->fetchField(); $num_records_after = $this->connection->query('SELECT COUNT(*) FROM {select}')->fetchField();
...@@ -87,8 +92,18 @@ public function testUpsertWithKeywords() { ...@@ -87,8 +92,18 @@ public function testUpsertWithKeywords() {
$record = $this->connection->query('SELECT * FROM {select} WHERE [id] = :id', [':id' => 1])->fetch(); $record = $this->connection->query('SELECT * FROM {select} WHERE [id] = :id', [':id' => 1])->fetch();
$this->assertEquals('Update value 1 updated', $record->update); $this->assertEquals('Update value 1 updated', $record->update);
$record = $this->connection->query('SELECT * FROM {select} WHERE [id] = :id', [':id' => 2])->fetch(); $record = $this->connection->query('SELECT * FROM {select} WHERE [id] = :id', [':id' => 3])->fetch();
$this->assertEquals('Update value 2', $record->update); $this->assertEquals('Update value 2', $record->update);
// An upsert should be re-usable.
$upsert->values([
'id' => 4,
'update' => 'Another value',
]);
$return_value = $upsert->execute();
$this->assertSame(1, $return_value);
$record = $this->connection->query('SELECT * FROM {select} WHERE [id] = :id', [':id' => 4])->fetch();
$this->assertEquals('Another value', $record->update);
} }
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment