Navigation Menu

Skip to content

Commit

Permalink
Extracting remaning methods with Mysql specific syntax out of DboSour…
Browse files Browse the repository at this point in the history
…ceTest
  • Loading branch information
lorenzo committed Nov 9, 2010
1 parent 539b907 commit b20becc
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 86 deletions.
85 changes: 85 additions & 0 deletions cake/tests/cases/libs/model/datasources/dbo/dbo_mysql.test.php
Expand Up @@ -3019,4 +3019,89 @@ function testConditionsWithComplexVirtualFields() {
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result);
}

/**
* test calculate to generate claculate statements on virtual fields
*
* @return void
*/
function testVirtualFieldsInCalculate() {
$Article = ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_moment' => 'NOW()',
'two' => '1 + 1',
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->Dbo->fullTableName('comments') .
' WHERE Article.id = ' . $this->Dbo->fullTableName('comments'). '.article_id'
);

$result = $this->Dbo->calculate($Article, 'count', array('this_moment'));
$expected = 'COUNT(NOW()) AS `count`';
$this->assertEqual($expected, $result);

$result = $this->Dbo->calculate($Article, 'max', array('comment_count'));
$expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`';
$this->assertEqual($expected, $result);
}

/**
* test reading virtual fields containing newlines when recursive > 0
*
* @return void
*/
function testReadVirtualFieldsWithNewLines() {
$Article = new Article();
$Article->recursive = 1;
$Article->virtualFields = array(
'test' => '
User.id + User.id
'
);
$result = $this->Dbo->fields($Article, null, array());
$result = $this->Dbo->fields($Article, $Article->alias, $result);
$this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]);
}

/**
* test group to generate GROUP BY statements on virtual fields
*
* @return void
*/
function testVirtualFieldsInGroup() {
$Article = ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_year' => 'YEAR(Article.created)'
);

$result = $this->Dbo->group('this_year', $Article);

$expected = " GROUP BY (YEAR(`Article`.`created`))";
$this->assertEqual($expected, $result);
}

/**
* test that virtualFields with complex functions and aliases work.
*
* @return void
*/
function testFieldsWithComplexVirtualFields() {
$Article = new Article();
$Article->virtualFields = array(
'distance' => 'ACOS(SIN(20 * PI() / 180)
* SIN(Article.latitude * PI() / 180)
+ COS(20 * PI() / 180)
* COS(Article.latitude * PI() / 180)
* COS((50 - Article.longitude) * PI() / 180)
) * 180 / PI() * 60 * 1.1515 * 1.609344'
);

$fields = array('id', 'distance');
$result = $this->Dbo->fields($Article, null, $fields);
$qs = $this->Dbo->startQuote;
$qe = $this->Dbo->endQuote;

$this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}");
$this->assertPattern('/Article__distance/', $result[1]);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]);
}
}
87 changes: 1 addition & 86 deletions cake/tests/cases/libs/model/datasources/dbo_source.test.php
Expand Up @@ -999,29 +999,6 @@ function testVirtualFieldsInOrder() {
$this->assertEqual($expected, $result);
}

/**
* test calculate to generate claculate statements on virtual fields
*
* @return void
*/
function testVirtualFieldsInCalculate() {
$Article = ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_moment' => 'NOW()',
'two' => '1 + 1',
'comment_count' => 'SELECT COUNT(*) FROM ' . $this->db->fullTableName('comments') .
' WHERE Article.id = ' . $this->db->fullTableName('comments'). '.article_id'
);

$result = $this->db->calculate($Article, 'count', array('this_moment'));
$expected = 'COUNT(NOW()) AS `count`';
$this->assertEqual($expected, $result);

$result = $this->db->calculate($Article, 'max', array('comment_count'));
$expected = 'MAX(SELECT COUNT(*) FROM comments WHERE `Article`.`id` = `comments`.`article_id`) AS `comment_count`';
$this->assertEqual($expected, $result);
}

/**
* test a full example of using virtual fields
*
Expand Down Expand Up @@ -1079,68 +1056,6 @@ function testVirtualFieldsComplexRead() {
$this->assertTrue($result[0]['DataTest']['complicated'] > 0);
}

/**
* test that virtualFields with complex functions and aliases work.
*
* @return void
*/
function testFieldsWithComplexVirtualFields() {
$Article = new Article();
$Article->virtualFields = array(
'distance' => 'ACOS(SIN(20 * PI() / 180)
* SIN(Article.latitude * PI() / 180)
+ COS(20 * PI() / 180)
* COS(Article.latitude * PI() / 180)
* COS((50 - Article.longitude) * PI() / 180)
) * 180 / PI() * 60 * 1.1515 * 1.609344'
);

$fields = array('id', 'distance');
$result = $this->db->fields($Article, null, $fields);
$qs = $this->db->startQuote;
$qe = $this->db->endQuote;

$this->assertEqual($result[0], "{$qs}Article{$qe}.{$qs}id{$qe}");
$this->assertPattern('/Article__distance/', $result[1]);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]latitude[`\'"]/', $result[1]);
$this->assertPattern('/[`\'"]Article[`\'"].[`\'"]longitude[`\'"]/', $result[1]);
}

/**
* test reading virtual fields containing newlines when recursive > 0
*
* @return void
*/
function testReadVirtualFieldsWithNewLines() {
$Article = new Article();
$Article->recursive = 1;
$Article->virtualFields = array(
'test' => '
User.id + User.id
'
);
$result = $this->db->fields($Article, null, array());
$result = $this->db->fields($Article, $Article->alias, $result);
$this->assertPattern('/[`\"]User[`\"]\.[`\"]id[`\"] \+ [`\"]User[`\"]\.[`\"]id[`\"]/', $result[7]);
}

/**
* test group to generate GROUP BY statements on virtual fields
*
* @return void
*/
function testVirtualFieldsInGroup() {
$Article = ClassRegistry::init('Article');
$Article->virtualFields = array(
'this_year' => 'YEAR(Article.created)'
);

$result = $this->db->group('this_year', $Article);

$expected = " GROUP BY (YEAR(`Article`.`created`))";
$this->assertEqual($expected, $result);
}

/**
* test the permutations of fullTableName()
*
Expand Down Expand Up @@ -1185,7 +1100,7 @@ function testFieldsUsingMethodCache() {
$this->testDb->cacheMethods = false;
$this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty');

$Article =& ClassRegistry::init('Article');
$Article = ClassRegistry::init('Article');
$this->testDb->fields($Article, null, array('title', 'body', 'published'));
$this->assertTrue(empty($this->testDb->methodCache['fields']), 'Cache not empty');
}
Expand Down

0 comments on commit b20becc

Please sign in to comment.