From 74d4a00eef80a9a65db96924234c7f3ee93f7c7c Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Tue, 8 Apr 2014 17:13:41 -0400 Subject: [PATCH] Test improvements --- core/abstract/abstract_driver.php | 26 +++++++++--- core/abstract/abstract_sql.php | 1 - drivers/mysql/mysql_sql.php | 2 +- drivers/mysql/mysql_util.php | 9 +++- tests/bootstrap.php | 5 +-- tests/core/db_qb_test.php | 38 ++++++----------- tests/core/db_test.php | 9 ++-- tests/databases/firebird/FirebirdQBTest.php | 37 ---------------- tests/databases/firebird/FirebirdTest.php | 9 +--- tests/databases/mysql/MySQLTest.php | 2 + tests/databases/pgsql/PgSQLTest.php | 1 + tests/databases/sqlite/SqliteTest.php | 24 ----------- tests/db_files/mysql.sql | 47 ++++++--------------- 13 files changed, 66 insertions(+), 144 deletions(-) diff --git a/core/abstract/abstract_driver.php b/core/abstract/abstract_driver.php index 5989118..0aaf32e 100644 --- a/core/abstract/abstract_driver.php +++ b/core/abstract/abstract_driver.php @@ -162,12 +162,11 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { // -------------------------------------------------------------------------- /** - * Quote database table name, and set prefix - * + * Prefixes a table if it is not already prefixed * @param string $table * @return string */ - public function quote_table($table) + public function prefix_table($table) { // Add the prefix to the table name // before quoting it @@ -188,6 +187,21 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { $table = implode('.', $idents); } + return $table; + } + + // -------------------------------------------------------------------------- + + /** + * Quote database table name, and set prefix + * + * @param string $table + * @return string + */ + public function quote_table($table) + { + $table = $this->prefix_table($table); + // Finally, quote the table return $this->quote_ident($table); } @@ -401,7 +415,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_columns($table) { - return $this->driver_query($this->sql->column_list($table), FALSE); + return $this->driver_query($this->sql->column_list($this->prefix_table($table)), FALSE); } // -------------------------------------------------------------------------- @@ -414,7 +428,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_fks($table) { - return $this->driver_query($this->sql->fk_list($table), FALSE); + return $this->driver_query($this->sql->fk_list($this->prefix_table($table)), FALSE); } // -------------------------------------------------------------------------- @@ -427,7 +441,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface { */ public function get_indexes($table) { - return $this->driver_query($this->sql->index_list($table), FALSE); + return $this->driver_query($this->sql->index_list($this->prefix_table($table)), FALSE); } // -------------------------------------------------------------------------- diff --git a/core/abstract/abstract_sql.php b/core/abstract/abstract_sql.php index 62e9761..a33332a 100644 --- a/core/abstract/abstract_sql.php +++ b/core/abstract/abstract_sql.php @@ -42,6 +42,5 @@ abstract class Abstract_SQL implements SQL_Interface { return $sql; } - } // End of abstract_sql.php diff --git a/drivers/mysql/mysql_sql.php b/drivers/mysql/mysql_sql.php index 81b96c2..b856ba6 100644 --- a/drivers/mysql/mysql_sql.php +++ b/drivers/mysql/mysql_sql.php @@ -75,7 +75,7 @@ class MySQL_SQL extends Abstract_SQL { */ public function db_list() { - return "SHOW DATABASES WHERE `Database` !='information_schema'"; + return "SHOW DATABASES WHERE `Database` NOT IN ('information_schema','mysql')"; } // -------------------------------------------------------------------------- diff --git a/drivers/mysql/mysql_util.php b/drivers/mysql/mysql_util.php index 8252c20..c77f491 100644 --- a/drivers/mysql/mysql_util.php +++ b/drivers/mysql/mysql_util.php @@ -49,10 +49,15 @@ class MySQL_Util extends Abstract_Util { // Get the list of tables $tables = $this->driver_query("SHOW TABLES FROM `{$d}`", TRUE); - foreach($tables as &$table) + foreach($tables as $table) { $array = $this->driver_query("SHOW CREATE TABLE `{$d}`.`{$table}`", FALSE); - $string[] = $array[0]['Create Table']; + $row = current($array); + + if ( ! isset($row['Create Table'])) continue; + + + $string[] = $row['Create Table']; } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 056a20c..525488e 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -13,8 +13,6 @@ // -------------------------------------------------------------------------- -use Query\Driver; - /** * Base class for TestCases */ @@ -91,9 +89,10 @@ require_once(QTEST_DIR . '/core/db_qb_test.php'); if (extension_loaded('pdo_sqlite')) { $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; + @unlink($path); $params = array( 'type' => 'sqlite', - 'file' => $path, + 'file' => ':memory:', 'host' => 'localhost', 'prefix' => 'create_', 'alias' => 'test_sqlite', diff --git a/tests/core/db_qb_test.php b/tests/core/db_qb_test.php index 2aa6a71..caf3832 100644 --- a/tests/core/db_qb_test.php +++ b/tests/core/db_qb_test.php @@ -26,6 +26,12 @@ abstract class QBTest extends Query_TestCase { } } + // -------------------------------------------------------------------------- + // ! Driver-specific results + // -------------------------------------------------------------------------- + + abstract public function testQueryExplain(); + // -------------------------------------------------------------------------- // ! Get tests // -------------------------------------------------------------------------- @@ -230,6 +236,8 @@ abstract class QBTest extends Query_TestCase { $this->assertIsA($query, 'PDOStatement'); } + // -------------------------------------------------------------------------- + public function testOrGroup() { $query = $this->db->select('id, key as k, val') @@ -247,6 +255,8 @@ abstract class QBTest extends Query_TestCase { $this->assertIsA($query, 'PDOStatement'); } + // -------------------------------------------------------------------------- + public function testOrNotGroup() { $query = $this->db->select('id, key as k, val') @@ -710,6 +720,8 @@ abstract class QBTest extends Query_TestCase { $qb_res = $this->db->get('test'); $sql_res = $this->db->query($sql); + $this->assertIsA($qb_res,'PDOStatement'); + $this->assertIsA($sql_res, 'PDOStatement'); $this->assertEquals($qb_res, $sql_res); } @@ -773,35 +785,11 @@ abstract class QBTest extends Query_TestCase { // -------------------------------------------------------------------------- - /*public function testBadConnection() - { - $params = array( - 'host' => '127.0.0.1', - 'port' => '987896', - 'database' => 'test', - 'user' => NULL, - 'pass' => NULL, - 'type' => 'sqlite', - 'name' => 'foobar' - ); - - try - { - $this->db = Query($params); - } - catch(BadConnectionException $e) - { - $this->assertInstanceOf('BadConnectionException', $e); - } - }*/ - - // -------------------------------------------------------------------------- - public function testBadMethod() { try { - $res = $this->db->foo(); + $this->db->foo(); } catch(BadMethodCallException $e) { diff --git a/tests/core/db_test.php b/tests/core/db_test.php index ee8fe68..a39dfbf 100644 --- a/tests/core/db_test.php +++ b/tests/core/db_test.php @@ -33,6 +33,7 @@ abstract class DBTest extends Query_TestCase { { $tables = $this->db->get_tables(); $this->assertTrue(is_array($tables)); + $this->assertTrue( ! empty($tables)); } // -------------------------------------------------------------------------- @@ -40,10 +41,10 @@ abstract class DBTest extends Query_TestCase { public function testGetSystemTables() { $tables = $this->db->get_system_tables(); - $this->assertTrue(is_array($tables)); + $this->assertTrue( ! empty($tables)); } - + // -------------------------------------------------------------------------- public function testBackupData() @@ -57,6 +58,7 @@ abstract class DBTest extends Query_TestCase { { $cols = $this->db->get_columns('test'); $this->assertTrue(is_array($cols)); + $this->assertTrue( ! empty($cols)); } // -------------------------------------------------------------------------- @@ -65,13 +67,14 @@ abstract class DBTest extends Query_TestCase { { $types = $this->db->get_types(); $this->assertTrue(is_array($types)); + $this->assertTrue( ! empty($types)); } // -------------------------------------------------------------------------- public function testGetFKs() { - $keys = $this->db->get_fks('create_test'); + $keys = $this->db->get_fks('test'); $this->assertTrue(is_array($keys)); } diff --git a/tests/databases/firebird/FirebirdQBTest.php b/tests/databases/firebird/FirebirdQBTest.php index 22c215b..d8dd7e3 100644 --- a/tests/databases/firebird/FirebirdQBTest.php +++ b/tests/databases/firebird/FirebirdQBTest.php @@ -70,16 +70,6 @@ class FirebirdQBTest extends QBTest { $this->assertReference($f_conn, Query('fire')); } - public function testGetCompiledSelect() - { - $sql = $this->db->get_compiled_select('create_test'); - $qb_res = $this->db->get('create_test'); - $sql_res = $this->db->query($sql); - - $this->assertIsA($qb_res, '\\Query\\Driver\\Firebird_Result'); - $this->assertIsA($sql_res, '\\Query\\Driver\\Firebird_Result'); - } - // -------------------------------------------------------------------------- public function testTypeList() @@ -146,31 +136,4 @@ class FirebirdQBTest extends QBTest { $this->assertTrue($this->db->util->backup_structure($existing, $backup)); } - - // -------------------------------------------------------------------------- - - public function testInsertBatch() - { - $data = array( - array( - 'id' => 544, - 'key' => 3, - 'val' => 7, - ), - array( - 'id' => 89, - 'key' => 34, - 'val' => 57, - ), - array( - 'id' => 48, - 'key' => 403, - 'val' => 97, - ), - ); - - $query = $this->db->insert_batch('test', $data); - - $this->assertIsA($query, 'PDOStatement'); - } } \ No newline at end of file diff --git a/tests/databases/firebird/FirebirdTest.php b/tests/databases/firebird/FirebirdTest.php index 297742c..e8f1a47 100644 --- a/tests/databases/firebird/FirebirdTest.php +++ b/tests/databases/firebird/FirebirdTest.php @@ -32,6 +32,7 @@ class FirebirdTest extends DBtest { // test the db driver directly $this->db = new \Query\Driver\Firebird('localhost:'.$dbpath); + $this->db->table_prefix = 'create_'; $this->tables = $this->db->get_tables(); } @@ -77,14 +78,6 @@ class FirebirdTest extends DBtest { // -------------------------------------------------------------------------- - public function testGetTables() - { - $tables = $this->tables; - $this->assertTrue(is_array($tables)); - } - - // -------------------------------------------------------------------------- - public function testGetSystemTables() { $only_system = TRUE; diff --git a/tests/databases/mysql/MySQLTest.php b/tests/databases/mysql/MySQLTest.php index c64007e..72ad121 100644 --- a/tests/databases/mysql/MySQLTest.php +++ b/tests/databases/mysql/MySQLTest.php @@ -63,6 +63,8 @@ class MySQLTest extends DBTest { public function testCreateTable() { + $this->db->exec(file_get_contents(QTEST_DIR.'/db_files/mysql.sql')); + //Attempt to create the table $sql = $this->db->util->create_table('test', array( diff --git a/tests/databases/pgsql/PgSQLTest.php b/tests/databases/pgsql/PgSQLTest.php index e7ee7ae..90b3a68 100644 --- a/tests/databases/pgsql/PgSQLTest.php +++ b/tests/databases/pgsql/PgSQLTest.php @@ -37,6 +37,7 @@ class PgTest extends DBTest { $params = $params->pgsql; $this->db = new $class("pgsql:dbname={$params->database}", $params->user, $params->pass); + $this->db->table_prefix = $params->prefix; } elseif (($var = getenv('CI'))) { diff --git a/tests/databases/sqlite/SqliteTest.php b/tests/databases/sqlite/SqliteTest.php index 70c15ce..39b92fb 100644 --- a/tests/databases/sqlite/SqliteTest.php +++ b/tests/databases/sqlite/SqliteTest.php @@ -152,23 +152,6 @@ SQL; // -------------------------------------------------------------------------- - public function testGetTables() - { - $tables = $this->db->get_tables(); - $this->assertTrue(is_array($tables)); - } - - // -------------------------------------------------------------------------- - - public function testGetSystemTables() - { - $tables = $this->db->get_system_tables(); - - $this->assertTrue(is_array($tables)); - } - - // -------------------------------------------------------------------------- - public function testTruncate() { $this->db->truncate('create_test'); @@ -242,13 +225,6 @@ SQL; $this->assertNull($this->db->get_schemas()); } - // -------------------------------------------------------------------------- - - public function testGetTypes() - { - $this->assertTrue(is_array($this->db->get_types())); - } - // -------------------------------------------------------------------------- // ! SQL tests // -------------------------------------------------------------------------- diff --git a/tests/db_files/mysql.sql b/tests/db_files/mysql.sql index 1397afa..febbdfc 100644 --- a/tests/db_files/mysql.sql +++ b/tests/db_files/mysql.sql @@ -1,16 +1,17 @@ --- sample data to test PostgreSQL INFORMATION_SCHEMA +-- sample data to test MySQL -- TABLE TEST -CREATE TABLE IF NOT EXISTS TEST1 ( +DROP TABLE IF EXISTS TEST1; +CREATE TABLE TEST1 ( TEST_NAME CHAR(30) NOT NULL, TEST_ID INTEGER DEFAULT '0' NOT NULL, TEST_DATE TIMESTAMP NOT NULL ); -ALTER TABLE TEST1 DROP CONSTRAINT IF EXISTS PK_TEST CASCADE; ALTER TABLE TEST1 ADD CONSTRAINT PK_TEST PRIMARY KEY (TEST_ID); -- TABLE TEST2 with some CONSTRAINTs and an INDEX -CREATE TABLE IF NOT EXISTS TEST2 ( +DROP TABLE IF EXISTS TEST2; +CREATE TABLE TEST2 ( ID INTEGER NOT NULL, FIELD1 INTEGER, FIELD2 CHAR(15), @@ -19,10 +20,6 @@ CREATE TABLE IF NOT EXISTS TEST2 ( FIELD5 INTEGER, ID2 INTEGER NOT NULL ); -ALTER TABLE TEST2 DROP CONSTRAINT IF EXISTS PK_TEST2 CASCADE; -ALTER TABLE TEST2 DROP CONSTRAINT IF EXISTS TEST2_FIELD1ID_IDX CASCADE; -ALTER TABLE TEST2 DROP CONSTRAINT IF EXISTS TEST2_FIELD4_IDX CASCADE; -DROP INDEX IF EXISTS TEST2_FIELD5_IDX; ALTER TABLE TEST2 ADD CONSTRAINT PK_TEST2 PRIMARY KEY (ID2); ALTER TABLE TEST2 ADD CONSTRAINT TEST2_FIELD1ID_IDX UNIQUE (ID, FIELD1); @@ -37,19 +34,19 @@ CREATE TABLE IF NOT EXISTS NUMBERS ( ); -- TABLE NEWTABLE -CREATE TABLE IF NOT EXISTS NEWTABLE ( +DROP TABLE IF EXISTS NEWTABLE; +CREATE TABLE NEWTABLE ( ID INT DEFAULT 0 NOT NULL, SOMENAME VARCHAR (12), SOMEDATE TIMESTAMP NOT NULL ); -ALTER TABLE NEWTABLE DROP CONSTRAINT IF EXISTS PKINDEX_IDX CASCADE; ALTER TABLE NEWTABLE ADD CONSTRAINT PKINDEX_IDX PRIMARY KEY (ID); -DROP SEQUENCE IF EXISTS NEWTABLE_SEQ CASCADE; -CREATE SEQUENCE NEWTABLE_SEQ INCREMENT 1 START 1; +-- DROP SEQUENCE IF EXISTS NEWTABLE_SEQ CASCADE; +-- CREATE SEQUENCE NEWTABLE_SEQ INCREMENT 1 START 1; -- VIEW on TEST -CREATE OR REPLACE VIEW "testview"( +CREATE OR REPLACE VIEW `testview`( TEST_NAME, TEST_ID, TEST_DATE @@ -59,7 +56,7 @@ FROM TEST1 WHERE TEST_NAME LIKE 't%'; -- VIEW on NUMBERS -CREATE OR REPLACE VIEW "numbersview"( +CREATE OR REPLACE VIEW `numbersview`( NUMBER, TRANS_EN, TRANS_FR @@ -68,34 +65,16 @@ SELECT * FROM NUMBERS WHERE NUMBER > 100; --- TRIGGER on NEWTABLE -DROP FUNCTION IF EXISTS add_stamp() CASCADE; -CREATE OR REPLACE FUNCTION add_stamp() RETURNS OPAQUE AS ' - BEGIN - IF (NEW.somedate IS NULL OR NEW.somedate = 0) THEN - NEW.somedate := CURRENT_TIMESTAMP; - RETURN NEW; - END IF; - END; -' LANGUAGE 'plpgsql'; - -DROP TRIGGER IF EXISTS ADDCURRENTDATE ON newtable; - -CREATE TRIGGER ADDCURRENTDATE -BEFORE INSERT OR UPDATE -ON newtable FOR EACH ROW - EXECUTE PROCEDURE add_stamp(); - -- TABLEs for testing CONSTRAINTs CREATE TABLE IF NOT EXISTS testconstraints ( someid integer NOT NULL, - somename character varying(10) NOT NULL, + somename varchar(10) NOT NULL, CONSTRAINT testconstraints_id_pk PRIMARY KEY (someid) ); CREATE TABLE IF NOT EXISTS testconstraints2 ( ext_id integer NOT NULL, modified date, - uniquefield character varying(10) NOT NULL, + uniquefield varchar(10) NOT NULL, usraction integer NOT NULL, CONSTRAINT testconstraints_id_fk FOREIGN KEY (ext_id) REFERENCES testconstraints (someid) MATCH SIMPLE