1 database_test.test DatabaseMergeTestCase::testMergeUpdate()

Confirm that we can merge-update a record successfully.

File

core/modules/simpletest/tests/database_test.test, line 1089
Database tests.

Class

DatabaseMergeTestCase
Test the MERGE query builder.

Code

function testMergeUpdate() {
  $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  $num_records_matched = db_query('SELECT COUNT(*) FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetchField();

  $merge = db_merge('test_people')
    ->key(array('job' => 'Speaker'))
    ->fields(array(
      'age' => 31,
      'name' => 'Tiffany',
    ));
  $result = $merge->execute();

  $this->assertEqual($result, MergeQuery::STATUS_UPDATE, 'Update status returned.');
  $this->assertEqual($merge->getResult(), $num_records_matched, 'Number of updated rows matches.');
  $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')->fetchField();
  $this->assertEqual($num_records_before, $num_records_after, 'Merge updated properly.');

  $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(':job' => 'Speaker'))->fetch();
  $this->assertEqual($person->name, 'Tiffany', 'Name set correctly.');
  $this->assertEqual($person->age, 31, 'Age set correctly.');
  $this->assertEqual($person->job, 'Speaker', 'Job set correctly.');
}