You are here

function DatabaseExtraTypesTestCase::testDateField in SimpleTest 7

Test the date data type.

File

tests/database_test.test, line 2913

Class

DatabaseExtraTypesTestCase
Test proposed new data types for the schema API.

Code

function testDateField() {
  try {
    $date_table = array(
      'fields' => array(
        'date_field' => array(
          'description' => t('Test Date field'),
          'type' => 'date',
          'not null' => FALSE,
        ),
      ),
    );
    db_create_table('date_table', $date_table);
    $this
      ->assertTrue(db_table_exists('date_table'), t('Created table with date field'));
    db_insert('date_table')
      ->fields(array(
      'date_field',
    ))
      ->values(array(
      'date_field' => '2001-01-01',
    ))
      ->values(array(
      'date_field' => '1856-12-31',
    ))
      ->values(array(
      'date_field' => '2100-06-30',
    ))
      ->execute();
    $num_records = (int) db_query('SELECT COUNT(*) FROM {date_table}')
      ->fetchField();
    $this
      ->assertEqual($num_records, 3, t('Inserted 3 records, and counted 3 records'));
    $res = db_query('SELECT date_field from {date_table} ORDER BY date_field');
    $date = $res
      ->fetch()->date_field;
    $this
      ->assertEqual($date, '1856-12-31', t('Date retrieved in order @date', array(
      '@date' => $date,
    )));
    $date = $res
      ->fetch()->date_field;
    $this
      ->assertEqual($date, '2001-01-01', t('Date retrieved in order @date', array(
      '@date' => $date,
    )));
    $date = $res
      ->fetch()->date_field;
    $this
      ->assertEqual($date, '2100-06-30', t('Date retrieved in order @date', array(
      '@date' => $date,
    )));
    db_drop_table('date_table');
    $this
      ->assertFalse(db_table_exists('date_table'), t('Dropped table with date field'));
  } catch (Exception $e) {
    $this
      ->fail($e
      ->getMessage());
  }
}