You are here

function DatabaseInsertTestCase::testMultiInsert in Drupal 7

Test that we can insert multiple records in one query object.

File

modules/simpletest/tests/database_test.test, line 513

Class

DatabaseInsertTestCase
Test the insert builder.

Code

function testMultiInsert() {
  $num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')
    ->fetchField();
  $query = db_insert('test');
  $query
    ->fields(array(
    'name' => 'Larry',
    'age' => '30',
  ));

  // We should be able to specify values in any order if named.
  $query
    ->values(array(
    'age' => '31',
    'name' => 'Curly',
  ));

  // We should be able to say "use the field order".
  // This is not the recommended mechanism for most cases, but it should work.
  $query
    ->values(array(
    'Moe',
    '32',
  ));
  $query
    ->execute();
  $num_records_after = (int) db_query('SELECT COUNT(*) FROM {test}')
    ->fetchField();
  $this
    ->assertIdentical($num_records_before + 3, $num_records_after, 'Record inserts correctly.');
  $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Larry',
  ))
    ->fetchField();
  $this
    ->assertIdentical($saved_age, '30', 'Can retrieve after inserting.');
  $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Curly',
  ))
    ->fetchField();
  $this
    ->assertIdentical($saved_age, '31', 'Can retrieve after inserting.');
  $saved_age = db_query('SELECT age FROM {test} WHERE name = :name', array(
    ':name' => 'Moe',
  ))
    ->fetchField();
  $this
    ->assertIdentical($saved_age, '32', 'Can retrieve after inserting.');
}