diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-02-22 16:53:41 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-02-22 16:53:41 +0000 |
commit | b3e36d655c831c63c26a710eb3c8bd82ca3b6fc5 (patch) | |
tree | af24f8462bd347de36cd7375fc78dffb48e7d0b9 /modules/simpletest/tests/database_test.test | |
parent | e1652e99b61577ac42d6d6618420c872c0e42435 (diff) | |
download | brdo-b3e36d655c831c63c26a710eb3c8bd82ca3b6fc5.tar.gz brdo-b3e36d655c831c63c26a710eb3c8bd82ca3b6fc5.tar.bz2 |
- Patch #299267 by Crell: add extender support to the SELECT query builder.
Diffstat (limited to 'modules/simpletest/tests/database_test.test')
-rw-r--r-- | modules/simpletest/tests/database_test.test | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/modules/simpletest/tests/database_test.test b/modules/simpletest/tests/database_test.test index faaeb01ab..09d209bc4 100644 --- a/modules/simpletest/tests/database_test.test +++ b/modules/simpletest/tests/database_test.test @@ -1530,6 +1530,125 @@ class DatabaseSelectComplexTestCase extends DatabaseTestCase { } } +class DatabaseSelectPagerDefaultTestCase extends DatabaseTestCase { + + function getInfo() { + return array( + 'name' => t('Pager query tests'), + 'description' => t('Test the pager query extender.'), + 'group' => t('Database'), + ); + } + + /** + * Confirm that a pager query returns the correct results. + * + * Note that we have to make an HTTP request to a test page handler + * because the pager depends on GET parameters. + */ + function testEvenPagerQuery() { + // To keep the test from being too brittle, we determine up front + // what the page count should be dynamically, and pass the control + // information forward to the actual query on the other side of the + // HTTP request. + $limit = 2; + $count = db_query("SELECT COUNT(*) FROM {test}")->fetchField(); + + $correct_number = $limit; + $num_pages = floor($count / $limit); + + // If there is no remainder from rounding, subtract 1 since we index from 0. + if (!($num_pages * $limit < $count)) { + $num_pages--; + } + + for ($page = 0; $page <= $num_pages; ++$page) { + $this->drupalGet('database_test/pager_query_even/' . $limit, array('query' => array('page' => $page))); + $data = json_decode($this->drupalGetContent()); + + if ($page == $num_pages) { + $correct_number = $count - ($limit * $page); + } + + $this->assertEqual(count($data->names), $correct_number, t('Correct number of records returned by pager: @number', array('@number' => $correct_number))); + } + } + + /** + * Confirm that a pager query returns the correct results. + * + * Note that we have to make an HTTP request to a test page handler + * because the pager depends on GET parameters. + */ + function testOddPagerQuery() { + // To keep the test from being too brittle, we determine up front + // what the page count should be dynamically, and pass the control + // information forward to the actual query on the other side of the + // HTTP request. + $limit = 2; + $count = db_query("SELECT COUNT(*) FROM {test_task}")->fetchField(); + + $correct_number = $limit; + $num_pages = floor($count / $limit); + + // If there is no remainder from rounding, subtract 1 since we index from 0. + if (!($num_pages * $limit < $count)) { + $num_pages--; + } + + for ($page = 0; $page <= $num_pages; ++$page) { + $this->drupalGet('database_test/pager_query_odd/' . $limit, array('query' => array('page' => $page))); + $data = json_decode($this->drupalGetContent()); + + if ($page == $num_pages) { + $correct_number = $count - ($limit * $page); + } + + $this->assertEqual(count($data->names), $correct_number, t('Correct number of records returned by pager: @number', array('@number' => $correct_number))); + } + } +} + + +class DatabaseSelectTableSortDefaultTestCase extends DatabaseTestCase { + + function getInfo() { + return array( + 'name' => t('Tablesort query tests'), + 'description' => t('Test the tablesort query extender.'), + 'group' => t('Database'), + ); + } + + /** + * Confirm that a tablesort query returns the correct results. + * + * Note that we have to make an HTTP request to a test page handler + * because the pager depends on GET parameters. + */ + function testTableSortQuery() { + $sorts = array( + array('field' => t('Task ID'), 'sort' => 'desc', 'first' => 'perform at superbowl', 'last' => 'eat'), + array('field' => t('Task ID'), 'sort' => 'asc', 'first' => 'eat', 'last' => 'perform at superbowl'), + array('field' => t('Task'), 'sort' => 'asc', 'first' => 'code', 'last' => 'sleep'), + array('field' => t('Task'), 'sort' => 'desc', 'first' => 'sleep', 'last' => 'code'), + // more elements here + + ); + + foreach ($sorts as $sort) { + $this->drupalGet('database_test/tablesort/', array('query' => array('order' => $sort['field'], 'sort' => $sort['sort']))); + $data = json_decode($this->drupalGetContent()); + + $first = array_shift($data->tasks); + $last = array_pop($data->tasks); + + $this->assertEqual($first->task, $sort['first'], t('Items appear in the correct order.')); + $this->assertEqual($last->task, $sort['last'], t('Items appear in the correct order.')); + } + } +} + /** * Select tagging tests. * |