You are here

public function SqlServerSchemaTest::testUnsignedField in Drupal driver for SQL Server and SQL Azure 8

Test adding / modifying an unsigned column.

File

src/Tests/SqlServerSchemaTest.php, line 98
Definition of Drupal\sqlsrv\Tests\SqlServerSchemaTest.

Class

SqlServerSchemaTest
Schema tests for SQL Server database driver.

Namespace

Drupal\sqlsrv\Tests

Code

public function testUnsignedField() {
  $table_spec = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
    ),
  );
  db_create_table('test_table', $table_spec);
  try {
    db_insert('test_table')
      ->fields(array(
      'id' => -1,
    ))
      ->execute();
    $failed = FALSE;
  } catch (DatabaseException $e) {
    $failed = TRUE;
  }
  $this
    ->assertTrue($failed, t('Inserting a negative value in an unsigned field failed.'));
  $this
    ->assertUnsignedField('test_table', 'id');
  try {
    db_insert('test_table')
      ->fields(array(
      'id' => 1,
    ))
      ->execute();
    $failed = FALSE;
  } catch (DatabaseException $e) {
    $failed = TRUE;
  }
  $this
    ->assertFalse($failed, t('Inserting a positive value in an unsigned field succeeded.'));

  // Change the field to signed.
  db_change_field('test_table', 'id', 'id', array(
    'type' => 'int',
    'not null' => TRUE,
  ));
  $this
    ->assertSignedField('test_table', 'id');

  // Change the field back to unsigned.
  db_change_field('test_table', 'id', 'id', array(
    'type' => 'int',
    'not null' => TRUE,
    'unsigned' => TRUE,
  ));
  $this
    ->assertUnsignedField('test_table', 'id');
}