You are here

public function SqlServerSelectTest::testGroupByExpansion in Drupal driver for SQL Server and SQL Azure 7

Same name and namespace in other branches
  1. 7.3 tests/sqlsrv.select.test \SqlServerSelectTest::testGroupByExpansion()
  2. 7.2 tests/sqlsrv.select.test \SqlServerSelectTest::testGroupByExpansion()

Test group by expansion.

File

tests/sqlsrv.select.test, line 23
Support tests for SQL Server.

Class

SqlServerSelectTest
@file Support tests for SQL Server.

Code

public function testGroupByExpansion() {

  // By ANSI SQL, GROUP BY columns cannot use aliases. Test that the
  // driver expands the aliases properly.
  $query = db_select('test_task', 't');
  $count_field = $query
    ->addExpression('COUNT(task)', 'num');
  $task_field = $query
    ->addExpression('CONCAT(:prefix, t.task)', 'task', array(
    ':prefix' => 'Task: ',
  ));
  $query
    ->orderBy($count_field);
  $query
    ->groupBy($task_field);
  $result = $query
    ->execute();
  $num_records = 0;
  $last_count = 0;
  $records = array();
  foreach ($result as $record) {
    $num_records++;
    $this
      ->assertTrue($record->{$count_field} >= $last_count, t('Results returned in correct order.'));
    $last_count = $record->{$count_field};
    $records[$record->{$task_field}] = $record->{$count_field};
  }
  $correct_results = array(
    'Task: eat' => 1,
    'Task: sleep' => 2,
    'Task: code' => 1,
    'Task: found new band' => 1,
    'Task: perform at superbowl' => 1,
  );
  foreach ($correct_results as $task => $count) {
    $this
      ->assertEqual($records[$task], $count, t("Correct number of '@task' records found.", array(
      '@task' => $task,
    )));
  }
  $this
    ->assertEqual($num_records, 6, t('Returned the correct number of total rows.'));
}