SchemaTestCase::assertFieldCharacteristics

7 schema.test protected SchemaTestCase::assertFieldCharacteristics($table_name, $field_name, $field_spec)
8 schema.test protected SchemaTestCase::assertFieldCharacteristics($table_name, $field_name, $field_spec)

Assert that a newly added field has the correct characteristics.

File

modules/simpletest/tests/schema.test, line 355
Tests for the Database Schema API.

Code

protected function assertFieldCharacteristics($table_name, $field_name, $field_spec) {
  // Check that the initial value has been registered.
  if (isset($field_spec['initial'])) {
    // There should be no row with a value different then $field_spec['initial'].
    $count = db_select($table_name)
        ->fields($table_name, array('serial_column'))
        ->condition($field_name, $field_spec['initial'], '<>')
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertEqual($count, 0, t('Initial values filled out.'));
  }

  // Check that the default value has been registered.
  if (isset($field_spec['default'])) {
    // Try inserting a row, and check the resulting value of the new column.
    $id = db_insert($table_name)
        ->useDefaults(array('serial_column'))
        ->execute();
    $field_value = db_select($table_name)
        ->fields($table_name, array($field_name))
        ->condition('serial_column', $id)
        ->execute()
        ->fetchField();
    $this->assertEqual($field_value, $field_spec['default'], t('Default value registered.'));
  }

  db_drop_field($table_name, $field_name);
}
Login or register to post comments