Skip to content

Commit

Permalink
Adding more tests for virtualFields and complex expressions. Closes #…
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Sep 7, 2010
1 parent e150697 commit aad7287
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
44 changes: 34 additions & 10 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -1279,7 +1279,7 @@ class DboSourceTest extends CakeTestCase {
*/
var $fixtures = array(
'core.apple', 'core.article', 'core.articles_tag', 'core.attachment', 'core.comment',
'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author'
'core.sample', 'core.tag', 'core.user', 'core.post', 'core.author', 'core.data_test'
);

/**
Expand Down Expand Up @@ -4329,8 +4329,6 @@ function testVirtualFields() {
* @return void
*/
function testVirtualFieldsInConditions() {
$this->loadFixtures('Article');

$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_moment' => 'NOW()',
Expand Down Expand Up @@ -4374,7 +4372,6 @@ function testConditionsWithComplexVirtualFields() {
* COS((50 - Article.longitude) * PI() / 180)
) * 180 / PI() * 60 * 1.1515 * 1.609344'
);

$conditions = array('distance >=' => 20);
$result = $this->db->conditions($conditions, true, true, $Article);

Expand All @@ -4389,8 +4386,6 @@ function testConditionsWithComplexVirtualFields() {
* @return void
*/
function testVirtualFieldsInOrder() {
$this->loadFixtures('Article');

$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_moment' => 'NOW()',
Expand All @@ -4413,8 +4408,6 @@ function testVirtualFieldsInOrder() {
* @return void
*/
function testVirtualFieldsInCalculate() {
$this->loadFixtures('Article');

$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_moment' => 'NOW()',
Expand Down Expand Up @@ -4455,6 +4448,39 @@ function testVirtualFieldsFetch() {
));
$this->assertEqual($expected, $result);
}
˝
/**
* test reading complex virtualFields with subqueries.
*
* @return void
*/
function testVirtualFieldsComplexRead() {
$this->loadFixtures('DataTest', 'Article', 'Comment');

$Article =& ClassRegistry::init('Article');
$commentTable = $this->db->fullTableName('comments');
$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'comment_count' => 'SELECT COUNT(*) FROM ' . $commentTable .
' AS Comment WHERE Article.id = Comment.article_id'
);
$result = $Article->find('all');
$this->assertTrue(count($result) > 0);
$this->assertTrue($result[0]['Article']['comment_count'] > 0);

$DataTest =& ClassRegistry::init('DataTest');
$DataTest->virtualFields = array(
'complicated' => 'ACOS(SIN(20 * PI() / 180)
* SIN(DataTest.float * PI() / 180)
+ COS(20 * PI() / 180)
* COS(DataTest.count * PI() / 180)
* COS((50 - DataTest.float) * PI() / 180)
) * 180 / PI() * 60 * 1.1515 * 1.609344'
);
$result = $DataTest->find('all');
$this->assertTrue(count($result) > 0);
$this->assertTrue($result[0]['DataTest']['complicated'] > 0);
}

/**
* test that virtualFields with complex functions and aliases work.
Expand Down Expand Up @@ -4489,8 +4515,6 @@ function testFieldsWithComplexVirtualFields() {
* @return void
*/
function testVirtualFieldsInGroup() {
$this->loadFixtures('Article');

$Article =& ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_year' => 'YEAR(Article.created)'
Expand Down
9 changes: 8 additions & 1 deletion cake/tests/fixtures/data_test_fixture.php
Expand Up @@ -55,5 +55,12 @@ class DataTestFixture extends CakeTestFixture {
* @var array
* @access public
*/
var $records = array();
var $records = array(
array(
'count' => 2,
'float' => 2.4,
'created' => '2010-09-06 12:28:00',
'updated' => '2010-09-06 12:28:00'
)
);
}

0 comments on commit aad7287

Please sign in to comment.