Skip to content

Commit

Permalink
Some tests refactored. Skipped non-compartible tests in MySQL
Browse files Browse the repository at this point in the history
ONLY_FULL_GROUP_BY mode.
  • Loading branch information
bancer committed Jul 11, 2017
1 parent 0d68007 commit 5cc0d7a
Showing 1 changed file with 83 additions and 53 deletions.
136 changes: 83 additions & 53 deletions lib/Cake/Test/Case/Model/ModelReadTest.php
Expand Up @@ -192,63 +192,95 @@ public function testInOperator() {
$this->assertEquals($expected, $result);
}

public function skipIfIsStrictGroupBy() {
$isOnlyFullGroupBy = false;
if($this->db instanceof Mysql) {
$sqlMode = $this->db->query('SELECT @@sql_mode AS sql_mode;');
if (strpos($sqlMode[0][0]['sql_mode'], 'ONLY_FULL_GROUP_BY') > -1) {
$isOnlyFullGroupBy = true;
}
}
$isStrictGroupBy = $isOnlyFullGroupBy ||
$this->db instanceof Postgres ||
$this->db instanceof Sqlite ||
$this->db instanceof Oracle ||
$this->db instanceof Sqlserver;
$message = 'Postgres, Oracle, SQLite, SQL Server and MySQL in ONLY_FULL_GROUP_BY ' .
'mode have strict GROUP BY and are incompatible with this test.';
$this->skipIf($isStrictGroupBy, $message);
}

/**
* testGroupBy method
* testGroupByAndOrder method
*
* These tests will never pass with Postgres or Oracle as all fields in a select must be
* This test will never pass with Postgres or Oracle as all fields in a select must be
* part of an aggregate function or in the GROUP BY statement.
*
* @return void
*/
public function testGroupBy() {
$isStrictGroupBy = $this->db instanceof Postgres || $this->db instanceof Sqlite || $this->db instanceof Oracle || $this->db instanceof Sqlserver;
$message = 'Postgres, Oracle, SQLite and SQL Server have strict GROUP BY and are incompatible with this test.';

$this->skipIf($isStrictGroupBy, $message);

$this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
public function testGroupByAndOrder() {
$this->skipIfIsStrictGroupBy();
$this->loadFixtures('Project', 'Thread', 'Message');
$Thread = new Thread();
$Product = new Product();

$result = $Thread->find('all', array(
'group' => 'Thread.project_id',
'order' => 'Thread.id ASC'
'order' => 'Thread.id ASC',
));

$expected = array(
array(
'Thread' => array(
$expected = array (
array (
'Thread' => array (
'id' => 1,
'project_id' => 1,
'name' => 'Project 1, Thread 1'
'name' => 'Project 1, Thread 1',
),
'Project' => array(
'Project' => array (
'id' => 1,
'name' => 'Project 1'
'name' => 'Project 1',
),
'Message' => array(
array(
'Message' => array (
array (
'id' => 1,
'thread_id' => 1,
'name' => 'Thread 1, Message 1'
))),
array(
'Thread' => array(
'name' => 'Thread 1, Message 1',
),
),
),
array (
'Thread' => array (
'id' => 3,
'project_id' => 2,
'name' => 'Project 2, Thread 1'
'name' => 'Project 2, Thread 1',
),
'Project' => array(
'Project' => array (
'id' => 2,
'name' => 'Project 2'
'name' => 'Project 2',
),
'Message' => array(
array(
'Message' => array (
array (
'id' => 3,
'thread_id' => 3,
'name' => 'Thread 3, Message 1'
))));
'name' => 'Thread 3, Message 1',
),
),
),
);
$this->assertEquals($expected, $result);
}

/**
* testGroupBy method
*
* These tests will never pass with Postgres or Oracle as all fields in a select must be
* part of an aggregate function or in the GROUP BY statement.
*
* @return void
*/
public function testGroupBy() {
$this->skipIfIsStrictGroupBy();

$this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
$Thread = new Thread();
$Product = new Product();

$rows = $Thread->find('all', array(
'group' => 'Thread.project_id',
Expand Down Expand Up @@ -8159,52 +8191,50 @@ public function testVirtualFieldsOrder() {
$this->assertEquals($expected, $result);
}

/**
* testVirtualFieldsMysql()
*
* Test correct fetching of virtual fields
* currently is not possible to do Relation.virtualField
*
* @return void
*/
public function testVirtualFieldsMysql() {
public function testVirtualFieldsMysqlGroup() {
$this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFields test only compatible with Mysql.');

$this->loadFixtures('Post', 'Author');
$this->skipIfIsStrictGroupBy();
$this->loadFixtures('Post');
$Post = ClassRegistry::init('Post');

$Post->create();
$Post->virtualFields = array(
'low_title' => 'lower(Post.title)',
'unique_test_field' => 'COUNT(Post.id)'
'unique_test_field' => 'COUNT(Post.id)',
);

$expectation = array(
'Post' => array(
'low_title' => 'first post',
'unique_test_field' => 1
)
'unique_test_field' => 1,
),
);

$result = $Post->find('first', array(
'fields' => array_keys($Post->virtualFields),
'group' => array('low_title')
'group' => array('low_title'),
));

$this->assertEquals($expectation, $result);
}

/**
* testVirtualFieldsMysql()
*
* Test correct fetching of virtual fields
* currently is not possible to do Relation.virtualField
*
* @return void
*/
public function testVirtualFieldsMysql() {
$this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFields test only compatible with Mysql.');
$this->loadFixtures('Author');
$Author = ClassRegistry::init('Author');
$Author->virtualFields = array(
'full_name' => 'CONCAT(Author.user, " ", Author.id)'
);

$result = $Author->find('first', array(
'conditions' => array('Author.user' => 'mariano'),
'fields' => array('Author.password', 'Author.full_name'),
'recursive' => -1
));
$this->assertTrue(isset($result['Author']['full_name']));

$result = $Author->find('first', array(
'conditions' => array('Author.user' => 'mariano'),
'fields' => array('Author.full_name', 'Author.password'),
Expand Down

0 comments on commit 5cc0d7a

Please sign in to comment.