From 2bfd5a4b3e8acde6c80a9d44f77a9da4c844874d Mon Sep 17 00:00:00 2001 From: nate Date: Wed, 1 Oct 2008 15:27:29 +0000 Subject: [PATCH] Refactoring schema test and fixing formatting, refactoring Model::_findCount() to use DboSource::calculate() in all cases, closes #4501 git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7684 3807eeeb-6ff5-0310-8944-8be069107fe0 --- cake/libs/model/datasources/dbo_source.php | 3 + cake/libs/model/model.php | 6 +- cake/libs/model/schema.php | 11 +- .../model/datasources/dbo_source.test.php | 9 +- cake/tests/cases/libs/model/schema.test.php | 102 ++++++++++-------- 5 files changed, 83 insertions(+), 48 deletions(-) diff --git a/cake/libs/model/datasources/dbo_source.php b/cake/libs/model/datasources/dbo_source.php index 58291dfe7..1727bb162 100644 --- a/cake/libs/model/datasources/dbo_source.php +++ b/cake/libs/model/datasources/dbo_source.php @@ -390,6 +390,9 @@ class DboSource extends DataSource { if ($data == '*') { return '*'; } + if (is_object($data) && isset($data->type)) { + return $data->value; + } $array = is_array($data); $data = (array)$data; $count = count($data); diff --git a/cake/libs/model/model.php b/cake/libs/model/model.php index f45fac16c..52a5858a2 100644 --- a/cake/libs/model/model.php +++ b/cake/libs/model/model.php @@ -1870,11 +1870,13 @@ class Model extends Overloadable { */ function _findCount($state, $query, $results = array()) { if ($state == 'before') { + $db =& ConnectionManager::getDataSource($this->useDbConfig); if (empty($query['fields'])) { - $db =& ConnectionManager::getDataSource($this->useDbConfig); $query['fields'] = $db->calculate($this, 'count'); } elseif (is_string($query['fields']) && !preg_match('/count/i', $query['fields'])) { - $query['fields'] = "COUNT({$query['fields']}) as count"; + $query['fields'] = $db->calculate($this, 'count', array( + $db->expression($query['fields']), 'count' + )); } $query['order'] = false; return $query; diff --git a/cake/libs/model/schema.php b/cake/libs/model/schema.php index 6cc4067f7..8f143fda1 100644 --- a/cake/libs/model/schema.php +++ b/cake/libs/model/schema.php @@ -250,8 +250,15 @@ class CakeSchema extends Object { } $table = str_replace($prefix, '', $table); } - $Object = new AppModel(array('name' => Inflector::classify($table), 'table' => $table, 'ds' => $connection)); - if (in_array($table, array('aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n'))) { + $Object = new AppModel(array( + 'name' => Inflector::classify($table), 'table' => $table, 'ds' => $connection + )); + + $systemTables = array( + 'aros', 'acos', 'aros_acos', Configure::read('Session.table'), 'i18n' + ); + + if (in_array($table, $systemTables)) { $tables[$Object->table] = $this->__columns($Object); $tables[$Object->table]['indexes'] = $db->index($Object); } elseif ($models === false) { diff --git a/cake/tests/cases/libs/model/datasources/dbo_source.test.php b/cake/tests/cases/libs/model/datasources/dbo_source.test.php index 4e3861790..4e618f598 100644 --- a/cake/tests/cases/libs/model/datasources/dbo_source.test.php +++ b/cake/tests/cases/libs/model/datasources/dbo_source.test.php @@ -3213,10 +3213,17 @@ class DboSourceTest extends CakeTestCase { function testCalculations() { $result = $this->testDb->calculate($this->Model, 'count'); $this->assertEqual($result, 'COUNT(*) AS `count`'); - + $result = $this->testDb->calculate($this->Model, 'count', array('id')); $this->assertEqual($result, 'COUNT(`id`) AS `count`'); + $result = $this->testDb->calculate( + $this->Model, + 'count', + array($this->testDb->expression('DISTINCT id')) + ); + $this->assertEqual($result, 'COUNT(DISTINCT id) AS `count`'); + $result = $this->testDb->calculate($this->Model, 'count', array('id', 'id_count')); $this->assertEqual($result, 'COUNT(`id`) AS `id_count`'); diff --git a/cake/tests/cases/libs/model/schema.test.php b/cake/tests/cases/libs/model/schema.test.php index 191ad4244..235fd7162 100644 --- a/cake/tests/cases/libs/model/schema.test.php +++ b/cake/tests/cases/libs/model/schema.test.php @@ -122,15 +122,15 @@ class TestAppSchema extends CakeSchema { * @access public */ var $comments = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => 0,'key' => 'primary'), - 'article_id' => array('type' => 'integer', 'null' => false), - 'user_id' => array('type' => 'integer', 'null' => false), - 'comment' => array('type' => 'text', 'null' => true, 'default' => null), - 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1), - 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), - 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), - 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), - ); + 'id' => array('type' => 'integer', 'null' => false, 'default' => 0,'key' => 'primary'), + 'article_id' => array('type' => 'integer', 'null' => false), + 'user_id' => array('type' => 'integer', 'null' => false), + 'comment' => array('type' => 'text', 'null' => true, 'default' => null), + 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1), + 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), + ); /** * posts property * @@ -138,15 +138,15 @@ class TestAppSchema extends CakeSchema { * @access public */ var $posts = array( - 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), - 'author_id' => array('type' => 'integer', 'null' => false), - 'title' => array('type' => 'string', 'null' => false), - 'body' => array('type' => 'text', 'null' => true, 'default' => null), - 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1), - 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), - 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), - 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), - ); + 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), + 'author_id' => array('type' => 'integer', 'null' => false), + 'title' => array('type' => 'string', 'null' => false), + 'body' => array('type' => 'text', 'null' => true, 'default' => null), + 'published' => array('type' => 'string', 'null' => true, 'default' => 'N', 'length' => 1), + 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), + 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), + ); /** * posts_tags property * @@ -154,8 +154,8 @@ class TestAppSchema extends CakeSchema { * @access public */ var $posts_tags = array( - 'post_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'), - 'tag_id' => array('type' => 'string', 'null' => false), + 'post_id' => array('type' => 'integer', 'null' => false), + 'tag_id' => array('type' => 'string', 'null' => false, 'key' => 'primary'), 'indexes' => array() ); /** @@ -169,7 +169,7 @@ class TestAppSchema extends CakeSchema { 'tag' => array('type' => 'string', 'null' => false), 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), 'updated' => array('type' => 'datetime', 'null' => true, 'default' => null), - 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), + 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)) ); /** * datatypes property @@ -180,7 +180,7 @@ class TestAppSchema extends CakeSchema { var $datatypes = array( 'id' => array('type' => 'integer', 'null' => false, 'default' => 0, 'key' => 'primary'), 'float_field' => array('type' => 'float', 'null' => false, 'length' => '5,2'), - 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)), + 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => true)) ); /** * setup method @@ -383,11 +383,19 @@ class CakeSchemaTest extends CakeTestCase { * @return void */ function testSchemaRead() { - $read = $this->Schema->read(array('connection' => 'test_suite', 'name' => 'TestApp', 'models' => array('SchemaPost', 'SchemaComment', 'SchemaTag', 'SchemaDatatype'))); + $read = $this->Schema->read(array( + 'connection' => 'test_suite', + 'name' => 'TestApp', + 'models' => array('SchemaPost', 'SchemaComment', 'SchemaTag', 'SchemaDatatype') + )); unset($read['tables']['missing']); + $this->assertEqual($read['tables'], $this->Schema->tables); - $this->assertIdentical($read['tables']['datatypes']['float_field'], $this->Schema->tables['datatypes']['float_field']); - + $this->assertIdentical( + $read['tables']['datatypes']['float_field'], + $this->Schema->tables['datatypes']['float_field'] + ); + $db =& ConnectionManager::getDataSource('test_suite'); $config = $db->config; $config['prefix'] = 'schema_test_prefix_'; @@ -421,23 +429,27 @@ class CakeSchemaTest extends CakeTestCase { $New = new MyAppSchema(); $compare = $New->compare($this->Schema); $expected = array( - 'comments' => array( - 'add' => array('post_id' => array('type' => 'integer', 'null' => false, 'default' => 0), - 'title' => array('type' => 'string', 'null' => false, 'length' => 100) - ), - 'drop' => array('article_id' => array('type' => 'integer', 'null' => false)), - 'change' => array('comment' => array('type' => 'text', 'null' => false, 'default' => null)) - - ), - 'posts' => array( - 'add' => array('summary' => array('type' => 'text', 'null' => 1)), - 'change' => array('author_id' => array('type' => 'integer', 'null' => true, 'default' => ''), - 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'), - 'published' => array('type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1') - ), - ), - ); - + 'comments' => array( + 'add' => array( + 'post_id' => array('type' => 'integer', 'null' => false, 'default' => 0), + 'title' => array('type' => 'string', 'null' => false, 'length' => 100) + ), + 'drop' => array('article_id' => array('type' => 'integer', 'null' => false)), + 'change' => array( + 'comment' => array('type' => 'text', 'null' => false, 'default' => null) + ) + ), + 'posts' => array( + 'add' => array('summary' => array('type' => 'text', 'null' => 1)), + 'change' => array( + 'author_id' => array('type' => 'integer', 'null' => true, 'default' => ''), + 'title' => array('type' => 'string', 'null' => false, 'default' => 'Title'), + 'published' => array( + 'type' => 'string', 'null' => true, 'default' => 'Y', 'length' => '1' + ) + ) + ), + ); $this->assertEqual($expected, $compare); } /** @@ -461,7 +473,11 @@ class CakeSchemaTest extends CakeTestCase { $db =& ConnectionManager::getDataSource('test_suite'); $db->cacheSources = false; - $db->query('CREATE TABLE ' . $db->fullTableName('testdescribes') . ' (id int(11) AUTO_INCREMENT, int_null int(10) unsigned NULL, int_not_null int(10) unsigned NOT NULL, primary key(id));'); + $db->query( + 'CREATE TABLE ' . $db->fullTableName('testdescribes') . ' (id int(11) AUTO_INCREMENT' . + ', int_null int(10) unsigned NULL, int_not_null int(10) unsigned NOT NULL, primary ' . + 'key(id));' + ); $Schema =& new CakeSchema(array('connection' => 'test_suite')); $read = $Schema->read(array('models' => array('Testdescribe')));