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

View file

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

View file

@ -186,8 +186,12 @@ class DboPostgresTest extends CakeTestCase {
} }
function testColumnParsing() { function testColumnParsing() {
var_export($this->db->isConnected()); $this->assertEqual($this->db->column('text'), 'text');
var_export($this->db->fetchAll("SELECT table_name as name FROM INFORMATION_SCHEMA.tables;")); $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() { function testHabtmFinderQuery() {
$this->loadFixtures('Article', 'Tag', 'ArticlesTag'); $this->loadFixtures('Article', 'Tag', 'ArticlesTag');
$this->Article =& new Article(); $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'); $result = $this->Article->find('first');
$expected = array(array('id' => '1', 'tag' => 'tag1'), array('id' => '2', 'tag' => 'tag2')); $expected = array(array('id' => '1', 'tag' => 'tag1'), array('id' => '2', 'tag' => 'tag2'));
$this->assertEqual($result['Tag'], $expected); $this->assertEqual($result['Tag'], $expected);