Migrating DboPostgres to phpunit

This commit is contained in:
José Lorenzo Rodríguez 2010-06-09 22:52:28 -04:30
parent b3d9de1a97
commit 0a3e27b000

View file

@ -227,7 +227,7 @@ class DboPostgresTest extends CakeTestCase {
* @var DboSource * @var DboSource
* @access public * @access public
*/ */
public $db = null; public $Dbo = null;
/** /**
* Simulated DB connection used in testing * Simulated DB connection used in testing
@ -235,24 +235,7 @@ class DboPostgresTest extends CakeTestCase {
* @var DboSource * @var DboSource
* @access public * @access public
*/ */
public $db2 = null; public $Dbo2 = null;
/**
* Skip if cannot connect to postgres
*
*/
public function skip() {
$this->_initDb();
$this->skipUnless($this->db->config['driver'] == 'postgres', '%s PostgreSQL connection not available');
}
/**
* Set up test suite database connection
*
*/
public function startTest() {
$this->_initDb();
}
/** /**
* Sets up a Dbo class instance for testing * Sets up a Dbo class instance for testing
@ -260,9 +243,9 @@ class DboPostgresTest extends CakeTestCase {
*/ */
public function setUp() { public function setUp() {
Configure::write('Cache.disable', true); Configure::write('Cache.disable', true);
$this->startTest(); $this->Dbo = ConnectionManager::getDataSource('test_suite');
$this->db =& ConnectionManager::getDataSource('test_suite'); $this->Dbo2 = new DboPostgresTestDb($this->Dbo->config, false);
$this->db2 = new DboPostgresTestDb($this->db->config, false); $this->skipUnless($this->Dbo->config['driver'] == 'postgres', 'PostgreSQL connection not available');
$this->model = new PostgresTestModel(); $this->model = new PostgresTestModel();
} }
@ -272,7 +255,7 @@ class DboPostgresTest extends CakeTestCase {
*/ */
public function tearDown() { public function tearDown() {
Configure::write('Cache.disable', false); Configure::write('Cache.disable', false);
unset($this->db2); unset($this->Dbo2);
} }
/** /**
@ -301,21 +284,21 @@ class DboPostgresTest extends CakeTestCase {
'"PostgresTestModel"."updated" AS "PostgresTestModel__updated"' '"PostgresTestModel"."updated" AS "PostgresTestModel__updated"'
); );
$result = $this->db->fields($this->model); $result = $this->Dbo->fields($this->model);
$expected = $fields; $expected = $fields;
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->fields($this->model, null, 'PostgresTestModel.*'); $result = $this->Dbo->fields($this->model, null, 'PostgresTestModel.*');
$expected = $fields; $expected = $fields;
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name')); $result = $this->Dbo->fields($this->model, null, array('*', 'AnotherModel.id', 'AnotherModel.name'));
$expected = array_merge($fields, array( $expected = array_merge($fields, array(
'"AnotherModel"."id" AS "AnotherModel__id"', '"AnotherModel"."id" AS "AnotherModel__id"',
'"AnotherModel"."name" AS "AnotherModel__name"')); '"AnotherModel"."name" AS "AnotherModel__name"'));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->fields($this->model, null, array('*', 'PostgresClientTestModel.*')); $result = $this->Dbo->fields($this->model, null, array('*', 'PostgresClientTestModel.*'));
$expected = array_merge($fields, array( $expected = array_merge($fields, array(
'"PostgresClientTestModel"."id" AS "PostgresClientTestModel__id"', '"PostgresClientTestModel"."id" AS "PostgresClientTestModel__id"',
'"PostgresClientTestModel"."name" AS "PostgresClientTestModel__name"', '"PostgresClientTestModel"."name" AS "PostgresClientTestModel__name"',
@ -332,12 +315,12 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testColumnParsing() { function testColumnParsing() {
$this->assertEqual($this->db2->column('text'), 'text'); $this->assertEqual($this->Dbo2->column('text'), 'text');
$this->assertEqual($this->db2->column('date'), 'date'); $this->assertEqual($this->Dbo2->column('date'), 'date');
$this->assertEqual($this->db2->column('boolean'), 'boolean'); $this->assertEqual($this->Dbo2->column('boolean'), 'boolean');
$this->assertEqual($this->db2->column('character varying'), 'string'); $this->assertEqual($this->Dbo2->column('character varying'), 'string');
$this->assertEqual($this->db2->column('time without time zone'), 'time'); $this->assertEqual($this->Dbo2->column('time without time zone'), 'time');
$this->assertEqual($this->db2->column('timestamp without time zone'), 'datetime'); $this->assertEqual($this->Dbo2->column('timestamp without time zone'), 'datetime');
} }
/** /**
@ -347,30 +330,30 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testValueQuoting() { function testValueQuoting() {
$this->assertIdentical($this->db2->value(1.2, 'float'), "'1.2'"); $this->assertIdentical($this->Dbo2->value(1.2, 'float'), "'1.2'");
$this->assertEqual($this->db2->value('1,2', 'float'), "'1,2'"); $this->assertEqual($this->Dbo2->value('1,2', 'float'), "'1,2'");
$this->assertEqual($this->db2->value('0', 'integer'), "'0'"); $this->assertEqual($this->Dbo2->value('0', 'integer'), "'0'");
$this->assertEqual($this->db2->value('', 'integer'), 'NULL'); $this->assertEqual($this->Dbo2->value('', 'integer'), 'NULL');
$this->assertEqual($this->db2->value('', 'float'), 'NULL'); $this->assertEqual($this->Dbo2->value('', 'float'), 'NULL');
$this->assertEqual($this->db2->value('', 'integer', false), "DEFAULT"); $this->assertEqual($this->Dbo2->value('', 'integer', false), "DEFAULT");
$this->assertEqual($this->db2->value('', 'float', false), "DEFAULT"); $this->assertEqual($this->Dbo2->value('', 'float', false), "DEFAULT");
$this->assertEqual($this->db2->value('0.0', 'float'), "'0.0'"); $this->assertEqual($this->Dbo2->value('0.0', 'float'), "'0.0'");
$this->assertEqual($this->db2->value('t', 'boolean'), "TRUE"); $this->assertEqual($this->Dbo2->value('t', 'boolean'), "TRUE");
$this->assertEqual($this->db2->value('f', 'boolean'), "FALSE"); $this->assertEqual($this->Dbo2->value('f', 'boolean'), "FALSE");
$this->assertEqual($this->db2->value(true), "TRUE"); $this->assertEqual($this->Dbo2->value(true), "TRUE");
$this->assertEqual($this->db2->value(false), "FALSE"); $this->assertEqual($this->Dbo2->value(false), "FALSE");
$this->assertEqual($this->db2->value('t'), "'t'"); $this->assertEqual($this->Dbo2->value('t'), "'t'");
$this->assertEqual($this->db2->value('f'), "'f'"); $this->assertEqual($this->Dbo2->value('f'), "'f'");
$this->assertEqual($this->db2->value('true', 'boolean'), 'TRUE'); $this->assertEqual($this->Dbo2->value('true', 'boolean'), 'TRUE');
$this->assertEqual($this->db2->value('false', 'boolean'), 'FALSE'); $this->assertEqual($this->Dbo2->value('false', 'boolean'), 'FALSE');
$this->assertEqual($this->db2->value('', 'boolean'), 'FALSE'); $this->assertEqual($this->Dbo2->value('', 'boolean'), 'FALSE');
$this->assertEqual($this->db2->value(0, 'boolean'), 'FALSE'); $this->assertEqual($this->Dbo2->value(0, 'boolean'), 'FALSE');
$this->assertEqual($this->db2->value(1, 'boolean'), 'TRUE'); $this->assertEqual($this->Dbo2->value(1, 'boolean'), 'TRUE');
$this->assertEqual($this->db2->value('1', 'boolean'), 'TRUE'); $this->assertEqual($this->Dbo2->value('1', 'boolean'), 'TRUE');
$this->assertEqual($this->db2->value(null, 'boolean'), "NULL"); $this->assertEqual($this->Dbo2->value(null, 'boolean'), "NULL");
$this->assertEqual($this->db2->value(array()), "NULL"); $this->assertEqual($this->Dbo2->value(array()), "NULL");
} }
/** /**
@ -379,17 +362,17 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testDateAndTimeAsNull() { function testDateAndTimeAsNull() {
$this->assertEqual($this->db2->value(null, 'date'), 'NULL'); $this->assertEqual($this->Dbo2->value(null, 'date'), 'NULL');
$this->assertEqual($this->db2->value('', 'date'), 'NULL'); $this->assertEqual($this->Dbo2->value('', 'date'), 'NULL');
$this->assertEqual($this->db2->value('', 'datetime'), 'NULL'); $this->assertEqual($this->Dbo2->value('', 'datetime'), 'NULL');
$this->assertEqual($this->db2->value(null, 'datetime'), 'NULL'); $this->assertEqual($this->Dbo2->value(null, 'datetime'), 'NULL');
$this->assertEqual($this->db2->value('', 'timestamp'), 'NULL'); $this->assertEqual($this->Dbo2->value('', 'timestamp'), 'NULL');
$this->assertEqual($this->db2->value(null, 'timestamp'), 'NULL'); $this->assertEqual($this->Dbo2->value(null, 'timestamp'), 'NULL');
$this->assertEqual($this->db2->value('', 'time'), 'NULL'); $this->assertEqual($this->Dbo2->value('', 'time'), 'NULL');
$this->assertEqual($this->db2->value(null, 'time'), 'NULL'); $this->assertEqual($this->Dbo2->value(null, 'time'), 'NULL');
} }
/** /**
@ -399,19 +382,19 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testBooleanNormalization() { function testBooleanNormalization() {
$this->assertTrue($this->db2->boolean('t')); $this->assertTrue($this->Dbo2->boolean('t'));
$this->assertTrue($this->db2->boolean('true')); $this->assertTrue($this->Dbo2->boolean('true'));
$this->assertTrue($this->db2->boolean('TRUE')); $this->assertTrue($this->Dbo2->boolean('TRUE'));
$this->assertTrue($this->db2->boolean(true)); $this->assertTrue($this->Dbo2->boolean(true));
$this->assertTrue($this->db2->boolean(1)); $this->assertTrue($this->Dbo2->boolean(1));
$this->assertTrue($this->db2->boolean(" ")); $this->assertTrue($this->Dbo2->boolean(" "));
$this->assertFalse($this->db2->boolean('f')); $this->assertFalse($this->Dbo2->boolean('f'));
$this->assertFalse($this->db2->boolean('false')); $this->assertFalse($this->Dbo2->boolean('false'));
$this->assertFalse($this->db2->boolean('FALSE')); $this->assertFalse($this->Dbo2->boolean('FALSE'));
$this->assertFalse($this->db2->boolean(false)); $this->assertFalse($this->Dbo2->boolean(false));
$this->assertFalse($this->db2->boolean(0)); $this->assertFalse($this->Dbo2->boolean(0));
$this->assertFalse($this->db2->boolean('')); $this->assertFalse($this->Dbo2->boolean(''));
} }
/** /**
@ -421,14 +404,10 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testLastInsertIdMultipleInsert() { function testLastInsertIdMultipleInsert() {
$this->loadFixtures('User');
$db1 = ConnectionManager::getDataSource('test_suite'); $db1 = ConnectionManager::getDataSource('test_suite');
if (PHP5) { $db2 = clone $db1;
$db2 = clone $db1;
} else {
$db2 = $db1;
}
$db2->connect(); $db2->connect();
$this->assertNotEqual($db1->connection, $db2->connection); $this->assertNotEqual($db1->connection, $db2->connection);
@ -438,8 +417,8 @@ class DboPostgresTest extends CakeTestCase {
"INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')" "INSERT INTO {$table} (\"user\", password) VALUES ('mariano', '{$password}')"
); );
$db2->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')"); $db2->execute("INSERT INTO {$table} (\"user\", password) VALUES ('hoge', '{$password}')");
$this->assertEqual($db1->lastInsertId($table), 1); $this->assertEqual($db1->lastInsertId($table), 5);
$this->assertEqual($db2->lastInsertId($table), 2); $this->assertEqual($db2->lastInsertId($table), 6);
} }
/** /**
@ -449,12 +428,12 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testSchemaScoping() { function testSchemaScoping() {
$db1 =& ConnectionManager::getDataSource('test_suite'); $db1 = ConnectionManager::getDataSource('test_suite');
$db1->cacheSources = false; $db1->cacheSources = false;
$db1->reconnect(array('persistent' => false)); $db1->reconnect(array('persistent' => false));
$db1->query('CREATE SCHEMA _scope_test'); $db1->query('CREATE SCHEMA _scope_test');
$db2 =& ConnectionManager::create( $db2 = ConnectionManager::create(
'test_suite_2', 'test_suite_2',
array_merge($db1->config, array('driver' => 'postgres', 'schema' => '_scope_test')) array_merge($db1->config, array('driver' => 'postgres', 'schema' => '_scope_test'))
); );
@ -473,11 +452,11 @@ class DboPostgresTest extends CakeTestCase {
function testColumnUseLength() { function testColumnUseLength() {
$result = array('name' => 'foo', 'type' => 'string', 'length' => 100, 'default' => 'FOO'); $result = array('name' => 'foo', 'type' => 'string', 'length' => 100, 'default' => 'FOO');
$expected = '"foo" varchar(100) DEFAULT \'FOO\''; $expected = '"foo" varchar(100) DEFAULT \'FOO\'';
$this->assertEqual($this->db->buildColumn($result), $expected); $this->assertEqual($this->Dbo->buildColumn($result), $expected);
$result = array('name' => 'foo', 'type' => 'text', 'length' => 100, 'default' => 'FOO'); $result = array('name' => 'foo', 'type' => 'text', 'length' => 100, 'default' => 'FOO');
$expected = '"foo" text DEFAULT \'FOO\''; $expected = '"foo" text DEFAULT \'FOO\'';
$this->assertEqual($this->db->buildColumn($result), $expected); $this->assertEqual($this->Dbo->buildColumn($result), $expected);
} }
/** /**
@ -487,6 +466,7 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testBinaryDataIntegrity() { function testBinaryDataIntegrity() {
$this->loadFixtures('BinaryTest');
$data = '%PDF-1.3 $data = '%PDF-1.3
%ƒÂÚÂÎßÛ†–ƒ∆ %ƒÂÚÂÎßÛ†–ƒ∆
4 0 obj 4 0 obj
@ -506,7 +486,7 @@ class DboPostgresTest extends CakeTestCase {
ªºnh˚ºO^∏…®[Ó“ÅfıÌ≥∫F!(π∑T6`¬tΩÆ0ì»rTÎ`»Ñ« ªºnh˚ºO^∏…®[Ó“ÅfıÌ≥∫F!(π∑T6`¬tΩÆ0ì»rTÎ`»Ñ«
]≈åp˝)=¿Ô0∆öVÂmˇˆ„ø~¯ÁÔ∏b*fc»‡Îı„Ú}∆tœs∂Y∫ÜaÆ˙X∏~<ÿ·Ù vé1p¿TD∆ÔîÄ“úhˆ*Ú€îe)K p¨ÚJ3Ÿ∞ã>ÊuNê°“√Ü Ê9iÙ0˙AAEÍ ˙`∂£\'ûce•åƒXŸÁ´1SK{qdá"tÏ[wQ#SµBe∞∑µó…ÌV`B"Ñ≥„!è_Óφ-ºú¿Ë0ˆeê∂´ë+HFj…‡zvHÓN|ÔL÷ûñ3õÜ$z%sá…pÎóV38âs Çoµ•ß3†<9B·¨û~¢3)ÂxóÿÁCÕòÆ ∫Í=»ÿSπS;∆~±êÆTEp∑óÈ÷ÀuìDHÈ $ÉõæÜjû§"≤ÃONM®RËíRr{õS ∏Ê™op±W;ÂUÔ P∫kÔˇflTæ∑óflË” ÆC©Ô[≥◊HÁ˚¨hê"ÆbF?ú%h˙ˇ4xèÕ(ó2ÙáíM])Ñd|=fë-cI0ñL¢kÖêk‰Rƒ«ıÄWñ8mO3∏&√æËX¯Hó—ì]yF2»˜ádàà‡‹Çο„≥7mªHAS∑¶.;Œx(1} _kd©.fidç48M\áªCp^Krí<ɉXÓıïl!Ì$N<ı∞B»G]…∂Ó¯>˛ÔbõÒπÀ•:ôO<j∂™œ%âÏ—>@È$pÖuÊ´-QqV ?V≥JÆÍqÛX8(lπï@zgÖ}Fe<ˇ‡Sñ“ÿ˜ê?6‡L∫Oß~µ ?ËeäÚ®YîÕ =¢DÁu*GvBk;)L¬N«î:flö∂≠ÇΩq„Ñm하Ë"û≥§:±≤i^ΩÑ!)Wıyŧô á„RÄ÷Òôc≠—s™rıPdêãh˘ßHVç5fifiÈF€çÌÛuçÖ/M=µ±ÿGû1coÔuñæ. õ∑7ÉÏÜÆ,°H†ÍÉÌ∂7e º® íˆ◊øNWK”ÂYµñé;µ¶gV->µtË¥áßN2 ¯¶BaP-)eW.àôt^∏1C∑Ö?L„&”54jvãªZ ÷+4% ´0l…»ú^°´© ûiπ∑é®óܱÒÿ‰ïˆÌdˆ◊Æ19rQ=Í|ı•rMæ¬;ò‰Y‰é9. ˝V«ã¯∏,+ë®j*¡·/'; ]≈åp˝)=¿Ô0∆öVÂmˇˆ„ø~¯ÁÔ∏b*fc»‡Îı„Ú}∆tœs∂Y∫ÜaÆ˙X∏~<ÿ·Ù vé1p¿TD∆ÔîÄ“úhˆ*Ú€îe)K p¨ÚJ3Ÿ∞ã>ÊuNê°“√Ü Ê9iÙ0˙AAEÍ ˙`∂£\'ûce•åƒXŸÁ´1SK{qdá"tÏ[wQ#SµBe∞∑µó…ÌV`B"Ñ≥„!è_Óφ-ºú¿Ë0ˆeê∂´ë+HFj…‡zvHÓN|ÔL÷ûñ3õÜ$z%sá…pÎóV38âs Çoµ•ß3†<9B·¨û~¢3)ÂxóÿÁCÕòÆ ∫Í=»ÿSπS;∆~±êÆTEp∑óÈ÷ÀuìDHÈ $ÉõæÜjû§"≤ÃONM®RËíRr{õS ∏Ê™op±W;ÂUÔ P∫kÔˇflTæ∑óflË” ÆC©Ô[≥◊HÁ˚¨hê"ÆbF?ú%h˙ˇ4xèÕ(ó2ÙáíM])Ñd|=fë-cI0ñL¢kÖêk‰Rƒ«ıÄWñ8mO3∏&√æËX¯Hó—ì]yF2»˜ádàà‡‹Çο„≥7mªHAS∑¶.;Œx(1} _kd©.fidç48M\áªCp^Krí<ɉXÓıïl!Ì$N<ı∞B»G]…∂Ó¯>˛ÔbõÒπÀ•:ôO<j∂™œ%âÏ—>@È$pÖuÊ´-QqV ?V≥JÆÍqÛX8(lπï@zgÖ}Fe<ˇ‡Sñ“ÿ˜ê?6‡L∫Oß~µ ?ËeäÚ®YîÕ =¢DÁu*GvBk;)L¬N«î:flö∂≠ÇΩq„Ñm하Ë"û≥§:±≤i^ΩÑ!)Wıyŧô á„RÄ÷Òôc≠—s™rıPdêãh˘ßHVç5fifiÈF€çÌÛuçÖ/M=µ±ÿGû1coÔuñæ. õ∑7ÉÏÜÆ,°H†ÍÉÌ∂7e º® íˆ◊øNWK”ÂYµñé;µ¶gV->µtË¥áßN2 ¯¶BaP-)eW.àôt^∏1C∑Ö?L„&”54jvãªZ ÷+4% ´0l…»ú^°´© ûiπ∑é®óܱÒÿ‰ïˆÌdˆ◊Æ19rQ=Í|ı•rMæ¬;ò‰Y‰é9. ˝V«ã¯∏,+ë®j*¡·/';
$model =& new AppModel(array('name' => 'BinaryTest', 'ds' => 'test_suite')); $model = new AppModel(array('name' => 'BinaryTest', 'ds' => 'test_suite'));
$model->save(compact('data')); $model->save(compact('data'));
$result = $model->find('first'); $result = $model->find('first');
@ -542,7 +522,7 @@ class DboPostgresTest extends CakeTestCase {
) )
)); ));
$result = $this->db->createSchema($schema); $result = $this->Dbo->createSchema($schema);
$this->assertNoPattern('/^CREATE INDEX(.+);,$/', $result); $this->assertNoPattern('/^CREATE INDEX(.+);,$/', $result);
} }
@ -555,7 +535,7 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
public function testCakeSchema() { public function testCakeSchema() {
$db1 =& ConnectionManager::getDataSource('test_suite'); $db1 = ConnectionManager::getDataSource('test_suite');
$db1->cacheSources = false; $db1->cacheSources = false;
$db1->reconnect(array('persistent' => false)); $db1->reconnect(array('persistent' => false));
$db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' ( $db1->query('CREATE TABLE ' . $db1->fullTableName('datatypes') . ' (
@ -566,7 +546,7 @@ class DboPostgresTest extends CakeTestCase {
date date, date date,
CONSTRAINT test_suite_data_types_pkey PRIMARY KEY (id) CONSTRAINT test_suite_data_types_pkey PRIMARY KEY (id)
)'); )');
$model =& ClassRegistry::init('datatypes'); $model = ClassRegistry::init('datatypes');
$schema = new CakeSchema(array('connection' => 'test_suite')); $schema = new CakeSchema(array('connection' => 'test_suite'));
$result = $schema->read(array( $result = $schema->read(array(
'connection' => 'test_suite', 'connection' => 'test_suite',
@ -599,30 +579,30 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testIndexGeneration() { function testIndexGeneration() {
$name = $this->db->fullTableName('index_test', false); $name = $this->Dbo->fullTableName('index_test', false);
$this->db->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )'); $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )');
$this->db->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")'); $this->Dbo->query('CREATE INDEX pointless_bool ON ' . $name . '("bool")');
$this->db->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")'); $this->Dbo->query('CREATE UNIQUE INDEX char_index ON ' . $name . '("small_char")');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => true),
'pointless_bool' => array('column' => 'bool', 'unique' => 0), 'pointless_bool' => array('column' => 'bool', 'unique' => false),
'char_index' => array('column' => 'small_char', 'unique' => 1), 'char_index' => array('column' => 'small_char', 'unique' => true),
); );
$result = $this->db->index($name); $result = $this->Dbo->index($name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
$name = $this->db->fullTableName('index_test_2', false); $name = $this->Dbo->fullTableName('index_test_2', false);
$this->db->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )'); $this->Dbo->query('CREATE TABLE ' . $name . ' ("id" serial NOT NULL PRIMARY KEY, "bool" integer, "small_char" varchar(50), "description" varchar(40) )');
$this->db->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")'); $this->Dbo->query('CREATE UNIQUE INDEX multi_col ON ' . $name . '("small_char", "bool")');
$expected = array( $expected = array(
'PRIMARY' => array('column' => 'id', 'unique' => 1), 'PRIMARY' => array('column' => 'id', 'unique' => true),
'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => 1), 'multi_col' => array('column' => array('small_char', 'bool'), 'unique' => true),
); );
$result = $this->db->index($name); $result = $this->Dbo->index($name);
$this->assertEqual($expected, $result); $this->assertEqual($expected, $result);
$this->db->query('DROP TABLE ' . $name); $this->Dbo->query('DROP TABLE ' . $name);
} }
/** /**
@ -632,7 +612,7 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testAlterSchema() { function testAlterSchema() {
$Old =& new CakeSchema(array( $Old = new CakeSchema(array(
'connection' => 'test_suite', 'connection' => 'test_suite',
'name' => 'AlterPosts', 'name' => 'AlterPosts',
'alter_posts' => array( 'alter_posts' => array(
@ -645,9 +625,9 @@ class DboPostgresTest extends CakeTestCase {
'updated' => array('type' => 'datetime'), 'updated' => array('type' => 'datetime'),
) )
)); ));
$this->db->query($this->db->createSchema($Old)); $this->Dbo->query($this->Dbo->createSchema($Old));
$New =& new CakeSchema(array( $New = new CakeSchema(array(
'connection' => 'test_suite', 'connection' => 'test_suite',
'name' => 'AlterPosts', 'name' => 'AlterPosts',
'alter_posts' => array( 'alter_posts' => array(
@ -660,7 +640,7 @@ class DboPostgresTest extends CakeTestCase {
'updated' => array('type' => 'datetime'), 'updated' => array('type' => 'datetime'),
) )
)); ));
$this->db->query($this->db->alterSchema($New->compare($Old), 'alter_posts')); $this->Dbo->query($this->Dbo->alterSchema($New->compare($Old), 'alter_posts'));
$model = new CakeTestModel(array('table' => 'alter_posts', 'ds' => 'test_suite')); $model = new CakeTestModel(array('table' => 'alter_posts', 'ds' => 'test_suite'));
$result = $model->schema(); $result = $model->schema();
@ -671,7 +651,7 @@ class DboPostgresTest extends CakeTestCase {
$this->assertEqual($result['author_id']['null'], true); $this->assertEqual($result['author_id']['null'], true);
$this->assertEqual($result['title']['null'], false); $this->assertEqual($result['title']['null'], false);
$this->db->query($this->db->dropSchema($New)); $this->Dbo->query($this->Dbo->dropSchema($New));
} }
/** /**
@ -681,9 +661,9 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testAlterIndexes() { function testAlterIndexes() {
$this->db->cacheSources = false; $this->Dbo->cacheSources = false;
$schema1 =& new CakeSchema(array( $schema1 = new CakeSchema(array(
'name' => 'AlterTest1', 'name' => 'AlterTest1',
'connection' => 'test_suite', 'connection' => 'test_suite',
'altertest' => array( 'altertest' => array(
@ -693,9 +673,9 @@ class DboPostgresTest extends CakeTestCase {
'group2' => array('type' => 'integer', 'null' => true) 'group2' => array('type' => 'integer', 'null' => true)
) )
)); ));
$this->db->query($this->db->createSchema($schema1)); $this->Dbo->query($this->Dbo->createSchema($schema1));
$schema2 =& new CakeSchema(array( $schema2 = new CakeSchema(array(
'name' => 'AlterTest2', 'name' => 'AlterTest2',
'connection' => 'test_suite', 'connection' => 'test_suite',
'altertest' => array( 'altertest' => array(
@ -704,20 +684,20 @@ class DboPostgresTest extends CakeTestCase {
'group1' => array('type' => 'integer', 'null' => true), 'group1' => array('type' => 'integer', 'null' => true),
'group2' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true),
'indexes' => array( 'indexes' => array(
'name_idx' => array('column' => 'name', 'unique' => 0), 'name_idx' => array('column' => 'name', 'unique' => false),
'group_idx' => array('column' => 'group1', 'unique' => 0), 'group_idx' => array('column' => 'group1', 'unique' => false),
'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => 0), 'compound_idx' => array('column' => array('group1', 'group2'), 'unique' => false),
'PRIMARY' => array('column' => 'id', 'unique' => 1) 'PRIMARY' => array('column' => 'id', 'unique' => true)
) )
) )
)); ));
$this->db->query($this->db->alterSchema($schema2->compare($schema1))); $this->Dbo->query($this->Dbo->alterSchema($schema2->compare($schema1)));
$indexes = $this->db->index('altertest'); $indexes = $this->Dbo->index('altertest');
$this->assertEqual($schema2->tables['altertest']['indexes'], $indexes); $this->assertEqual($schema2->tables['altertest']['indexes'], $indexes);
// Change three indexes, delete one and add another one // Change three indexes, delete one and add another one
$schema3 =& new CakeSchema(array( $schema3 = new CakeSchema(array(
'name' => 'AlterTest3', 'name' => 'AlterTest3',
'connection' => 'test_suite', 'connection' => 'test_suite',
'altertest' => array( 'altertest' => array(
@ -726,27 +706,27 @@ class DboPostgresTest extends CakeTestCase {
'group1' => array('type' => 'integer', 'null' => true), 'group1' => array('type' => 'integer', 'null' => true),
'group2' => array('type' => 'integer', 'null' => true), 'group2' => array('type' => 'integer', 'null' => true),
'indexes' => array( 'indexes' => array(
'name_idx' => array('column' => 'name', 'unique' => 1), 'name_idx' => array('column' => 'name', 'unique' => true),
'group_idx' => array('column' => 'group2', 'unique' => 0), 'group_idx' => array('column' => 'group2', 'unique' => false),
'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => 0), 'compound_idx' => array('column' => array('group2', 'group1'), 'unique' => false),
'another_idx' => array('column' => array('group1', 'name'), 'unique' => 0)) 'another_idx' => array('column' => array('group1', 'name'), 'unique' => false))
))); )));
$this->db->query($this->db->alterSchema($schema3->compare($schema2))); $this->Dbo->query($this->Dbo->alterSchema($schema3->compare($schema2)));
$indexes = $this->db->index('altertest'); $indexes = $this->Dbo->index('altertest');
$this->assertEqual($schema3->tables['altertest']['indexes'], $indexes); $this->assertEqual($schema3->tables['altertest']['indexes'], $indexes);
// Compare us to ourself. // Compare us to ourself.
$this->assertEqual($schema3->compare($schema3), array()); $this->assertEqual($schema3->compare($schema3), array());
// Drop the indexes // Drop the indexes
$this->db->query($this->db->alterSchema($schema1->compare($schema3))); $this->Dbo->query($this->Dbo->alterSchema($schema1->compare($schema3)));
$indexes = $this->db->index('altertest'); $indexes = $this->Dbo->index('altertest');
$this->assertEqual(array(), $indexes); $this->assertEqual(array(), $indexes);
$this->db->query($this->db->dropSchema($schema1)); $this->Dbo->query($this->Dbo->dropSchema($schema1));
} }
/* /*
@ -756,7 +736,7 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testVirtualFields() { function testVirtualFields() {
$this->loadFixtures('Article', 'Comment'); $this->loadFixtures('Article', 'Comment', 'User', 'Attachment', 'Tag', 'ArticlesTag');
$Article = new Article; $Article = new Article;
$Article->virtualFields = array( $Article->virtualFields = array(
'next_id' => 'Article.id + 1', 'next_id' => 'Article.id + 1',
@ -767,7 +747,7 @@ class DboPostgresTest extends CakeTestCase {
$result = $Article->find('first'); $result = $Article->find('first');
$this->assertEqual($result['Article']['next_id'], 2); $this->assertEqual($result['Article']['next_id'], 2);
$this->assertEqual($result['Article']['complex'], $result['Article']['title'] . $result['Article']['body']); $this->assertEqual($result['Article']['complex'], $result['Article']['title'] . $result['Article']['body']);
$this->assertEqual($result['Article']['functional'], $result['Article']['title']); $this->assertEqual($result['Article']['functional'], $result['User']['user']);
$this->assertEqual($result['Article']['subquery'], 6); $this->assertEqual($result['Article']['subquery'], 6);
} }
@ -778,7 +758,7 @@ class DboPostgresTest extends CakeTestCase {
* @return void * @return void
*/ */
function testOrderAdditionalParams() { function testOrderAdditionalParams() {
$result = $this->db->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC')); $result = $this->Dbo->order(array('title' => 'DESC NULLS FIRST', 'body' => 'DESC'));
$expected = ' ORDER BY "title" DESC NULLS FIRST, "body" DESC'; $expected = ' ORDER BY "title" DESC NULLS FIRST, "body" DESC';
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
@ -789,15 +769,15 @@ class DboPostgresTest extends CakeTestCase {
function testQuoteDistinctInFunction() { function testQuoteDistinctInFunction() {
$this->loadFixtures('Article'); $this->loadFixtures('Article');
$Article = new Article; $Article = new Article;
$result = $this->db->fields($Article, null, array('COUNT(DISTINCT Article.id)')); $result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT Article.id)'));
$expected = array('COUNT(DISTINCT "Article"."id")'); $expected = array('COUNT(DISTINCT "Article"."id")');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->fields($Article, null, array('COUNT(DISTINCT id)')); $result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT id)'));
$expected = array('COUNT(DISTINCT "id")'); $expected = array('COUNT(DISTINCT "id")');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->db->fields($Article, null, array('COUNT(DISTINCT FUNC(id))')); $result = $this->Dbo->fields($Article, null, array('COUNT(DISTINCT FUNC(id))'));
$expected = array('COUNT(DISTINCT FUNC("id"))'); $expected = array('COUNT(DISTINCT FUNC("id"))');
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }