diff --git a/includes/database.mysql.inc b/includes/database.mysql.inc index 43342e9..a06bd17 100644 --- a/includes/database.mysql.inc +++ b/includes/database.mysql.inc @@ -365,7 +365,7 @@ function db_distinct_field($table, $field, $query) { $matches = array(); if (preg_match('/^SELECT(.*?)FROM(.*)/is', $query, $matches)) { $select = preg_replace( - '/((?:^|,)\s*)(? 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']))); + } + } + + + + + + } + + +}