Test improvements

This commit is contained in:
Timothy Warren 2014-04-08 17:13:41 -04:00
parent 303eda1567
commit 74d4a00eef
13 changed files with 66 additions and 144 deletions

View File

@ -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 * @param string $table
* @return string * @return string
*/ */
public function quote_table($table) public function prefix_table($table)
{ {
// Add the prefix to the table name // Add the prefix to the table name
// before quoting it // before quoting it
@ -188,6 +187,21 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
$table = implode('.', $idents); $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 // Finally, quote the table
return $this->quote_ident($table); return $this->quote_ident($table);
} }
@ -401,7 +415,7 @@ abstract class Abstract_Driver extends \PDO implements Driver_Interface {
*/ */
public function get_columns($table) 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) 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) 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);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -42,6 +42,5 @@ abstract class Abstract_SQL implements SQL_Interface {
return $sql; return $sql;
} }
} }
// End of abstract_sql.php // End of abstract_sql.php

View File

@ -75,7 +75,7 @@ class MySQL_SQL extends Abstract_SQL {
*/ */
public function db_list() public function db_list()
{ {
return "SHOW DATABASES WHERE `Database` !='information_schema'"; return "SHOW DATABASES WHERE `Database` NOT IN ('information_schema','mysql')";
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -49,10 +49,15 @@ class MySQL_Util extends Abstract_Util {
// Get the list of tables // Get the list of tables
$tables = $this->driver_query("SHOW TABLES FROM `{$d}`", TRUE); $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); $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'];
} }
} }

View File

@ -13,8 +13,6 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
use Query\Driver;
/** /**
* Base class for TestCases * Base class for TestCases
*/ */
@ -91,9 +89,10 @@ require_once(QTEST_DIR . '/core/db_qb_test.php');
if (extension_loaded('pdo_sqlite')) if (extension_loaded('pdo_sqlite'))
{ {
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; $path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
@unlink($path);
$params = array( $params = array(
'type' => 'sqlite', 'type' => 'sqlite',
'file' => $path, 'file' => ':memory:',
'host' => 'localhost', 'host' => 'localhost',
'prefix' => 'create_', 'prefix' => 'create_',
'alias' => 'test_sqlite', 'alias' => 'test_sqlite',

View File

@ -26,6 +26,12 @@ abstract class QBTest extends Query_TestCase {
} }
} }
// --------------------------------------------------------------------------
// ! Driver-specific results
// --------------------------------------------------------------------------
abstract public function testQueryExplain();
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Get tests // ! Get tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -230,6 +236,8 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// --------------------------------------------------------------------------
public function testOrGroup() public function testOrGroup()
{ {
$query = $this->db->select('id, key as k, val') $query = $this->db->select('id, key as k, val')
@ -247,6 +255,8 @@ abstract class QBTest extends Query_TestCase {
$this->assertIsA($query, 'PDOStatement'); $this->assertIsA($query, 'PDOStatement');
} }
// --------------------------------------------------------------------------
public function testOrNotGroup() public function testOrNotGroup()
{ {
$query = $this->db->select('id, key as k, val') $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'); $qb_res = $this->db->get('test');
$sql_res = $this->db->query($sql); $sql_res = $this->db->query($sql);
$this->assertIsA($qb_res,'PDOStatement');
$this->assertIsA($sql_res, 'PDOStatement');
$this->assertEquals($qb_res, $sql_res); $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() public function testBadMethod()
{ {
try try
{ {
$res = $this->db->foo(); $this->db->foo();
} }
catch(BadMethodCallException $e) catch(BadMethodCallException $e)
{ {

View File

@ -33,6 +33,7 @@ abstract class DBTest extends Query_TestCase {
{ {
$tables = $this->db->get_tables(); $tables = $this->db->get_tables();
$this->assertTrue(is_array($tables)); $this->assertTrue(is_array($tables));
$this->assertTrue( ! empty($tables));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -40,8 +41,8 @@ abstract class DBTest extends Query_TestCase {
public function testGetSystemTables() public function testGetSystemTables()
{ {
$tables = $this->db->get_system_tables(); $tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables)); $this->assertTrue(is_array($tables));
$this->assertTrue( ! empty($tables));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -57,6 +58,7 @@ abstract class DBTest extends Query_TestCase {
{ {
$cols = $this->db->get_columns('test'); $cols = $this->db->get_columns('test');
$this->assertTrue(is_array($cols)); $this->assertTrue(is_array($cols));
$this->assertTrue( ! empty($cols));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -65,13 +67,14 @@ abstract class DBTest extends Query_TestCase {
{ {
$types = $this->db->get_types(); $types = $this->db->get_types();
$this->assertTrue(is_array($types)); $this->assertTrue(is_array($types));
$this->assertTrue( ! empty($types));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetFKs() public function testGetFKs()
{ {
$keys = $this->db->get_fks('create_test'); $keys = $this->db->get_fks('test');
$this->assertTrue(is_array($keys)); $this->assertTrue(is_array($keys));
} }

View File

@ -70,16 +70,6 @@ class FirebirdQBTest extends QBTest {
$this->assertReference($f_conn, Query('fire')); $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() public function testTypeList()
@ -146,31 +136,4 @@ class FirebirdQBTest extends QBTest {
$this->assertTrue($this->db->util->backup_structure($existing, $backup)); $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');
}
} }

View File

@ -32,6 +32,7 @@ class FirebirdTest extends DBtest {
// test the db driver directly // test the db driver directly
$this->db = new \Query\Driver\Firebird('localhost:'.$dbpath); $this->db = new \Query\Driver\Firebird('localhost:'.$dbpath);
$this->db->table_prefix = 'create_';
$this->tables = $this->db->get_tables(); $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() public function testGetSystemTables()
{ {
$only_system = TRUE; $only_system = TRUE;

View File

@ -63,6 +63,8 @@ class MySQLTest extends DBTest {
public function testCreateTable() public function testCreateTable()
{ {
$this->db->exec(file_get_contents(QTEST_DIR.'/db_files/mysql.sql'));
//Attempt to create the table //Attempt to create the table
$sql = $this->db->util->create_table('test', $sql = $this->db->util->create_table('test',
array( array(

View File

@ -37,6 +37,7 @@ class PgTest extends DBTest {
$params = $params->pgsql; $params = $params->pgsql;
$this->db = new $class("pgsql:dbname={$params->database}", $params->user, $params->pass); $this->db = new $class("pgsql:dbname={$params->database}", $params->user, $params->pass);
$this->db->table_prefix = $params->prefix;
} }
elseif (($var = getenv('CI'))) elseif (($var = getenv('CI')))
{ {

View File

@ -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() public function testTruncate()
{ {
$this->db->truncate('create_test'); $this->db->truncate('create_test');
@ -242,13 +225,6 @@ SQL;
$this->assertNull($this->db->get_schemas()); $this->assertNull($this->db->get_schemas());
} }
// --------------------------------------------------------------------------
public function testGetTypes()
{
$this->assertTrue(is_array($this->db->get_types()));
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! SQL tests // ! SQL tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -1,16 +1,17 @@
-- sample data to test PostgreSQL INFORMATION_SCHEMA -- sample data to test MySQL
-- TABLE TEST -- TABLE TEST
CREATE TABLE IF NOT EXISTS TEST1 ( DROP TABLE IF EXISTS TEST1;
CREATE TABLE TEST1 (
TEST_NAME CHAR(30) NOT NULL, TEST_NAME CHAR(30) NOT NULL,
TEST_ID INTEGER DEFAULT '0' NOT NULL, TEST_ID INTEGER DEFAULT '0' NOT NULL,
TEST_DATE TIMESTAMP 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); ALTER TABLE TEST1 ADD CONSTRAINT PK_TEST PRIMARY KEY (TEST_ID);
-- TABLE TEST2 with some CONSTRAINTs and an INDEX -- 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, ID INTEGER NOT NULL,
FIELD1 INTEGER, FIELD1 INTEGER,
FIELD2 CHAR(15), FIELD2 CHAR(15),
@ -19,10 +20,6 @@ CREATE TABLE IF NOT EXISTS TEST2 (
FIELD5 INTEGER, FIELD5 INTEGER,
ID2 INTEGER NOT NULL 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 PK_TEST2 PRIMARY KEY (ID2);
ALTER TABLE TEST2 ADD CONSTRAINT TEST2_FIELD1ID_IDX UNIQUE (ID, FIELD1); ALTER TABLE TEST2 ADD CONSTRAINT TEST2_FIELD1ID_IDX UNIQUE (ID, FIELD1);
@ -37,19 +34,19 @@ CREATE TABLE IF NOT EXISTS NUMBERS (
); );
-- TABLE NEWTABLE -- TABLE NEWTABLE
CREATE TABLE IF NOT EXISTS NEWTABLE ( DROP TABLE IF EXISTS NEWTABLE;
CREATE TABLE NEWTABLE (
ID INT DEFAULT 0 NOT NULL, ID INT DEFAULT 0 NOT NULL,
SOMENAME VARCHAR (12), SOMENAME VARCHAR (12),
SOMEDATE TIMESTAMP NOT NULL SOMEDATE TIMESTAMP NOT NULL
); );
ALTER TABLE NEWTABLE DROP CONSTRAINT IF EXISTS PKINDEX_IDX CASCADE;
ALTER TABLE NEWTABLE ADD CONSTRAINT PKINDEX_IDX PRIMARY KEY (ID); ALTER TABLE NEWTABLE ADD CONSTRAINT PKINDEX_IDX PRIMARY KEY (ID);
DROP SEQUENCE IF EXISTS NEWTABLE_SEQ CASCADE; -- DROP SEQUENCE IF EXISTS NEWTABLE_SEQ CASCADE;
CREATE SEQUENCE NEWTABLE_SEQ INCREMENT 1 START 1; -- CREATE SEQUENCE NEWTABLE_SEQ INCREMENT 1 START 1;
-- VIEW on TEST -- VIEW on TEST
CREATE OR REPLACE VIEW "testview"( CREATE OR REPLACE VIEW `testview`(
TEST_NAME, TEST_NAME,
TEST_ID, TEST_ID,
TEST_DATE TEST_DATE
@ -59,7 +56,7 @@ FROM TEST1
WHERE TEST_NAME LIKE 't%'; WHERE TEST_NAME LIKE 't%';
-- VIEW on NUMBERS -- VIEW on NUMBERS
CREATE OR REPLACE VIEW "numbersview"( CREATE OR REPLACE VIEW `numbersview`(
NUMBER, NUMBER,
TRANS_EN, TRANS_EN,
TRANS_FR TRANS_FR
@ -68,34 +65,16 @@ SELECT *
FROM NUMBERS FROM NUMBERS
WHERE NUMBER > 100; 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 -- TABLEs for testing CONSTRAINTs
CREATE TABLE IF NOT EXISTS testconstraints ( CREATE TABLE IF NOT EXISTS testconstraints (
someid integer NOT NULL, someid integer NOT NULL,
somename character varying(10) NOT NULL, somename varchar(10) NOT NULL,
CONSTRAINT testconstraints_id_pk PRIMARY KEY (someid) CONSTRAINT testconstraints_id_pk PRIMARY KEY (someid)
); );
CREATE TABLE IF NOT EXISTS testconstraints2 ( CREATE TABLE IF NOT EXISTS testconstraints2 (
ext_id integer NOT NULL, ext_id integer NOT NULL,
modified date, modified date,
uniquefield character varying(10) NOT NULL, uniquefield varchar(10) NOT NULL,
usraction integer NOT NULL, usraction integer NOT NULL,
CONSTRAINT testconstraints_id_fk FOREIGN KEY (ext_id) CONSTRAINT testconstraints_id_fk FOREIGN KEY (ext_id)
REFERENCES testconstraints (someid) MATCH SIMPLE REFERENCES testconstraints (someid) MATCH SIMPLE