Some tests refactored. Skipped non-compartible tests in MySQL

ONLY_FULL_GROUP_BY mode.
This commit is contained in:
Val Bancer 2017-07-12 00:46:02 +02:00
parent 0d68007e5c
commit 5cc0d7a5cf

View file

@ -192,6 +192,81 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $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);
}
/**
* testGroupByAndOrder method
*
* 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 testGroupByAndOrder() {
$this->skipIfIsStrictGroupBy();
$this->loadFixtures('Project', 'Thread', 'Message');
$Thread = new Thread();
$result = $Thread->find('all', array(
'group' => 'Thread.project_id',
'order' => 'Thread.id ASC',
));
$expected = array (
array (
'Thread' => array (
'id' => 1,
'project_id' => 1,
'name' => 'Project 1, Thread 1',
),
'Project' => array (
'id' => 1,
'name' => 'Project 1',
),
'Message' => array (
array (
'id' => 1,
'thread_id' => 1,
'name' => 'Thread 1, Message 1',
),
),
),
array (
'Thread' => array (
'id' => 3,
'project_id' => 2,
'name' => 'Project 2, Thread 1',
),
'Project' => array (
'id' => 2,
'name' => 'Project 2',
),
'Message' => array (
array (
'id' => 3,
'thread_id' => 3,
'name' => 'Thread 3, Message 1',
),
),
),
);
$this->assertEquals($expected, $result);
}
/** /**
* testGroupBy method * testGroupBy method
* *
@ -201,55 +276,12 @@ class ModelReadTest extends BaseModelTest {
* @return void * @return void
*/ */
public function testGroupBy() { public function testGroupBy() {
$isStrictGroupBy = $this->db instanceof Postgres || $this->db instanceof Sqlite || $this->db instanceof Oracle || $this->db instanceof Sqlserver; $this->skipIfIsStrictGroupBy();
$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'); $this->loadFixtures('Project', 'Product', 'Thread', 'Message', 'Bid');
$Thread = new Thread(); $Thread = new Thread();
$Product = new Product(); $Product = new Product();
$result = $Thread->find('all', array(
'group' => 'Thread.project_id',
'order' => 'Thread.id ASC'
));
$expected = array(
array(
'Thread' => array(
'id' => 1,
'project_id' => 1,
'name' => 'Project 1, Thread 1'
),
'Project' => array(
'id' => 1,
'name' => 'Project 1'
),
'Message' => array(
array(
'id' => 1,
'thread_id' => 1,
'name' => 'Thread 1, Message 1'
))),
array(
'Thread' => array(
'id' => 3,
'project_id' => 2,
'name' => 'Project 2, Thread 1'
),
'Project' => array(
'id' => 2,
'name' => 'Project 2'
),
'Message' => array(
array(
'id' => 3,
'thread_id' => 3,
'name' => 'Thread 3, Message 1'
))));
$this->assertEquals($expected, $result);
$rows = $Thread->find('all', array( $rows = $Thread->find('all', array(
'group' => 'Thread.project_id', 'group' => 'Thread.project_id',
'fields' => array('Thread.project_id', 'COUNT(*) AS total') 'fields' => array('Thread.project_id', 'COUNT(*) AS total')
@ -8159,6 +8191,29 @@ class ModelReadTest extends BaseModelTest {
$this->assertEquals($expected, $result); $this->assertEquals($expected, $result);
} }
public function testVirtualFieldsMysqlGroup() {
$this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFields test only compatible with Mysql.');
$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)',
);
$expectation = array(
'Post' => array(
'low_title' => 'first post',
'unique_test_field' => 1,
),
);
$result = $Post->find('first', array(
'fields' => array_keys($Post->virtualFields),
'group' => array('low_title'),
));
$this->assertEquals($expectation, $result);
}
/** /**
* testVirtualFieldsMysql() * testVirtualFieldsMysql()
* *
@ -8169,42 +8224,17 @@ class ModelReadTest extends BaseModelTest {
*/ */
public function testVirtualFieldsMysql() { public function testVirtualFieldsMysql() {
$this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFields test only compatible with Mysql.'); $this->skipIf(!($this->db instanceof Mysql), 'The rest of virtualFields test only compatible with Mysql.');
$this->loadFixtures('Author');
$this->loadFixtures('Post', 'Author');
$Post = ClassRegistry::init('Post');
$Post->create();
$Post->virtualFields = array(
'low_title' => 'lower(Post.title)',
'unique_test_field' => 'COUNT(Post.id)'
);
$expectation = array(
'Post' => array(
'low_title' => 'first post',
'unique_test_field' => 1
)
);
$result = $Post->find('first', array(
'fields' => array_keys($Post->virtualFields),
'group' => array('low_title')
));
$this->assertEquals($expectation, $result);
$Author = ClassRegistry::init('Author'); $Author = ClassRegistry::init('Author');
$Author->virtualFields = array( $Author->virtualFields = array(
'full_name' => 'CONCAT(Author.user, " ", Author.id)' 'full_name' => 'CONCAT(Author.user, " ", Author.id)'
); );
$result = $Author->find('first', array( $result = $Author->find('first', array(
'conditions' => array('Author.user' => 'mariano'), 'conditions' => array('Author.user' => 'mariano'),
'fields' => array('Author.password', 'Author.full_name'), 'fields' => array('Author.password', 'Author.full_name'),
'recursive' => -1 'recursive' => -1
)); ));
$this->assertTrue(isset($result['Author']['full_name'])); $this->assertTrue(isset($result['Author']['full_name']));
$result = $Author->find('first', array( $result = $Author->find('first', array(
'conditions' => array('Author.user' => 'mariano'), 'conditions' => array('Author.user' => 'mariano'),
'fields' => array('Author.full_name', 'Author.password'), 'fields' => array('Author.full_name', 'Author.password'),