From b1b23fa9d95b0aed24827f207a2b1f7174d4c5d9 Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Mon, 11 Jan 2010 12:28:05 -0500 Subject: [PATCH] Add unit tests for the bug I encountered when specifying primary key columns explicitly. --- framework/Db/test/Horde/Db/Adapter/MysqliTest.php | 9 +++++++ .../Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php | 14 +++++++--- .../Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php | 21 ++++++++++----- .../Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php | 30 ++++++++++++++-------- 4 files changed, 54 insertions(+), 20 deletions(-) diff --git a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php index 8f30ee320..7719aa2ea 100644 --- a/framework/Db/test/Horde/Db/Adapter/MysqliTest.php +++ b/framework/Db/test/Horde/Db/Adapter/MysqliTest.php @@ -489,6 +489,15 @@ class Horde_Db_Adapter_MysqliTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } + public function testCreateTableWithExplicitPk() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'primaryKey'); + + $pkColumn = $table['foo']; + $this->assertEquals('`foo` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', $pkColumn->toSql()); + } + public function testCreateTableForce() { $this->_createTestTable('sports'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php index d06ace9cf..d6134d36c 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/MysqlTest.php @@ -509,6 +509,15 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } + public function testCreateTableWithExplicitPk() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'primaryKey'); + + $pkColumn = $table['foo']; + $this->assertEquals('`foo` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY', $pkColumn->toSql()); + } + public function testCreateTableForce() { $this->_createTestTable('sports'); @@ -1132,7 +1141,7 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase public function testAddColumnNotNullWithoutDefault() { $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string'); + $table->column('foo', 'string'); $table->end(); $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); @@ -1140,13 +1149,12 @@ class Horde_Db_Adapter_Pdo_MysqlTest extends PHPUnit_Framework_TestCase $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->column('foo', 'string'); $table->end(); $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php index 8b3e204e4..82136c481 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/PgsqlTest.php @@ -254,13 +254,13 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase public function testTransactionRollback() { $this->_conn->beginDbTransaction(); - $sql = "INSERT INTO unit_tests (id, integer_value) VALUES (7, 999)"; - $this->_conn->insert($sql); - $this->_conn->rollbackDbTransaction(); + $sql = "INSERT INTO unit_tests (id, integer_value) VALUES (7, 999)"; + $this->_conn->insert($sql); + $this->_conn->rollbackDbTransaction(); - // make sure it inserted - $sql = "SELECT integer_value FROM unit_tests WHERE id='7'"; - $this->assertEquals(null, $this->_conn->selectValue($sql)); + // make sure it inserted + $sql = "SELECT integer_value FROM unit_tests WHERE id='7'"; + $this->assertEquals(null, $this->_conn->selectValue($sql)); } @@ -410,6 +410,15 @@ class Horde_Db_Adapter_Pdo_PgsqlTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } + public function testCreateTableWithExplicitPk() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'primaryKey'); + + $pkColumn = $table['foo']; + $this->assertEquals('"foo" serial primary key', $pkColumn->toSql()); + } + public function testCreateTableForce() { $this->_createTestTable('sports'); diff --git a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php index 322630196..bb59fe7dd 100644 --- a/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php +++ b/framework/Db/test/Horde/Db/Adapter/Pdo/SqliteTest.php @@ -406,6 +406,15 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $this->fail("Expected exception for no pk"); } + public function testCreateTableWithExplicitPk() + { + $table = $this->_conn->createTable('testings'); + $table->column('foo', 'primaryKey'); + + $pkColumn = $table['foo']; + $this->assertEquals('"foo" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL', $pkColumn->toSql()); + } + public function testCreateTableForce() { $this->_createTestTable('sports'); @@ -426,7 +435,7 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase public function testCreateTableAddsId() { $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string'); + $table->column('foo', 'string'); $table->end(); $columns = array(); @@ -440,7 +449,7 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase public function testCreateTableWithNotNullColumn() { $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string', array('null' => false)); + $table->column('foo', 'string', array('null' => false)); $table->end(); try { @@ -452,10 +461,10 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase 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->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(); @@ -472,7 +481,7 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase public function testCreateTableWithLimits() { $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string', array('limit' => 80)); + $table->column('foo', 'string', array('limit' => 80)); $table->end(); $columns = array(); @@ -486,7 +495,7 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase { try { $table = $this->_conn->createTable('binary_testings'); - $table->column('data', 'binary', array('null' => false)); + $table->column('data', 'binary', array('null' => false)); $table->end(); } catch (Exception $e) { $this->fail('Unexepected exception raised'); } @@ -1020,7 +1029,7 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase public function testAddColumnNotNullWithoutDefault() { $table = $this->_conn->createTable('testings'); - $table->column('foo', 'string'); + $table->column('foo', 'string'); $table->end(); $this->_conn->addColumn('testings', 'bar', 'string', array('null' => false, 'default' => '')); @@ -1028,13 +1037,12 @@ class Horde_Db_Adapter_Pdo_SqliteTest extends PHPUnit_Framework_TestCase $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->column('foo', 'string'); $table->end(); $this->_conn->execute("INSERT INTO testings (id, foo) VALUES ('1', 'hello')"); -- 2.11.0