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
This commit is contained in:
nate 2008-10-01 15:27:29 +00:00
parent 1a0299d257
commit 2bfd5a4b3e
5 changed files with 83 additions and 48 deletions

View file

@ -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);

View file

@ -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;

View file

@ -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) {

View file

@ -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`');

View file

@ -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')));