You are here

sqlsrv.schema.test in Drupal driver for SQL Server and SQL Azure 7

Same filename and directory in other branches
  1. 7.3 tests/sqlsrv.schema.test
  2. 7.2 tests/sqlsrv.schema.test

Support tests for SQL Server.

File

tests/sqlsrv.schema.test
View source
<?php

/**
 * @file
 * Support tests for SQL Server.
 */
class SqlServerSchemaTest extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Schema tests',
      'description' => 'Generic tests for SQL Server Schema.',
      'group' => 'Database (SQL Server)',
    );
  }

  /**
   * Test adding / removing / readding a primary key to a table.
   */
  public function testPrimaryKeyHandling() {
    $table_spec = array(
      'fields' => array(
        'id' => array(
          'type' => 'int',
          'not null' => TRUE,
        ),
      ),
    );
    db_create_table('test_table', $table_spec);
    $this
      ->assertTrue(db_table_exists('test_table'), t('Creating a table without a primary key works.'));
    db_add_primary_key('test_table', array(
      'id',
    ));
    $this
      ->pass(t('Adding a primary key should work when the table has no data.'));

    // Try adding a row.
    db_insert('test_table')
      ->fields(array(
      'id' => 1,
    ))
      ->execute();

    // The second row with the same value should conflict.
    try {
      db_insert('test_table')
        ->fields(array(
        'id' => 1,
      ))
        ->execute();
      $this
        ->fail(t('Duplicate values in the table should not be allowed when the primary key is there.'));
    } catch (Exception $e) {
    }

    // Drop the primary key and retry.
    db_drop_primary_key('test_table');
    $this
      ->pass(t('Removing a primary key should work.'));
    db_insert('test_table')
      ->fields(array(
      'id' => 1,
    ))
      ->execute();
    $this
      ->pass(t('Adding a duplicate row should work without the primary key.'));
    try {
      db_add_primary_key('test_table', array(
        'id',
      ));
      $this
        ->fail(t('Trying to add a primary key should fail with duplicate rows in the table.'));
    } catch (Exception $e) {
    }
  }

  /**
   * Test altering a primary key.
   */
  public function testPrimaryKeyAlter() {
    $table_spec = array(
      'fields' => array(
        'id' => array(
          'type' => 'int',
          'not null' => TRUE,
        ),
      ),
      'primary key' => array(
        'id',
      ),
    );
    db_create_table('test_table', $table_spec);

    // Add a default value.
    db_change_field('test_table', 'id', 'id', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 1,
    ));
  }

  /**
   * Test adding / modifying an unsigned column.
   */
  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 (Exception $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 (Exception $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');
  }

  /**
   * Test insert data in unsigned field.
   */
  protected function assertUnsignedField($table, $field_name) {
    try {
      db_insert('test_table')
        ->fields(array(
        'id' => -1,
      ))
        ->execute();
      $success = TRUE;
    } catch (Exception $e) {
      $success = FALSE;
    }
    $this
      ->assertFalse($success, t('Inserting a negative value in an unsigned field failed.'));
    try {
      db_insert('test_table')
        ->fields(array(
        'id' => 1,
      ))
        ->execute();
      $success = TRUE;
    } catch (Exception $e) {
      $success = FALSE;
    }
    $this
      ->assertTrue($success, t('Inserting a positive value in an unsigned field succeeded.'));
    db_delete('test_table')
      ->execute();
  }

  /**
   * Test inserting data in signed field.
   */
  protected function assertSignedField($table, $field_name) {
    try {
      db_insert('test_table')
        ->fields(array(
        'id' => -1,
      ))
        ->execute();
      $success = TRUE;
    } catch (Exception $e) {
      $success = FALSE;
    }
    $this
      ->assertTrue($success, t('Inserting a negative value in a signed field succeeded.'));
    try {
      db_insert('test_table')
        ->fields(array(
        'id' => 1,
      ))
        ->execute();
      $success = TRUE;
    } catch (Exception $e) {
      $success = FALSE;
    }
    $this
      ->assertTrue($success, t('Inserting a positive value in a signed field succeeded.'));
    db_delete('test_table')
      ->execute();
  }

  /**
   * Test db_add_field() and db_change_field() with indexes.
   */
  public function testAddChangeWithIndex() {
    $table_spec = array(
      'fields' => array(
        'id' => array(
          'type' => 'int',
          'not null' => TRUE,
        ),
      ),
      'primary key' => array(
        'id',
      ),
    );
    db_create_table('test_table', $table_spec);

    // Add a default value.
    db_add_field('test_table', 'test', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 1,
    ), array(
      'indexes' => array(
        'id_test' => array(
          'id, test',
        ),
      ),
    ));
    $this
      ->assertTrue(db_index_exists('test_table', 'id_test'), t('The index has been created by db_add_field().'));

    // Change the definition, we have by contract to remove the indexes before.
    db_drop_index('test_table', 'id_test');
    $this
      ->assertFalse(db_index_exists('test_table', 'id_test'), t('The index has been dropped.'));
    db_change_field('test_table', 'test', 'test', array(
      'type' => 'int',
      'not null' => TRUE,
      'default' => 1,
    ), array(
      'indexes' => array(
        'id_test' => array(
          'id, test',
        ),
      ),
    ));
    $this
      ->assertTrue(db_index_exists('test_table', 'id_test'), t('The index has been recreated by db_change_field().'));
  }

}

Classes

Namesort descending Description
SqlServerSchemaTest @file Support tests for SQL Server.