You are here

public function SchemaTestExtended::testUnsignedField in Drupal driver for SQL Server and SQL Azure 8.2

Test adding / modifying an unsigned column.

File

tests/src/Kernel/SchemaTestExtended.php, line 156

Class

SchemaTestExtended
Tests table creation and modification via the schema API.

Namespace

Drupal\Tests\sqlsrv\Kernel

Code

public function testUnsignedField() {
  $table_spec = array(
    'fields' => array(
      'id' => array(
        'type' => 'int',
        'not null' => TRUE,
        'unsigned' => TRUE,
      ),
    ),
  );
  $schema = $this->connection
    ->schema();
  $schema
    ->createTable('test_table', $table_spec);
  try {
    $this->connection
      ->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 {
    $this->connection
      ->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.
  $schema
    ->changeField('test_table', 'id', 'id', array(
    'type' => 'int',
    'not null' => TRUE,
  ));
  $this
    ->assertSignedField('test_table', 'id');

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