Updating model tests for Postgres, fixing Postgres metadata handling for UUID primary keys, adding Postgres tests

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6558 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-03-11 05:22:17 +00:00
parent c1c35b2e13
commit 2c3ec768c7
4 changed files with 45 additions and 21 deletions

View file

@ -104,10 +104,10 @@ class DboPostgres extends DboSource {
* @return boolean True if the database could be disconnected, else false
*/
function disconnect() {
if (is_resource($this->results)) {
pg_free_result($this->results);
if (is_resource($this->_result)) {
pg_free_result($this->_result);
}
if (is_resource($this->connected)) {
if (is_resource($this->connection)) {
$this->connected = !pg_close($this->connection);
} else {
$this->connected = false;
@ -176,6 +176,7 @@ class DboPostgres extends DboSource {
if (isset($column[0])) {
$c = $column[0];
if (!empty($c['char_length'])) {
$length = intval($c['char_length']);
} elseif (!empty($c['oct_length'])) {
@ -191,7 +192,9 @@ class DboPostgres extends DboSource {
);
if ($c['name'] == $model->primaryKey) {
$fields[$c['name']]['key'] = 'primary';
$fields[$c['name']]['length'] = 11;
if ($fields[$c['name']]['type'] !== 'string') {
$fields[$c['name']]['length'] = 11;
}
}
if ($fields[$c['name']]['default'] == 'NULL' || preg_match('/nextval\([\'"]?(\w+)/', $c['default'], $seq)) {
$fields[$c['name']]['default'] = null;
@ -558,20 +561,15 @@ class DboPostgres extends DboSource {
* @return mixed Converted boolean value
*/
function boolean($data, $quote = true) {
$result = null;
if ($data === true || $data === false) {
$result = $data;
} elseif (is_string($data) && !is_numeric($data)) {
if (strpos(strtolower($data), 't') !== false) {
$result = true;
} else {
$result = false;
}
} else {
$result = (bool)$data;
switch (true) {
case ($data === true || $data === false):
return $data;
case ($data === 't' || $data === 'f'):
return ($data === 't');
default:
return (bool)$data;
break;
}
return $result;
}
/**
* Sets the database encoding

View file

@ -264,7 +264,6 @@ class DboSource extends DataSource {
* @return array The fetched row as an array
*/
function fetchRow($sql = null) {
if (!empty($sql) && is_string($sql) && strlen($sql) > 5) {
if (!$this->execute($sql)) {
return null;

View file

@ -186,8 +186,12 @@ class DboPostgresTest extends CakeTestCase {
}
function testColumnParsing() {
var_export($this->db->isConnected());
var_export($this->db->fetchAll("SELECT table_name as name FROM INFORMATION_SCHEMA.tables;"));
$this->assertEqual($this->db->column('text'), 'text');
$this->assertEqual($this->db->column('date'), 'date');
$this->assertEqual($this->db->column('boolean'), 'boolean');
$this->assertEqual($this->db->column('character varying'), 'string');
$this->assertEqual($this->db->column('time without time zone'), 'time');
$this->assertEqual($this->db->column('timestamp without time zone'), 'datetime');
}
}

View file

@ -138,7 +138,30 @@ class ModelTest extends CakeTestCase {
function testHabtmFinderQuery() {
$this->loadFixtures('Article', 'Tag', 'ArticlesTag');
$this->Article =& new Article();
$this->Article->hasAndBelongsToMany['Tag']['finderQuery'] = 'SELECT Tag.id, Tag.tag, ArticlesTag.article_id, ArticlesTag.tag_id FROM tags AS Tag JOIN articles_tags AS ArticlesTag ON (ArticlesTag.article_id = {$__cakeID__$} AND ArticlesTag.tag_id = Tag.id)';
$db =& ConnectionManager::getDataSource('test_suite');
$sql = $db->buildStatement(
array(
'fields' => $db->fields($this->Article->Tag, null, array('Tag.id', 'Tag.tag', 'ArticlesTag.article_id', 'ArticlesTag.tag_id')),
'table' => 'tags',
'alias' => 'Tag',
'limit' => null,
'offset' => null,
'joins' => array(array(
'alias' => 'ArticlesTag',
'table' => 'articles_tags',
'conditions' => array(
array("ArticlesTag.article_id" => '{$__cakeID__$}'),
array("ArticlesTag.tag_id" => '{$__cakeIdentifier[Tag.id]__$}')
)
)),
'conditions' => array(),
'order' => null
),
$this->Article
);
$this->Article->hasAndBelongsToMany['Tag']['finderQuery'] = $sql;
$result = $this->Article->find('first');
$expected = array(array('id' => '1', 'tag' => 'tag1'), array('id' => '2', 'tag' => 'tag2'));
$this->assertEqual($result['Tag'], $expected);