1 schema.test SchemaTestCase::testUnsignedColumns()

Tests creating unsigned columns and data integrity thereof.

File

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

Class

SchemaTestCase
Unit tests for the Schema API.

Code

function testUnsignedColumns() {
  // First create the table with just a serial column.
  $table_name = 'unsigned_table';
  $table_spec = array(
    'fields' => array('serial_column' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE)),
    'primary key' => array('serial_column'),
  );
  $ret = array();
  db_create_table($table_name, $table_spec);

  // Now set up columns for the other types.
  $types = array('int', 'float', 'numeric');
  foreach ($types as $type) {
    $column_spec = array('type' => $type, 'unsigned' => TRUE);
    if ($type == 'numeric') {
      $column_spec += array('precision' => 10, 'scale' => 0);
    }
    $column_name = $type . '_column';
    $table_spec['fields'][$column_name] = $column_spec;
    db_add_field($table_name, $column_name, $column_spec);
  }

  // Finally, check each column and try to insert invalid values into them.
  foreach ($table_spec['fields'] as $column_name => $column_spec) {
    $this->assertTrue(db_field_exists($table_name, $column_name), format_string('Unsigned @type column was created.', array('@type' => $column_spec['type'])));
    $this->assertFalse($this->tryUnsignedInsert($table_name, $column_name), format_string('Unsigned @type column rejected a negative value.', array('@type' => $column_spec['type'])));
  }
}