From 944e4ce6960a72b97f5b32ef32ebf9936eaedf16 Mon Sep 17 00:00:00 2001 From: Simon JAILLET Date: Wed, 27 Mar 2013 16:17:09 +0100 Subject: [PATCH] Refactor & move database adapter `PostgreSqlTest` to integration tests. --- .../database/adapter/PostgreSqlTest.php | 190 ++++++++++-------- 1 file changed, 111 insertions(+), 79 deletions(-) rename tests/{cases => integration}/data/source/database/adapter/PostgreSqlTest.php (54%) diff --git a/tests/cases/data/source/database/adapter/PostgreSqlTest.php b/tests/integration/data/source/database/adapter/PostgreSqlTest.php similarity index 54% rename from tests/cases/data/source/database/adapter/PostgreSqlTest.php rename to tests/integration/data/source/database/adapter/PostgreSqlTest.php index 62497466cd..358046e5ed 100644 --- a/tests/cases/data/source/database/adapter/PostgreSqlTest.php +++ b/tests/integration/data/source/database/adapter/PostgreSqlTest.php @@ -6,41 +6,58 @@ * @license http://opensource.org/licenses/bsd-license.php The BSD License */ -namespace lithium\tests\cases\data\source\database\adapter; +namespace lithium\tests\integration\data\source\database\adapter; -use lithium\core\Libraries; +use lithium\data\Schema; use lithium\data\Connections; use lithium\data\model\Query; use lithium\data\source\database\adapter\PostgreSql; use lithium\tests\mocks\data\source\database\adapter\MockPostgreSql; +use lithium\tests\fixture\model\gallery\Galleries; -class PostgreSqlTest extends \lithium\test\Unit { +class PostgreSqlTest extends \lithium\tests\integration\data\Base { - protected $_dbConfig = array(); - - protected $_model = 'lithium\tests\mocks\data\model\MockDatabasePost'; - - public $db = null; + protected $_schema = array( + 'fields' => array( + 'id' => array('type' => 'id'), + 'name' => array('type' => 'string', 'length' => 255), + 'active' => array('type' => 'boolean'), + 'created' => array('type' => 'datetime', 'null' => true), + 'modified' => array('type' => 'datetime', 'null' => true) + ) + ); /** * Skip the test if a PostgreSQL adapter configuration is unavailable. - * @todo Tie into the Environment class to ensure that the test database is being used. */ public function skip() { - $this->skipIf(!PostgreSql::enabled(), 'PostgreSQL Extension is not loaded'); + parent::connect($this->_connection); + $this->skipIf(!$this->with(array('PostgreSql'))); + } - $adapter = 'PostgreSql'; - $this->_dbConfig = Connections::get('test', array('config' => true)); - $hasDb = (isset($this->_dbConfig['adapter']) && $this->_dbConfig['adapter'] === $adapter); - $message = 'Test database is either unavailable, or not using a PostgreSQL adapter'; - $this->skipIf(!$hasDb, $message); + public function setUp() { + $this->_db->dropSchema('galleries'); + $schema = new Schema($this->_schema); + $this->_db->createSchema('galleries', $schema); + } - $this->db = new PostgreSql($this->_dbConfig); + public function tearDown() { + $this->_db->dropSchema('galleries'); + } - $lithium = Libraries::get('lithium', 'path'); - $sqlFile = $lithium . '/tests/mocks/data/source/database/adapter/postgresql_companies.sql'; - $sql = file_get_contents($sqlFile); - $this->db->read($sql, array('return' => 'resource')); + public function testEnabledFeatures() { + $supported = array('booleans', 'schema', 'relationships', 'sources', 'transactions'); + $notSupported = array('arrays'); + + foreach($supported as $feature) { + $this->assertTrue(PostgreSql::enabled($feature)); + } + + foreach($notSupported as $feature) { + $this->assertFalse(PostgreSql::enabled($feature)); + } + + $this->assertNull(PostgreSql::enabled('unexisting')); } /** @@ -86,69 +103,66 @@ public function testDatabaseConnection() { } public function testDatabaseEncoding() { - $this->assertTrue($this->db->isConnected()); - $this->assertTrue($this->db->encoding('utf8')); - $this->assertEqual('UTF-8', $this->db->encoding()); - - $this->assertTrue($this->db->encoding('UTF-8')); - $this->assertEqual('UTF-8', $this->db->encoding()); - } - - public function testDatabaseTimezone() { - $this->assertTrue($this->db->isConnected()); - $this->assertTrue($this->db->timezone('UTC')); - $this->assertEqual('UTC', $this->db->timezone()); + $this->assertTrue($this->_db->isConnected()); + $this->assertTrue($this->_db->encoding('utf8')); + $this->assertEqual('UTF-8', $this->_db->encoding()); - $this->assertTrue($this->db->timezone('US/Eastern')); - $this->assertEqual('US/Eastern', $this->db->timezone()); + $this->assertTrue($this->_db->encoding('UTF-8')); + $this->assertEqual('UTF-8', $this->_db->encoding()); } public function testValueByIntrospect() { $expected = "'string'"; - $result = $this->db->value("string"); + $result = $this->_db->value("string"); $this->assertInternalType('string', $result); $this->assertEqual($expected, $result); $expected = "'''this string is escaped'''"; - $result = $this->db->value("'this string is escaped'"); + $result = $this->_db->value("'this string is escaped'"); $this->assertInternalType('string', $result); $this->assertEqual($expected, $result); - $this->assertIdentical("'t'", $this->db->value(true)); - $this->assertIdentical(1, $this->db->value('1')); - $this->assertIdentical(1.1, $this->db->value('1.1')); + $this->assertIdentical("'t'", $this->_db->value(true)); + $this->assertIdentical(1, $this->_db->value('1')); + $this->assertIdentical(1.1, $this->_db->value('1.1')); + } + + public function testNameQuoting() { + $result = $this->_db->name('title'); + $expected = '"title"'; + $this->assertEqual($expected, $result); } public function testColumnAbstraction() { - $result = $this->db->invokeMethod('_column', array('varchar')); + $result = $this->_db->invokeMethod('_column', array('varchar')); $this->assertIdentical(array('type' => 'string'), $result); - $result = $this->db->invokeMethod('_column', array('tinyint(1)')); + $result = $this->_db->invokeMethod('_column', array('tinyint(1)')); $this->assertIdentical(array('type' => 'boolean'), $result); - $result = $this->db->invokeMethod('_column', array('varchar(255)')); + $result = $this->_db->invokeMethod('_column', array('varchar(255)')); $this->assertIdentical(array('type' => 'string', 'length' => 255), $result); - $result = $this->db->invokeMethod('_column', array('text')); + $result = $this->_db->invokeMethod('_column', array('text')); $this->assertIdentical(array('type' => 'text'), $result); - $result = $this->db->invokeMethod('_column', array('text')); + $result = $this->_db->invokeMethod('_column', array('text')); $this->assertIdentical(array('type' => 'text'), $result); - $result = $this->db->invokeMethod('_column', array('decimal(12,2)')); + $result = $this->_db->invokeMethod('_column', array('decimal(12,2)')); $this->assertIdentical(array('type' => 'float', 'length' => 12, 'precision' => 2), $result); - $result = $this->db->invokeMethod('_column', array('int(11)')); + $result = $this->_db->invokeMethod('_column', array('int(11)')); $this->assertIdentical(array('type' => 'integer', 'length' => 11), $result); } public function testRawSqlQuerying() { - $this->assertTrue($this->db->create( - 'INSERT INTO companies (name, active) VALUES (?, ?)', + $this->assertTrue($this->_db->create( + 'INSERT INTO galleries (name, active) VALUES (?, ?)', array('Test', "t") )); - $result = $this->db->read('SELECT * From companies AS Company WHERE name = {:name}', array( + $result = $this->_db->read('SELECT * FROM galleries AS Company WHERE name = {:name}', array( 'name' => 'Test', 'return' => 'array' )); @@ -167,33 +181,24 @@ public function testRawSqlQuerying() { ); $this->assertIdentical($expected, $result[0]); - $this->assertTrue($this->db->delete('DELETE From companies WHERE name = {:name}', array( + $this->assertTrue($this->_db->delete('DELETE FROM galleries WHERE name = {:name}', array( 'name' => 'Test' ))); - $result = $this->db->read('SELECT * From companies AS Company WHERE name = {:name}', array( + $result = $this->_db->read('SELECT * FROM galleries AS Company WHERE name = {:name}', array( 'name' => 'Test', 'return' => 'array' )); $this->assertEmpty($result); } - public function testAbstractColumnResolution() { - } - public function testExecuteException() { $this->expectException(); - $this->db->read('SELECT deliberate syntax error'); - } - - public function testEnabledFeatures() { - $this->assertTrue(PostgreSql::enabled()); - $this->assertTrue(PostgreSql::enabled('relationships')); - $this->assertFalse(PostgreSql::enabled('arrays')); + $this->_db->read('SELECT deliberate syntax error'); } public function testEntityQuerying() { - $sources = $this->db->sources(); + $sources = $this->_db->sources(); $this->assertInternalType('array', $sources); $this->assertNotEmpty($sources); } @@ -201,34 +206,34 @@ public function testEntityQuerying() { public function testQueryOrdering() { $insert = new Query(array( 'type' => 'create', - 'source' => 'companies', + 'source' => 'galleries', 'data' => array( 'name' => 'Foo', 'active' => true, 'created' => date('Y-m-d H:i:s') ) )); - $this->assertTrue($this->db->create($insert)); + $this->assertTrue($this->_db->create($insert)); $insert->data(array( 'name' => 'Bar', 'created' => date('Y-m-d H:i:s', strtotime('-5 minutes')) )); - $this->assertTrue($this->db->create($insert)); + $this->assertTrue($this->_db->create($insert)); $insert->data(array( 'name' => 'Baz', 'created' => date('Y-m-d H:i:s', strtotime('-10 minutes')) )); - $this->assertTrue($this->db->create($insert)); + $this->assertTrue($this->_db->create($insert)); $read = new Query(array( 'type' => 'read', - 'source' => 'companies', + 'source' => 'galleries', 'fields' => array('name'), 'order' => array('created' => 'asc') )); - $result = $this->db->read($read, array('return' => 'array')); + $result = $this->_db->read($read, array('return' => 'array')); $expected = array( array('name' => 'Baz'), array('name' => 'Bar'), @@ -237,7 +242,7 @@ public function testQueryOrdering() { $this->assertEqual($expected, $result); $read->order(array('created' => 'desc')); - $result = $this->db->read($read, array('return' => 'array')); + $result = $this->_db->read($read, array('return' => 'array')); $expected = array( array('name' => 'Foo'), array('name' => 'Bar'), @@ -245,8 +250,8 @@ public function testQueryOrdering() { ); $this->assertEqual($expected, $result); - $delete = new Query(array('type' => 'delete', 'source' => 'companies')); - $this->assertTrue($this->db->delete($delete)); + $delete = new Query(array('type' => 'delete', 'source' => 'galleries')); + $this->assertTrue($this->_db->delete($delete)); } /** @@ -254,21 +259,20 @@ public function testQueryOrdering() { * support this. */ public function testDeletesWithoutAliases() { - $delete = new Query(array('type' => 'delete', 'source' => 'companies')); - $this->assertTrue($this->db->delete($delete)); + $delete = new Query(array('type' => 'delete', 'source' => 'galleries')); + $this->assertTrue($this->_db->delete($delete)); } /** * Tests that describing a table's schema returns the correct column meta-information. */ public function testDescribe() { - $result = $this->db->describe('companies'); + $result = $this->_db->describe('galleries'); $expected = array( - 'id' => array( - 'type' => 'integer', 'null' => false, - 'default' => null + 'id' => array('type' => 'integer', 'null' => false, 'default' => null), + 'name' => array( + 'type' => 'string', 'length' => 255, 'null' => true, 'default' => null ), - 'name' => array('type' => 'string', 'length' => 255, 'null' => true, 'default' => null), 'active' => array('type' => 'boolean', 'null' => true, 'default' => null), 'created' => array('type' => 'datetime', 'null' => true, 'default' => null), 'modified' => array('type' => 'datetime', 'null' => true, 'default' => null) @@ -277,9 +281,37 @@ public function testDescribe() { unset($expected['name']); unset($expected['modified']); - $result = $this->db->describe('companies', $expected)->fields(); + $result = $this->_db->describe('galleries', $expected)->fields(); $this->assertEqual($expected, $result); } + + public function testDatabaseTimezone() { + $this->assertTrue($this->_db->isConnected()); + $this->assertTrue($this->_db->timezone('UTC')); + $this->assertEqual('UTC', $this->_db->timezone()); + + $this->assertTrue($this->_db->timezone('US/Eastern')); + $this->assertEqual('US/Eastern', $this->_db->timezone()); + } + + public function testResultSet() { + Galleries::config(array('meta' => array('connection' => $this->_connection))); + + for ($i = 1; $i < 9; $i++) { + Galleries::create(array('id' => $i, 'name' => "Title {$i}"))->save(); + } + + $galleries = Galleries::all(); + + $cpt = 0; + foreach ($galleries as $gallery) { + $this->assertEqual(++$cpt, $gallery->id); + } + $this->assertIdentical(8, $cpt); + $this->assertCount(8, $galleries); + + Galleries::reset(); + } } ?> \ No newline at end of file