t('Database functionality'), 'description' => t('Exercise the database helper functions, this not not test the database but the functions that abstract the database.'), 'group' => t('Database'), ); } /** * Test the db_distinct_field() function. * * See http://drupal.org/node/284392 for more information */ function testDbDistinctField() { // These assertions are taken from: // http://drupal.org/node/284392 $assertions[] = array( 'input' => array("table", "field", "SELECT table.field FROM table"), 'expected' => 'SELECT DISTINCT(table.field) FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT table.field AS table_field FROM table"), 'expected' => 'SELECT DISTINCT(table.field) AS table_field FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT DISTINCT(table.field) FROM table"), 'expected' => 'SELECT DISTINCT(table.field) FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT DISTINCT(table.field) AS table_field FROM table"), 'expected' => 'SELECT DISTINCT(table.field) AS table_field FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT table.field,table.field2 FROM table"), 'expected' => 'SELECT DISTINCT(table.field),table.field2 FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT table.field AS table_field, table.field2 AS table_field2 FROM table"), 'expected' => 'SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT DISTINCT(table.field),table.field2 FROM table"), 'expected' => 'SELECT DISTINCT(table.field),table.field2 FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table"), 'expected' => 'SELECT DISTINCT(table.field) AS table_field, table.field2 AS table_field2 FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT COUNT(table.field) FROM table"), 'expected' => 'SELECT COUNT(DISTINCT(table.field)) FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT COUNT(table.field) AS table_field FROM table"), 'expected' => 'SELECT COUNT(DISTINCT(table.field)) AS table_field FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT table.field1, COUNT(table.field), table.field2 FROM table"), 'expected' => 'SELECT table.field1, COUNT(DISTINCT(table.field)), table.field2 FROM table', ); $assertions[] = array( 'input' => array("table", "field", "SELECT COUNT(DISTINCT(table.field)) FROM table"), 'expected' => 'SELECT COUNT(DISTINCT(table.field)) FROM table', ); foreach ($assertions as $assert) { $distinct_added = call_user_func_array('db_distinct_field', $assert['input']); if (!$this->assertEqual( $distinct_added, $assert['expected'], t('Add DISTINCT to %query', array('%query' => $assert['input'][2])) )) { $this->pass(t('Query was rewritten to: %query, expected query: %expected_query', array('%query' => $distinct_added, '%expected_query' => $assert['expected']))); } } } }