You are here

function DatabaseMergeTestCase::testMergeInsert in SimpleTest 7

Confirm that we can merge-insert a record successfully.

File

tests/database_test.test, line 991

Class

DatabaseMergeTestCase
Test the MERGE query builder.

Code

function testMergeInsert() {
  $num_records_before = db_query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $result = db_merge('test_people')
    ->key(array(
    'job' => 'Presenter',
  ))
    ->fields(array(
    'age' => 31,
    'name' => 'Tiffany',
  ))
    ->execute();
  $this
    ->assertEqual($result, MergeQuery::STATUS_INSERT, t('Insert status returned.'));
  $num_records_after = db_query('SELECT COUNT(*) FROM {test_people}')
    ->fetchField();
  $this
    ->assertEqual($num_records_before + 1, $num_records_after, t('Merge inserted properly.'));
  $person = db_query('SELECT * FROM {test_people} WHERE job = :job', array(
    ':job' => 'Presenter',
  ))
    ->fetch();
  $this
    ->assertEqual($person->name, 'Tiffany', t('Name set correctly.'));
  $this
    ->assertEqual($person->age, 31, t('Age set correctly.'));
  $this
    ->assertEqual($person->job, 'Presenter', t('Job set correctly.'));
}