You are here

public function SelectQueryTest::testGroupByExpansion in Drupal driver for SQL Server and SQL Azure 8

Checks that invalid sort directions in ORDER BY get converted to ASC.

File

src/Tests/SelectQueryTest.php, line 68
Definition of Drupal\sqlsrv\Tests\SelectQueryTest.

Class

SelectQueryTest
General tests for SQL Server database driver.

Namespace

Drupal\sqlsrv\Tests

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, '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, "Correct number of '@task' records found.");
  }
  $this
    ->assertEqual($num_records, 5, 'Returned the correct number of total rows.');
}