1 database_test.test DatabaseExtraTypesTestCase::testTimeField()

Test the time data type.

File

core/modules/simpletest/tests/database_test.test, line 3810
Database tests.

Class

DatabaseExtraTypesTestCase
Test proposed new data types for the schema API.

Code

function testTimeField() {
  try {
    $time_table = array(
      'fields' => array(
        'time_field' => array(
          'description' => t('Test Time field'),
          'type' => 'time',
          'not null' => FALSE,
        ),
      ),
    );
    db_create_table('time_table', $time_table);
    $this->assertTrue(db_table_exists('time_table'), t('Created table with time field'));

    db_insert('time_table')->fields(array('time_field'))
      ->values(array('time_field' => '12:59:00'))
      ->values(array('time_field' => '00:01:00'))
      ->values(array('time_field' => '23:17:00'))
      ->execute();

    $num_records = (int) db_query('SELECT COUNT(*) FROM {time_table}')->fetchField();
    $this->assertEqual($num_records, 3, t('Inserted 3 records, and counted 3 records'));
    $res = db_query('SELECT time_field from {time_table} ORDER BY time_field');

    $time = $res->fetch()->time_field;
    $this->assertEqual($time, '00:01:00', t('Time retrieved in order @time', array('@time' => $time)));
    $time = $res->fetch()->time_field;
    $this->assertEqual($time, '12:59:00', t('Time retrieved in order @time', array('@time' => $time)));
    $time = $res->fetch()->time_field;
    $this->assertEqual($time, '23:17:00', t('Time retrieved in order @time', array('@time' => $time)));

    db_drop_table('time_table');
    $this->assertFalse(db_table_exists('time_table'), t('Dropped table with time field'));
  }
  catch (Exception $e) {
    $this->fail($e->getMessage());
  }

}