1 database_test.test DatabaseMergeTestCase::testMergeInsert()

Confirm that we can merge-insert a record successfully.

File

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

Class

DatabaseMergeTestCase
Test the MERGE query builder.

Code

function testMergeInsert() {
  $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  $max_id_before = db_query('SELECT MAX(id) FROM {test}')->fetchField();

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

  $this->assertEqual($result, MergeQuery::STATUS_INSERT, 'Insert status returned.');
  $this->assertEqual($merge->getResult(), $max_id_before + 1, 'Incremented ID returned.');

  $num_records_after = db_query('SELECT COUNT(*) FROM {test}')->fetchField();
  $this->assertEqual($num_records_before + 1, $num_records_after, 'Merge inserted properly.');

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