First pass moving tests that are testing adapter functionality, not migration
authorChuck Hagenbuch <chuck@horde.org>
Wed, 6 Jan 2010 19:12:09 +0000 (14:12 -0500)
committerChuck Hagenbuch <chuck@horde.org>
Thu, 7 Jan 2010 03:19:54 +0000 (22:19 -0500)
functionality, to the adapter test suites.

Also adding a few checks to make sure that tables are renamed, not copied.

framework/Db/test/Horde/Db/Adapter/MysqliTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php
framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php
framework/Db/test/Horde/Db/Migration/BaseTest.php

index 6bae108..3b1979d 100644 (file)
@@ -506,13 +506,142 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(1, $this->_conn->selectValue($sql));
     }
 
+    public function testCreateTableAddsId()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[] = $col->getName();
+        }
+        sort($columns);
+        $this->assertEquals(array('foo', 'id'), $columns);
+    }
+
+    public function testCreateTableWithNotNullColumn()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('null' => false));
+        $table->end();
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testCreateTableWithDefaults()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('one',   'string',  array('default' => 'hello'));
+            $table->column('two',   'boolean', array('default' => true));
+            $table->column('three', 'boolean', array('default' => false));
+            $table->column('four',  'integer', array('default' => 1));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+
+        $this->assertEquals('hello', $columns['one']->getDefault());
+        $this->assertTrue($columns['two']->getDefault());
+        $this->assertFalse($columns['three']->getDefault());
+        $this->assertEquals(1, $columns['four']->getDefault());
+    }
+
+    public function testCreateTableWithLimits()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('limit' => 80));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+        $this->assertEquals(80, $columns['foo']->getLimit());
+    }
+
+    public function testCreateTableWithBinaryColumn()
+    {
+        try {
+            $table = $this->_conn->createTable('binary_testings');
+                $table->column('data', 'binary', array('null' => false));
+            $table->end();
+        } catch (Exception $e) { $this->fail('Unexepected exception raised'); }
+
+        $columns = $this->_conn->columns('binary_testings');
+
+        foreach ($columns as $c) {
+            if ($c->getName() == 'data') { $dataColumn = $c; }
+        }
+        $this->assertEquals('', $dataColumn->getDefault());
+    }
+
     public function testRenameTable()
     {
+        // Simple rename then select test
         $this->_createTestTable('sports');
         $this->_conn->renameTable('sports', 'my_sports');
 
         $sql = "SELECT id FROM my_sports WHERE id = 1";
         $this->assertEquals("1", $this->_conn->selectValue($sql));
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM sports WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+
+        // Rename then insert test
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $this->_conn->dropTable('octopi');
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM octopuses WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+    }
+
+    public function testRenameTableWithAnIndex()
+    {
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+        $this->_conn->addIndex('octopuses', 'url');
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $indexes = $this->_conn->indexes('octopi');
+        $this->assertEquals('url', $indexes[0]->columns[0]);
+
+        $this->_conn->dropTable('octopi');
     }
 
     public function testDropTable()
@@ -556,6 +685,64 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->fail("Column exists where it shouldn't have");
     }
 
+    public function testChangeColumn()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->addColumn('users', 'age', 'integer');
+        $oldColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $this->_conn->changeColumn('users', 'age', 'string');
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertFalse($found);
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false));
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertFalse($found);
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == false) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true));
+    }
+
     public function testChangeColumnDefault()
     {
         $this->_createTestTable('sports');
@@ -608,6 +795,11 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
 
     public function testRenameColumn()
     {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'nick_name');
+        $this->assertTrue(in_array('nick_name', $this->_columnNames('users')));
+
         $this->_createTestTable('sports');
 
         $beforeChange = $this->_getColumn('sports', 'is_college');
@@ -619,6 +811,45 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('tinyint(1)', $afterChange->getSqlType());
     }
 
+    public function testRenameColumnWithSqlReservedWord()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'group');
+        $this->assertTrue(in_array('group', $this->_columnNames('users')));
+    }
+
+    public function testAddIndex()
+    {
+        $this->_createTestUsersTable();
+
+        // Limit size of last_name and key columns to support Firebird index limitations
+        $this->_conn->addColumn('users', 'last_name',     'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'key',           'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'administrator', 'boolean');
+
+        $this->_conn->addIndex('users', 'last_name');
+        $this->_conn->removeIndex('users', 'last_name');
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name')));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name'));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', 'last_name_and_first_name');
+
+        // quoting
+        $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true));
+        $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'),
+                                        array('name' => "named_admin"));
+
+        $this->_conn->removeIndex('users', array('name' => 'named_admin'));
+    }
+
     public function testAddIndexDefault()
     {
         $this->_createTestTable('sports');
@@ -882,6 +1113,63 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         $this->assertEquals("test NOT NULL", $result);
     }
 
+    public function testAddColumnNotNullWithoutDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => ''));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testAddColumnNotNullWithDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')");
+
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default'));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testAddRemoveSingleField()
+    {
+        $this->_createTestUsersTable();
+
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->addColumn('users', 'last_name', 'string');
+        $this->assertTrue(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->removeColumn('users', 'last_name');
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+    }
+
+    public function testAddRename()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->delete('DELETE FROM users');
+
+        $this->_conn->addColumn('users', 'girlfriend', 'string');
+        $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')");
+
+        $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend');
+
+        $bob = (object)$this->_conn->selectOne('SELECT * FROM users');
+        $this->assertEquals('bobette', $bob->exgirlfriend);
+    }
+
     public function testDistinct()
     {
         $result = $this->_conn->distinct("test");
@@ -971,6 +1259,9 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
             $this->_conn->dropTable('unit_tests');
         } catch (Exception $e) {}
         try {
+            $this->_conn->dropTable('testings');
+        } catch (Exception $e) {}
+        try {
             $this->_conn->dropTable('users');
         } catch (Exception $e) {}
         try {
@@ -987,6 +1278,15 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase
         } catch (Exception $e) {}
     }
 
+    protected function _columnNames($tableName)
+    {
+        $columns = array();
+        foreach ($this->_conn->columns($tableName) as $c) {
+            $columns[] = $c->getName();
+        }
+        return $columns;
+    }
+
     /**
      * Get a column by name
      */
index 3238d7d..05a568a 100644 (file)
@@ -528,13 +528,142 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(1, $this->_conn->selectValue($sql));
     }
 
+    public function testCreateTableAddsId()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[] = $col->getName();
+        }
+        sort($columns);
+        $this->assertEquals(array('foo', 'id'), $columns);
+    }
+
+    public function testCreateTableWithNotNullColumn()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('null' => false));
+        $table->end();
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testCreateTableWithDefaults()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('one',   'string',  array('default' => 'hello'));
+            $table->column('two',   'boolean', array('default' => true));
+            $table->column('three', 'boolean', array('default' => false));
+            $table->column('four',  'integer', array('default' => 1));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+
+        $this->assertEquals('hello', $columns['one']->getDefault());
+        $this->assertTrue($columns['two']->getDefault());
+        $this->assertFalse($columns['three']->getDefault());
+        $this->assertEquals(1, $columns['four']->getDefault());
+    }
+
+    public function testCreateTableWithLimits()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('limit' => 80));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+        $this->assertEquals(80, $columns['foo']->getLimit());
+    }
+
+    public function testCreateTableWithBinaryColumn()
+    {
+        try {
+            $table = $this->_conn->createTable('binary_testings');
+                $table->column('data', 'binary', array('null' => false));
+            $table->end();
+        } catch (Exception $e) { $this->fail('Unexepected exception raised'); }
+
+        $columns = $this->_conn->columns('binary_testings');
+
+        foreach ($columns as $c) {
+            if ($c->getName() == 'data') { $dataColumn = $c; }
+        }
+        $this->assertEquals('', $dataColumn->getDefault());
+    }
+
     public function testRenameTable()
     {
+        // Simple rename then select test
         $this->_createTestTable('sports');
         $this->_conn->renameTable('sports', 'my_sports');
 
         $sql = "SELECT id FROM my_sports WHERE id = 1";
         $this->assertEquals("1", $this->_conn->selectValue($sql));
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM sports WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+
+        // Rename then insert test
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $this->_conn->dropTable('octopi');
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM octopuses WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+    }
+
+    public function testRenameTableWithAnIndex()
+    {
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+        $this->_conn->addIndex('octopuses', 'url');
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $indexes = $this->_conn->indexes('octopi');
+        $this->assertEquals('url', $indexes[0]->columns[0]);
+
+        $this->_conn->dropTable('octopi');
     }
 
     public function testDropTable()
@@ -578,6 +707,64 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->fail("Column exists where it shouldn't have");
     }
 
+    public function testChangeColumn()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->addColumn('users', 'age', 'integer');
+        $oldColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $this->_conn->changeColumn('users', 'age', 'string');
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertFalse($found);
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false));
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertFalse($found);
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == false) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true));
+    }
+
     public function testChangeColumnDefault()
     {
         $this->_createTestTable('sports');
@@ -630,6 +817,11 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
 
     public function testRenameColumn()
     {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'nick_name');
+        $this->assertTrue(in_array('nick_name', $this->_columnNames('users')));
+
         $this->_createTestTable('sports');
 
         $beforeChange = $this->_getColumn('sports', 'is_college');
@@ -641,6 +833,45 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('tinyint(1)', $afterChange->getSqlType());
     }
 
+    public function testRenameColumnWithSqlReservedWord()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'group');
+        $this->assertTrue(in_array('group', $this->_columnNames('users')));
+    }
+
+    public function testAddIndex()
+    {
+        $this->_createTestUsersTable();
+
+        // Limit size of last_name and key columns to support Firebird index limitations
+        $this->_conn->addColumn('users', 'last_name',     'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'key',           'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'administrator', 'boolean');
+
+        $this->_conn->addIndex('users', 'last_name');
+        $this->_conn->removeIndex('users', 'last_name');
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name')));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name'));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', 'last_name_and_first_name');
+
+        // quoting
+        $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true));
+        $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'),
+                                        array('name' => "named_admin"));
+
+        $this->_conn->removeIndex('users', array('name' => 'named_admin'));
+    }
+
     public function testAddIndexDefault()
     {
         $this->_createTestTable('sports');
@@ -904,6 +1135,64 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals("test NOT NULL", $result);
     }
 
+    public function testAddColumnNotNullWithoutDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => ''));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+
+    }
+
+    public function testAddColumnNotNullWithDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')");
+
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default'));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testAddRemoveSingleField()
+    {
+        $this->_createTestUsersTable();
+
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->addColumn('users', 'last_name', 'string');
+        $this->assertTrue(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->removeColumn('users', 'last_name');
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+    }
+
+    public function testAddRename()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->delete('DELETE FROM users');
+
+        $this->_conn->addColumn('users', 'girlfriend', 'string');
+        $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')");
+
+        $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend');
+
+        $bob = (object)$this->_conn->selectOne('SELECT * FROM users');
+        $this->assertEquals('bobette', $bob->exgirlfriend);
+    }
+
     public function testDistinct()
     {
         $result = $this->_conn->distinct("test");
@@ -993,6 +1282,9 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
             $this->_conn->dropTable('unit_tests');
         } catch (Exception $e) {}
         try {
+            $this->_conn->dropTable('testings');
+        } catch (Exception $e) {}
+        try {
             $this->_conn->dropTable('users');
         } catch (Exception $e) {}
         try {
@@ -1009,6 +1301,15 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase
         } catch (Exception $e) {}
     }
 
+    protected function _columnNames($tableName)
+    {
+        $columns = array();
+        foreach ($this->_conn->columns($tableName) as $c) {
+            $columns[] = $c->getName();
+        }
+        return $columns;
+    }
+
     /**
      * Get a column by name
      */
index a5023e8..0b2e295 100644 (file)
@@ -430,13 +430,142 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(1, $this->_conn->selectValue($sql));
     }
 
+    public function testCreateTableAddsId()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[] = $col->getName();
+        }
+        sort($columns);
+        $this->assertEquals(array('foo', 'id'), $columns);
+    }
+
+    public function testCreateTableWithNotNullColumn()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('null' => false));
+        $table->end();
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testCreateTableWithDefaults()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('one',   'string',  array('default' => 'hello'));
+            $table->column('two',   'boolean', array('default' => true));
+            $table->column('three', 'boolean', array('default' => false));
+            $table->column('four',  'integer', array('default' => 1));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+
+        $this->assertEquals('hello', $columns['one']->getDefault());
+        $this->assertTrue($columns['two']->getDefault());
+        $this->assertFalse($columns['three']->getDefault());
+        $this->assertEquals(1, $columns['four']->getDefault());
+    }
+
+    public function testCreateTableWithLimits()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('limit' => 80));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+        $this->assertEquals(80, $columns['foo']->getLimit());
+    }
+
+    public function testCreateTableWithBinaryColumn()
+    {
+        try {
+            $table = $this->_conn->createTable('binary_testings');
+                $table->column('data', 'binary', array('null' => false));
+            $table->end();
+        } catch (Exception $e) { $this->fail('Unexepected exception raised'); }
+
+        $columns = $this->_conn->columns('binary_testings');
+
+        foreach ($columns as $c) {
+            if ($c->getName() == 'data') { $dataColumn = $c; }
+        }
+        $this->assertEquals('', $dataColumn->getDefault());
+    }
+
     public function testRenameTable()
     {
+        // Simple rename then select test
         $this->_createTestTable('sports');
         $this->_conn->renameTable('sports', 'my_sports');
 
         $sql = "SELECT id FROM my_sports WHERE id = 1";
         $this->assertEquals("1", $this->_conn->selectValue($sql));
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM sports WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+
+        // Rename then insert test
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $this->_conn->dropTable('octopi');
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM octopuses WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+    }
+
+    public function testRenameTableWithAnIndex()
+    {
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+        $this->_conn->addIndex('octopuses', 'url');
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $indexes = $this->_conn->indexes('octopi');
+        $this->assertEquals('url', $indexes[0]->columns[0]);
+
+        $this->_conn->dropTable('octopi');
     }
 
     public function testDropTable()
@@ -480,6 +609,64 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         $this->fail("Column exists where it shouldn't have");
     }
 
+    public function testChangeColumn()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->addColumn('users', 'age', 'integer');
+        $oldColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $this->_conn->changeColumn('users', 'age', 'string');
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertFalse($found);
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false));
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertFalse($found);
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == false) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true));
+    }
+
     public function testChangeColumnDefault()
     {
         $this->_createTestTable('sports');
@@ -532,6 +719,11 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
 
     public function testRenameColumn()
     {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'nick_name');
+        $this->assertTrue(in_array('nick_name', $this->_columnNames('users')));
+
         $this->_createTestTable('sports');
 
         $beforeChange = $this->_getColumn('sports', 'is_college');
@@ -543,6 +735,45 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('boolean', $afterChange->getSqlType());
     }
 
+    public function testRenameColumnWithSqlReservedWord()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'group');
+        $this->assertTrue(in_array('group', $this->_columnNames('users')));
+    }
+
+    public function testAddIndex()
+    {
+        $this->_createTestUsersTable();
+
+        // Limit size of last_name and key columns to support Firebird index limitations
+        $this->_conn->addColumn('users', 'last_name',     'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'key',           'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'administrator', 'boolean');
+
+        $this->_conn->addIndex('users', 'last_name');
+        $this->_conn->removeIndex('users', 'last_name');
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name')));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name'));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', 'last_name_and_first_name');
+
+        // quoting
+        $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true));
+        $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'),
+                                        array('name' => "named_admin"));
+
+        $this->_conn->removeIndex('users', array('name' => 'named_admin'));
+    }
+
     public function testAddIndexDefault()
     {
         $this->_createTestTable('sports');
@@ -774,6 +1005,64 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         $this->assertEquals("test NOT NULL", $result);
     }
 
+    public function testAddColumnNotNullWithoutDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => ''));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+
+    }
+
+    public function testAddColumnNotNullWithDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')");
+
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default'));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testAddRemoveSingleField()
+    {
+        $this->_createTestUsersTable();
+
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->addColumn('users', 'last_name', 'string');
+        $this->assertTrue(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->removeColumn('users', 'last_name');
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+    }
+
+    public function testAddRename()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->delete('DELETE FROM users');
+
+        $this->_conn->addColumn('users', 'girlfriend', 'string');
+        $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')");
+
+        $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend');
+
+        $bob = (object)$this->_conn->selectOne('SELECT * FROM users');
+        $this->assertEquals('bobette', $bob->exgirlfriend);
+    }
+
     public function testDistinct()
     {
         $result = $this->_conn->distinct("test");
@@ -863,6 +1152,9 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
             $this->_conn->dropTable('unit_tests');
         } catch (Exception $e) {}
         try {
+            $this->_conn->dropTable('testings');
+        } catch (Exception $e) {}
+        try {
             $this->_conn->dropTable('users');
         } catch (Exception $e) {}
         try {
@@ -879,6 +1171,15 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
         } catch (Exception $e) {}
     }
 
+    protected function _columnNames($tableName)
+    {
+        $columns = array();
+        foreach ($this->_conn->columns($tableName) as $c) {
+            $columns[] = $c->getName();
+        }
+        return $columns;
+    }
+
     /**
      * Get a column by name
      */
@@ -903,5 +1204,4 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase
             if ($columns == $indexes) return $index;
         }
     }
-
 }
index 4df2c77..3226301 100644 (file)
@@ -423,13 +423,138 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $this->assertEquals(1, $this->_conn->selectValue($sql));
     }
 
+    public function testCreateTableAddsId()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[] = $col->getName();
+        }
+        sort($columns);
+        $this->assertEquals(array('foo', 'id'), $columns);
+    }
+
+    public function testCreateTableWithNotNullColumn()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('null' => false));
+        $table->end();
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testCreateTableWithDefaults()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('one',   'string',  array('default' => 'hello'));
+            $table->column('two',   'boolean', array('default' => true));
+            $table->column('three', 'boolean', array('default' => false));
+            $table->column('four',  'integer', array('default' => 1));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+
+        $this->assertEquals('hello', $columns['one']->getDefault());
+        $this->assertTrue($columns['two']->getDefault());
+        $this->assertFalse($columns['three']->getDefault());
+        $this->assertEquals(1, $columns['four']->getDefault());
+    }
+
+    public function testCreateTableWithLimits()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string', array('limit' => 80));
+        $table->end();
+
+        $columns = array();
+        foreach ($this->_conn->columns('testings') as $col) {
+            $columns[$col->getName()] = $col;
+        }
+        $this->assertEquals(80, $columns['foo']->getLimit());
+    }
+
+    public function testCreateTableWithBinaryColumn()
+    {
+        try {
+            $table = $this->_conn->createTable('binary_testings');
+                $table->column('data', 'binary', array('null' => false));
+            $table->end();
+        } catch (Exception $e) { $this->fail('Unexepected exception raised'); }
+
+        $columns = $this->_conn->columns('binary_testings');
+
+        foreach ($columns as $c) {
+            if ($c->getName() == 'data') { $dataColumn = $c; }
+        }
+        $this->assertEquals('', $dataColumn->getDefault());
+    }
+
     public function testRenameTable()
     {
+        // Simple rename then select test
         $this->_createTestTable('sports');
         $this->_conn->renameTable('sports', 'my_sports');
 
         $sql = "SELECT id FROM my_sports WHERE id = 1";
         $this->assertEquals("1", $this->_conn->selectValue($sql));
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM sports WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+
+        // Rename then insert test
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        // Make sure the old table name isn't still there
+        try {
+            $sql = "SELECT id FROM octopuses WHERE id = 1";
+            $this->_conn->execute($sql);
+        } catch (Exception $e) {
+            return;
+        }
+        $this->fail("Table exists where it shouldn't have");
+    }
+
+    public function testRenameTableWithAnIndex()
+    {
+        $table = $this->_conn->createTable('octopuses');
+            $table->column('url', 'string');
+        $table->end();
+        $this->_conn->addIndex('octopuses', 'url');
+        $this->_conn->renameTable('octopuses', 'octopi');
+
+        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
+        $this->_conn->execute($sql);
+
+        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
+                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
+
+        $indexes = $this->_conn->indexes('octopi');
+        $this->assertEquals('url', $indexes[0]->columns[0]);
     }
 
     public function testDropTable()
@@ -473,6 +598,64 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $this->fail("Column exists where it shouldn't have");
     }
 
+    public function testChangeColumn()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->addColumn('users', 'age', 'integer');
+        $oldColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $this->_conn->changeColumn('users', 'age', 'string');
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
+        }
+        $this->assertFalse($found);
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        $found = false;
+        foreach ($oldColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false));
+
+        $newColumns = $this->_conn->columns('users', "User Columns");
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == true) { $found = true; }
+        }
+        $this->assertFalse($found);
+
+        $found = false;
+        foreach ($newColumns as $c) {
+            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
+                $c->getDefault() == false) { $found = true; }
+        }
+        $this->assertTrue($found);
+
+        // changeColumn() throws exception on error
+        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true));
+    }
+
     public function testChangeColumnDefault()
     {
         $this->_createTestTable('sports');
@@ -525,6 +708,11 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
 
     public function testRenameColumn()
     {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'nick_name');
+        $this->assertTrue(in_array('nick_name', $this->_columnNames('users')));
+
         $this->_createTestTable('sports');
 
         $beforeChange = $this->_getColumn('sports', 'is_college');
@@ -536,6 +724,45 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('boolean', $afterChange->getSqlType());
     }
 
+    public function testRenameColumnWithSqlReservedWord()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->renameColumn('users', 'first_name', 'group');
+        $this->assertTrue(in_array('group', $this->_columnNames('users')));
+    }
+
+    public function testAddIndex()
+    {
+        $this->_createTestUsersTable();
+
+        // Limit size of last_name and key columns to support Firebird index limitations
+        $this->_conn->addColumn('users', 'last_name',     'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'key',           'string',  array('limit' => 100));
+        $this->_conn->addColumn('users', 'administrator', 'boolean');
+
+        $this->_conn->addIndex('users', 'last_name');
+        $this->_conn->removeIndex('users', 'last_name');
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name')));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name'));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name'));
+        $this->_conn->removeIndex('users', 'last_name_and_first_name');
+
+        // quoting
+        $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true));
+        $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true));
+
+        $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'),
+                                        array('name' => "named_admin"));
+
+        $this->_conn->removeIndex('users', array('name' => 'named_admin'));
+    }
+
     public function testAddIndexDefault()
     {
         $this->_createTestTable('sports');
@@ -790,6 +1017,64 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $this->assertEquals('test NOT NULL', $result);
     }
 
+    public function testAddColumnNotNullWithoutDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => ''));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+
+    }
+
+    public function testAddColumnNotNullWithDefault()
+    {
+        $table = $this->_conn->createTable('testings');
+            $table->column('foo', 'string');
+        $table->end();
+
+        $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')");
+
+        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default'));
+
+        try {
+            $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)");
+        } catch (Exception $e) { return; }
+        $this->fail('Expected exception wasn\'t raised');
+    }
+
+    public function testAddRemoveSingleField()
+    {
+        $this->_createTestUsersTable();
+
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->addColumn('users', 'last_name', 'string');
+        $this->assertTrue(in_array('last_name', $this->_columnNames('users')));
+
+        $this->_conn->removeColumn('users', 'last_name');
+        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
+    }
+
+    public function testAddRename()
+    {
+        $this->_createTestUsersTable();
+
+        $this->_conn->delete('DELETE FROM users');
+
+        $this->_conn->addColumn('users', 'girlfriend', 'string');
+        $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')");
+
+        $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend');
+
+        $bob = (object)$this->_conn->selectOne('SELECT * FROM users');
+        $this->assertEquals('bobette', $bob->exgirlfriend);
+    }
+
     public function testDistinct()
     {
         $result = $this->_conn->distinct('test');
@@ -870,6 +1155,15 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
         $table->end();
     }
 
+    protected function _columnNames($tableName)
+    {
+        $columns = array();
+        foreach ($this->_conn->columns($tableName) as $c) {
+            $columns[] = $c->getName();
+        }
+        return $columns;
+    }
+
     /**
      * Get a column by name
      */
@@ -894,5 +1188,4 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase
             if ($columns == $indexes) return $index;
         }
     }
-
 }
index 35dd8ef..5ba207a 100644 (file)
@@ -28,328 +28,12 @@ require_once dirname(dirname(__FILE__)) . '/fixtures/migrations_with_decimal/1_g
  */
 class Horde_Db_Migration_BaseTest extends PHPUnit_Framework_TestCase
 {
-    public function setUp()
-    {
-        $this->_conn = Horde_Db_Adapter::factory(array(
-            'adapter' => 'pdo_sqlite',
-            'dbname' => ':memory:',
-        ));
-
-        /*
-CREATE TABLE users (
-  id         int(11) auto_increment,
-  company_id int(11),
-  name       varchar(255) default '',
-  first_name varchar(40) default '',
-  approved   tinyint(1) default '1',
-  type       varchar(255) default '',
-  created_at datetime default '0000-00-00 00:00:00',
-  created_on date default '0000-00-00',
-  updated_at datetime default '0000-00-00 00:00:00',
-  updated_on date default '0000-00-00',
-  PRIMARY KEY (id)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-        */
-        $table = $this->_conn->createTable('users');
-          $table->column('company_id',  'integer',  array('limit' => 11));
-          $table->column('name',        'string',   array('limit' => 255, 'default' => ''));
-          $table->column('first_name',  'string',   array('limit' => 40, 'default' => ''));
-          $table->column('approved',    'boolean',  array('default' => true));
-          $table->column('type',        'string',   array('limit' => 255, 'default' => ''));
-          $table->column('created_at',  'datetime', array('default' => '0000-00-00 00:00:00'));
-          $table->column('created_on',  'date',     array('default' => '0000-00-00'));
-          $table->column('updated_at',  'datetime', array('default' => '0000-00-00 00:00:00'));
-          $table->column('updated_on',  'date',     array('default' => '0000-00-00'));
-        $table->end();
-        /*
-mike:
-  id:         1
-  company_id: 1
-  name:       Mike Naberezny
-  first_name: Mike
-  approved:   1
-  type:       User
-  created_at: '2008-01-01 12:20:00'
-  created_on: '2008-01-01'
-  updated_at: '2008-01-01 12:20:00'
-  updated_on: '2008-01-01'
-
-derek:
-  id:         2
-  company_id: 1
-  name:       Derek DeVries
-  first_name: Derek
-  approved:   1
-  type:       User
-  created_at: '<?php echo date("Y-m-d H:i:s", strtotime("-1 day")) ?>'
-  created_on: '<?php echo date("Y-m-d",       strtotime("-1 day")) ?>'
-  updated_at: '<?php echo date("Y-m-d H:i:s", strtotime("-1 day")) ?>'
-  updated_on: '<?php echo date("Y-m-d",       strtotime("-1 day")) ?>'
-
-client:
-  id:         3
-  company_id: 1
-  name:       Extreme
-  first_name: Engineer
-  approved:   1
-  type:       Client
-  created_at: '2008-01-01 12:20:00'
-  created_on: '2008-01-01'
-  updated_at: '2008-01-01 12:20:00'
-  updated_on: '2008-01-01'
-        */
-    }
-
-    public function testAddIndex()
-    {
-        // Limit size of last_name and key columns to support Firebird index limitations
-        $this->_conn->addColumn('users', 'last_name',     'string',  array('limit' => 100));
-        $this->_conn->addColumn('users', 'key',           'string',  array('limit' => 100));
-        $this->_conn->addColumn('users', 'administrator', 'boolean');
-
-        $this->_conn->addIndex('users', 'last_name');
-        $this->_conn->removeIndex('users', 'last_name');
-
-        $this->_conn->addIndex('users', array('last_name', 'first_name'));
-        $this->_conn->removeIndex('users', array('column' => array('last_name', 'first_name')));
-
-        $this->_conn->addIndex('users', array('last_name', 'first_name'));
-        $this->_conn->removeIndex('users', array('name' => 'index_users_on_last_name_and_first_name'));
-
-        $this->_conn->addIndex('users', array('last_name', 'first_name'));
-        $this->_conn->removeIndex('users', 'last_name_and_first_name');
-
-        // quoting
-
-        $this->_conn->addIndex('users', array('key'), array('name' => 'key_idx', 'unique' => true));
-        $this->_conn->removeIndex('users', array('name' => 'key_idx', 'unique' => true));
-
-        $this->_conn->addIndex('users', array('last_name', 'first_name', 'administrator'),
-                                        array('name' => "named_admin"));
-
-        $this->_conn->removeIndex('users', array('name' => 'named_admin'));
-    }
-
-    public function testCreateTableAddsId()
-    {
-        $table = $this->_conn->createTable('testings');
-            $table->column('foo', 'string');
-        $table->end();
-
-        $columns = array();
-        foreach ($this->_conn->columns('testings') as $col) {
-            $columns[] = $col->getName();
-        }
-        sort($columns);
-        $this->assertEquals(array('foo', 'id'), $columns);
-    }
-
-    public function testCreateTableWithNotNullColumn()
-    {
-        $table = $this->_conn->createTable('testings');
-            $table->column('foo', 'string', array('null' => false));
-        $table->end();
-
-        try {
-            $this->_conn->execute("INSERT INTO testings (foo) VALUES (NULL)");
-        } catch (Exception $e) { return; }
-        $this->fail('Expected exception wasn\'t raised');
-    }
-
-    /**
-     * @see  Horde_Db_Adapter_Abstract_ColumnTest
-     */
-    public function testCreateTableWithDefaults()
-    {
-        $table = $this->_conn->createTable('testings');
-            $table->column('one',   'string',  array('default' => 'hello'));
-            $table->column('two',   'boolean', array('default' => true));
-            $table->column('three', 'boolean', array('default' => false));
-            $table->column('four',  'integer', array('default' => 1));
-        $table->end();
-
-        $columns = array();
-        foreach ($this->_conn->columns('testings') as $col) {
-            $columns[$col->getName()] = $col;
-        }
-
-        $this->assertEquals('hello', $columns['one']->getDefault());
-        $this->assertTrue($columns['two']->getDefault());
-        $this->assertFalse($columns['three']->getDefault());
-        $this->assertEquals(1, $columns['four']->getDefault());
-    }
-
-    public function testCreateTableWithLimits()
-    {
-        $table = $this->_conn->createTable('testings');
-            $table->column('foo', 'string', array('limit' => 80));
-        $table->end();
-
-        $columns = array();
-        foreach ($this->_conn->columns('testings') as $col) {
-            $columns[$col->getName()] = $col;
-        }
-        $this->assertEquals(80, $columns['foo']->getLimit());
-    }
-
-    public function testAddColumnNotNullWithoutDefault()
-    {
-        $table = $this->_conn->createTable('testings');
-            $table->column('foo', 'string');
-        $table->end();
-        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => ''));
-
-        try {
-            $this->_conn->execute("INSERT INTO testings (foo, bar) VALUES ('hello', NULL)");
-        } catch (Exception $e) { return; }
-        $this->fail('Expected exception wasn\'t raised');
-
-    }
-
-    public function testAddColumnNotNullWithDefault()
-    {
-        $table = $this->_conn->createTable('testings');
-            $table->column('foo', 'string');
-        $table->end();
-
-        $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')");
-
-        $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => 'default'));
-
-        try {
-            $this->_conn->execute("INSERT INTO testings (id, foo, bar) VALUES (2, 'hello', NULL)");
-        } catch (Exception $e) { return; }
-        $this->fail('Expected exception wasn\'t raised');
-    }
-
-    public function testAddRemoveSingleField()
-    {
-        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
-
-        $this->_conn->addColumn('users', 'last_name', 'string');
-        $this->assertTrue(in_array('last_name', $this->_columnNames('users')));
-
-        $this->_conn->removeColumn('users', 'last_name');
-        $this->assertFalse(in_array('last_name', $this->_columnNames('users')));
-    }
-
-    public function testAddRename()
-    {
-        $this->_conn->delete('DELETE FROM users');
-
-        $this->_conn->addColumn('users', 'girlfriend', 'string');
-        $this->_conn->insert("INSERT INTO users (girlfriend) VALUES ('bobette')");
-
-        $this->_conn->renameColumn('users', 'girlfriend', 'exgirlfriend');
-
-        $bob = (object)$this->_conn->selectOne('SELECT * FROM users');
-        $this->assertEquals('bobette', $bob->exgirlfriend);
-    }
-
-    public function testRenameColumn()
-    {
-        $this->_conn->renameColumn('users', 'first_name', 'nick_name');
-        $this->assertTrue(in_array('nick_name', $this->_columnNames('users')));
-    }
-
-    public function testRenameColumnWithSqlReservedWord()
-    {
-        $this->_conn->renameColumn('users', 'first_name', 'group');
-        $this->assertTrue(in_array('group', $this->_columnNames('users')));
-    }
-
-    public function testRenameTable()
-    {
-        $table = $this->_conn->createTable('octopuses');
-            $table->column('url', 'string');
-        $table->end();
-
-        $this->_conn->renameTable('octopuses', 'octopi');
-
-        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
-        $this->_conn->execute($sql);
-
-        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
-                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
-    }
-
-    public function testRenameTableWithAnIndex()
-    {
-        $table = $this->_conn->createTable('octopuses');
-            $table->column('url', 'string');
-        $table->end();
-        $this->_conn->addIndex('octopuses', 'url');
-        $this->_conn->renameTable('octopuses', 'octopi');
-
-        $sql = "INSERT INTO octopi (id, url) VALUES (1, 'http://www.foreverflying.com/octopus-black7.jpg')";
-        $this->_conn->execute($sql);
-
-        $this->assertEquals('http://www.foreverflying.com/octopus-black7.jpg',
-                $this->_conn->selectValue("SELECT url FROM octopi WHERE id=1"));
-
-        $indexes = $this->_conn->indexes('octopi');
-        $this->assertEquals('url', $indexes[0]->columns[0]);
-    }
-
-    public function testChangeColumn()
-    {
-        $this->_conn->addColumn('users', 'age', 'integer');
-        $oldColumns = $this->_conn->columns('users', "User Columns");
-
-        $found = false;
-        foreach ($oldColumns as $c) {
-            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
-        }
-        $this->assertTrue($found);
-
-        $this->_conn->changeColumn('users', 'age', 'string');
-
-        $newColumns = $this->_conn->columns('users', "User Columns");
-
-        $found = false;
-        foreach ($newColumns as $c) {
-            if ($c->getName() == 'age' && $c->getType() == 'integer') { $found = true; }
-        }
-        $this->assertFalse($found);
-        $found = false;
-        foreach ($newColumns as $c) {
-            if ($c->getName() == 'age' && $c->getType() == 'string') { $found = true; }
-        }
-        $this->assertTrue($found);
-
-        $found = false;
-        foreach ($oldColumns as $c) {
-            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
-                $c->getDefault() == true) { $found = true; }
-        }
-        $this->assertTrue($found);
-
-        // changeColumn() throws exception on error
-        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => false));
-
-        $newColumns = $this->_conn->columns('users', "User Columns");
-
-        $found = false;
-        foreach ($newColumns as $c) {
-            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
-                $c->getDefault() == true) { $found = true; }
-        }
-        $this->assertFalse($found);
-
-        $found = false;
-        foreach ($newColumns as $c) {
-            if ($c->getName() == 'approved' && $c->getType() == 'boolean' &&
-                $c->getDefault() == false) { $found = true; }
-        }
-        $this->assertTrue($found);
-
-        // changeColumn() throws exception on error
-        $this->_conn->changeColumn('users', 'approved', 'boolean', array('default' => true));
-    }
-
+    /** These tests need support for pulling default properties for an object from a table definition **/
+    /*
     public function testChangeColumnWithNilDefault()
     {
-        $this->markTestSkipped();
+        $this->_createTestUsersTable();
+
         $this->_conn->addColumn('users', 'contributor', 'boolean', array('default' => true));
         $user = new User;
         $this->assertTrue($user->contributor);
@@ -363,7 +47,8 @@ client:
 
     public function testChangeColumnWithNewDefault()
     {
-        $this->markTestSkipped();
+        $this->_createTestUsersTable();
+
         $this->_conn->addColumn('users', 'administrator', 'boolean', array('default' => true));
         $user = new User;
         $this->assertTrue($user->administrator);
@@ -377,7 +62,8 @@ client:
 
     public function testChangeColumnDefault()
     {
-        $this->markTestSkipped();
+        $this->_createTestUsersTable();
+
         $this->_conn->changeColumnDefault('users', 'first_name', 'Tester');
 
         $user = new User;
@@ -386,12 +72,22 @@ client:
 
     public function testChangeColumnDefaultToNull()
     {
-        $this->markTestSkipped();
+        $this->_createTestUsersTable();
+
         $this->_conn->changeColumnDefault('users', 'first_name', null);
 
         $user = new User;
         $this->assertNull($user->first_name);
     }
+    */
+
+    public function setUp()
+    {
+        $this->_conn = Horde_Db_Adapter::factory(array(
+            'adapter' => 'pdo_sqlite',
+            'dbname' => ':memory:',
+        ));
+    }
 
     public function testAddTable()
     {
@@ -444,31 +140,4 @@ client:
         } catch (Exception $e) {}
         $this->assertType('Horde_Db_Exception', $e);
     }
-
-    public function testCreateTableWithBinaryColumn()
-    {
-        try {
-            $table = $this->_conn->createTable('binary_testings');
-                $table->column('data', 'binary', array('null' => false));
-            $table->end();
-        } catch (Exception $e) { $this->fail('Unexepected exception raised'); }
-
-        $columns = $this->_conn->columns('binary_testings');
-
-        foreach ($columns as $c) {
-            if ($c->getName() == 'data') { $dataColumn = $c; }
-        }
-        $this->assertEquals('', $dataColumn->getDefault());
-    }
-
-
-    protected function _columnNames($tableName)
-    {
-        $columns = array();
-        foreach ($this->_conn->columns($tableName) as $c) {
-            $columns[] = $c->getName();
-        }
-        return $columns;
-    }
-
 }