Skip to content
Snippets Groups Projects
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
No related branches found
No related tags found
9 merge requests!10011Issue #3200534 by quietone, longwave, Kristen Pol: Use dataprovider for...,!2571Issue #3000717: Missing mapping for "nodereference_url" widget,!2521Issue #3185775: Place Views preview on the side on large monitors,!1603Issue #3231707: mxr576's core patch playground,!1479Issue #3250298: Return empty string "" with JSON Serializer instead of FALSE,!1478Issue #3250298: Return empty string "" with JSON Serializer instead of FALSE,!1203Issue #3236191 Wrong group exposed form widgets and multiple selection error.,!1076Issue #2903336 Added node context for tokens.,!1015Issue #3226944: REST's Request handler doesn't resolve $data argument for put method
......@@ -320,7 +320,9 @@ function database_test_schema() {
'fields' => [
'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,
],
'update' => [
......
......@@ -67,7 +67,8 @@ public function testUpsertWithKeywords() {
// Add a new row.
$upsert->values([
'id' => 2,
// Test a non sequence ID for better testing of the default return value.
'id' => 3,
'update' => 'Update value 2',
]);
......@@ -79,6 +80,10 @@ public function testUpsertWithKeywords() {
$result = $upsert->execute();
$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.');
$num_records_after = $this->connection->query('SELECT COUNT(*) FROM {select}')->fetchField();
......@@ -87,8 +92,18 @@ public function testUpsertWithKeywords() {
$record = $this->connection->query('SELECT * FROM {select} WHERE [id] = :id', [':id' => 1])->fetch();
$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);
// 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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment