No more skipped tests, and minor update to query formatting

This commit is contained in:
Timothy Warren 2014-02-21 15:02:41 -05:00
parent 81692053ed
commit 4702ccb2b3
6 changed files with 109 additions and 99 deletions

View File

@ -1266,6 +1266,8 @@ class Query_Builder implements iQuery_Builder {
{ {
$vals = array_merge($this->values, (array) $this->where_values); $vals = array_merge($this->values, (array) $this->where_values);
} }
$evals = (is_array($vals)) ? $vals : array();
$start_time = microtime(TRUE); $start_time = microtime(TRUE);
@ -1283,12 +1285,18 @@ class Query_Builder implements iQuery_Builder {
$total_time = number_format($end_time - $start_time, 5); $total_time = number_format($end_time - $start_time, 5);
// Add the interpreted query to the list of executed queries // Add the interpreted query to the list of executed queries
foreach($evals as $k => &$v)
{
$v = ( ! is_numeric($v)) ? htmlentities($this->db->quote($v), ENT_HTML401 | ENT_NOQUOTES, 'utf-8', FALSE) : $v;
}
$esql = str_replace('?', "%s", $sql); $esql = str_replace('?', "%s", $sql);
array_unshift($vals, $esql); array_unshift($vals, $esql);
array_unshift($evals, $esql);
$this->queries[] = array( $this->queries[] = array(
'time' => $total_time, 'time' => $total_time,
'sql' => call_user_func_array('sprintf', $vals), 'sql' => call_user_func_array('sprintf', $evals),
); );
$this->queries['total_time'] += $total_time; $this->queries['total_time'] += $total_time;
@ -1357,11 +1365,11 @@ class Query_Builder implements iQuery_Builder {
$params = array_fill(0, $param_count, '?'); $params = array_fill(0, $param_count, '?');
$sql = "INSERT INTO {$table} (" $sql = "INSERT INTO {$table} ("
. implode(',', $this->set_array_keys) . . implode(',', $this->set_array_keys) .
') VALUES ('.implode(',', $params).')'; ")\nVALUES (".implode(',', $params).')';
break; break;
case "update": case "update":
$sql = "UPDATE {$table} SET {$this->set_string}"; $sql = "UPDATE {$table}\nSET {$this->set_string}";
break; break;
case "delete": case "delete":

View File

@ -119,7 +119,7 @@ class SQLite_Util extends DB_Util {
if( ! empty($excluded)) if( ! empty($excluded))
{ {
$sql .= ' WHERE NOT IN("'.implode('","', $excluded).'")'; $sql .= " WHERE \"name\" NOT IN('".implode("','", $excluded)."')";
} }
$res = $this->query($sql); $res = $this->query($sql);

View File

@ -17,10 +17,10 @@
* Base class for TestCases * Base class for TestCases
*/ */
class Query_TestCase extends PHPUnit_Framework_TestCase { class Query_TestCase extends PHPUnit_Framework_TestCase {
/** /**
* Wrapper for Simpletest's assertEqual * Wrapper for Simpletest's assertEqual
* *
* @param mixed $expected * @param mixed $expected
* @param mixed $actual * @param mixed $actual
* @param string $message * @param string $message
@ -29,7 +29,7 @@ class Query_TestCase extends PHPUnit_Framework_TestCase {
{ {
$this->assertEquals($expected, $actual, $message); $this->assertEquals($expected, $actual, $message);
} }
/** /**
* Wrapper for SimpleTest's assertIsA * Wrapper for SimpleTest's assertIsA
* *
@ -41,17 +41,17 @@ class Query_TestCase extends PHPUnit_Framework_TestCase {
{ {
$this->assertTrue(is_a($object, $type), $message); $this->assertTrue(is_a($object, $type), $message);
} }
/** /**
* Implementation of SimpleTest's assertReference * Implementation of SimpleTest's assertReference
* *
* @param mixed $first * @param mixed $first
* @param mixed $second * @param mixed $second
* @param string $message * @param string $message
*/ */
public function assertReference($first, $second, $message='') public function assertReference($first, $second, $message='')
{ {
if (is_object($first)) if (is_object($first))
{ {
$res = ($first === $second); $res = ($first === $second);
} }
@ -61,7 +61,7 @@ class Query_TestCase extends PHPUnit_Framework_TestCase {
$first = uniqid("test"); $first = uniqid("test");
$is_ref = ($first === $second); $is_ref = ($first === $second);
$first = $temp; $first = $temp;
$res = $is_ref; $res = $is_ref;
} }
$this->assertTrue($res, $message); $this->assertTrue($res, $message);
} }
@ -85,6 +85,24 @@ require_once(QTEST_DIR . '/core/db_test.php');
require_once(QTEST_DIR . '/core/db_qp_test.php'); require_once(QTEST_DIR . '/core/db_qp_test.php');
require_once(QTEST_DIR . '/core/db_qb_test.php'); require_once(QTEST_DIR . '/core/db_qb_test.php');
// Preset SQLite connection, so there aren't locking issues
if (extension_loaded('pdo_sqlite'))
{
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
$params = (object) array(
'type' => 'sqlite',
'file' => $path,
'host' => 'localhost',
'prefix' => 'create_',
'alias' => 'test_sqlite',
'options' => array(
PDO::ATTR_PERSISTENT => TRUE
)
);
Query($params);
}
// If Firebird (interbase) extension does not exist, // If Firebird (interbase) extension does not exist,
// create a fake class to suppress errors from skipped tests // create a fake class to suppress errors from skipped tests
if ( ! function_exists('fbird_connect')) if ( ! function_exists('fbird_connect'))

View File

@ -56,7 +56,7 @@ abstract class DBTest extends Query_TestCase {
public function testBackupData() public function testBackupData()
{ {
$this->assertTrue(is_string($this->db->util->backup_data())); $this->assertTrue(is_string($this->db->util->backup_data(array('create_delete'))));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------

View File

@ -14,38 +14,31 @@
/** /**
* Class for testing Query Builder with SQLite * Class for testing Query Builder with SQLite
* *
* @requires extension pdo_sqlite * @requires extension pdo_sqlite
*/ */
class SQLiteQBTest extends QBTest { class SQLiteQBTest extends QBTest {
public function setUp() public function setUp()
{ {
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; // Set up in the bootstrap to mitigate
$params = (object) array( // connection locking issues
'type' => 'sqlite', $this->db = Query('test_sqlite');
'file' => $path,
'host' => 'localhost',
'prefix' => 'create_',
'options' => array(
PDO::ATTR_PERSISTENT => TRUE
)
);
$this->db = Query($params);
// echo '<hr /> SQLite Queries <hr />'; // echo '<hr /> SQLite Queries <hr />';
} }
public function testInsert() { $this->markTestSkipped();}
public function testInsertArray() { $this->markTestSkipped();}
public function testUpdate() { $this->markTestSkipped();}
public function testSetArrayUpdate() { $this->markTestSkipped();}
public function testWhereSetUpdate() { $this->markTestSkipped();}
public function testDelete() { $this->markTestSkipped();}
public function testBadNumRows() { $this->markTestSkipped();}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testQueryFunctionAlias()
{
$db = Query('test_sqlite');
$this->assertTrue($this->db === $db);
}
// --------------------------------------------------------------------------
public function testInsertBatch() public function testInsertBatch()
{ {
$insert_array = array( $insert_array = array(
@ -70,9 +63,9 @@
$this->assertNull($query); $this->assertNull($query);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testQueryExplain() public function testQueryExplain()
{ {
$query = $this->db->select('id, key as k, val') $query = $this->db->select('id, key as k, val')
@ -80,11 +73,11 @@
->where('id >', 1) ->where('id >', 1)
->where('id <', 900) ->where('id <', 900)
->get('create_test', 2, 1); ->get('create_test', 2, 1);
$res = $query->fetchAll(PDO::FETCH_ASSOC); $res = $query->fetchAll(PDO::FETCH_ASSOC);
$expected_possibilities = array(); $expected_possibilities = array();
$expected_possibilities[] = array( $expected_possibilities[] = array(
array( array(
'order' => '0', 'order' => '0',
@ -92,7 +85,7 @@
'detail' => 'TABLE create_test USING PRIMARY KEY', 'detail' => 'TABLE create_test USING PRIMARY KEY',
) )
); );
$expected_possibilities[] = array ( $expected_possibilities[] = array (
array ( array (
'selectid' => '0', 'selectid' => '0',
@ -101,7 +94,7 @@
'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~60000 rows)', 'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~60000 rows)',
), ),
); );
$expected_possibilities[] = array ( $expected_possibilities[] = array (
array ( array (
'selectid' => '0', 'selectid' => '0',
@ -110,7 +103,7 @@
'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?)', 'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?)',
), ),
); );
$expected_possibilities[] = array ( $expected_possibilities[] = array (
array ( array (
'selectid' => '0', 'selectid' => '0',
@ -119,9 +112,9 @@
'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~62500 rows)', 'detail' => 'SEARCH TABLE create_test USING INTEGER PRIMARY KEY (rowid>? AND rowid<?) (~62500 rows)',
), ),
); );
$passed = FALSE; $passed = FALSE;
// Check for a matching possibility // Check for a matching possibility
foreach($expected_possibilities as $ep) foreach($expected_possibilities as $ep)
{ {
@ -131,7 +124,7 @@
$passed = TRUE; $passed = TRUE;
} }
} }
// Well, apparently not an expected possibility // Well, apparently not an expected possibility
if ( ! $passed) if ( ! $passed)
{ {

View File

@ -22,23 +22,17 @@ class SQLiteTest extends DBTest {
public function setUp() public function setUp()
{ {
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db'; // Set up in the bootstrap to mitigate
$this->db = new SQLite($path); // connection locking issues
$this->db = Query('test_sqlite');
} }
// --------------------------------------------------------------------------
public function tearDown()
{
unset($this->db);
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! Util Method tests // ! Util Method tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testCreateTable() public function testCreateTable()
{ {
//Attempt to create the table //Attempt to create the table
$sql = $this->db->util->create_table('create_test', $sql = $this->db->util->create_table('create_test',
array( array(
@ -64,7 +58,7 @@ class SQLiteTest extends DBTest {
) )
); );
$this->db->query($sql); $this->db->query($sql);
// A table to delete // A table to delete
$sql = $this->db->util->create_table('create_delete', $sql = $this->db->util->create_table('create_delete',
array( array(
@ -83,32 +77,32 @@ class SQLiteTest extends DBTest {
$this->assertTrue(in_array('create_test', $dbs)); $this->assertTrue(in_array('create_test', $dbs));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testBackupData() public function testBackupData()
{ {
$sql = mb_trim($this->db->util->backup_data()); $sql = mb_trim($this->db->util->backup_data(array('create_join', 'create_test')));
$sql_array = explode("\n", $sql); $sql_array = explode("\n", $sql);
$expected = <<<SQL $expected = <<<SQL
INSERT INTO "create_test" ("id","key","val") VALUES (1,'boogers','Gross'); INSERT INTO "create_test" ("id","key","val") VALUES (1,'boogers','Gross');
INSERT INTO "create_test" ("id","key","val") VALUES (2,'works','also?'); INSERT INTO "create_test" ("id","key","val") VALUES (2,'works','also?');
INSERT INTO "create_test" ("id","key","val") VALUES (10,12,14); INSERT INTO "create_test" ("id","key","val") VALUES (10,12,14);
INSERT INTO "create_test" ("id","key","val") VALUES (587,1,2); INSERT INTO "create_test" ("id","key","val") VALUES (587,1,2);
INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale'''); INSERT INTO "create_test" ("id","key","val") VALUES (999,'''ring''','''sale''');
SQL; SQL;
$expected_array = explode("\n", $sql); $expected_array = explode("\n", $sql);
$this->assertEqual($expected_array, $sql_array); $this->assertEqual($expected_array, $sql_array);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testBackupStructure() public function testBackupStructure()
{ {
$sql = mb_trim($this->db->util->backup_structure()); $sql = mb_trim($this->db->util->backup_structure());
$expected = <<<SQL $expected = <<<SQL
CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT ); CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT );
CREATE TABLE "create_join" (id INTEGER PRIMARY KEY, key TEXT , val TEXT ); CREATE TABLE "create_join" (id INTEGER PRIMARY KEY, key TEXT , val TEXT );
@ -117,32 +111,37 @@ SQL;
$expected_array = explode("\n", $expected); $expected_array = explode("\n", $expected);
$result_array = explode("\n", $sql); $result_array = explode("\n", $sql);
$this->assertEqual($expected_array, $result_array); $this->assertEqual($expected_array, $result_array);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testDeleteTable() public function testDeleteTable()
{ {
$sql = $this->db->util->delete_table('create_delete'); $sql = $this->db->util->delete_table('create_delete');
$this->db->query($sql); $this->db->query($sql);
//Check //Check
$dbs = $this->db->get_tables(); $dbs = $this->db->get_tables();
$this->assertFalse(in_array('create_delete', $dbs)); $this->assertFalse(in_array('create_delete', $dbs));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! General tests // ! General tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testConnection() public function testConnection()
{ {
$this->assertIsA($this->db, 'SQLite'); $db = new SQLite(QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db');
$this->assertIsA($db, 'SQLite');
$this->assertIsA($this->db->db, 'SQLite');
unset($db);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetTables() public function testGetTables()
@ -150,7 +149,7 @@ SQL;
$tables = $this->db->get_tables(); $tables = $this->db->get_tables();
$this->assertTrue(is_array($tables)); $this->assertTrue(is_array($tables));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetSystemTables() public function testGetSystemTables()
@ -159,22 +158,14 @@ SQL;
$this->assertTrue(is_array($tables)); $this->assertTrue(is_array($tables));
} }
// --------------------------------------------------------------------------
public function testCreateTransaction()
{
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testTruncate() public function testTruncate()
{ {
$this->db->truncate('create_test'); $this->db->truncate('create_test');
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testPreparedStatements() public function testPreparedStatements()
@ -188,7 +179,7 @@ SQL;
$statement->execute(); $statement->execute();
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testPrepareExecute() public function testPrepareExecute()
@ -202,7 +193,7 @@ SQL;
)); ));
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testCommitTransaction() public function testCommitTransaction()
@ -215,11 +206,11 @@ SQL;
$res = $this->db->commit(); $res = $this->db->commit();
$this->assertTrue($res); $this->assertTrue($res);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testRollbackTransaction() public function testRollbackTransaction()
{ {
$res = $this->db->beginTransaction(); $res = $this->db->beginTransaction();
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
@ -228,43 +219,43 @@ SQL;
$res = $this->db->rollback(); $res = $this->db->rollback();
$this->assertTrue($res); $this->assertTrue($res);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetDBs() public function testGetDBs()
{ {
$this->assertNull($this->db->get_dbs()); $this->assertNull($this->db->get_dbs());
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testGetSchemas() public function testGetSchemas()
{ {
$this->assertNull($this->db->get_schemas()); $this->assertNull($this->db->get_schemas());
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
// ! SQL tests // ! SQL tests
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
public function testNullMethods() public function testNullMethods()
{ {
$sql = $this->db->sql->system_table_list(); $sql = $this->db->sql->system_table_list();
$this->assertEqual(NULL, $sql); $this->assertEqual(NULL, $sql);
$sql = $this->db->sql->trigger_list(); $sql = $this->db->sql->trigger_list();
$this->assertEqual(NULL, $sql); $this->assertEqual(NULL, $sql);
$sql = $this->db->sql->function_list(); $sql = $this->db->sql->function_list();
$this->assertEqual(NULL, $sql); $this->assertEqual(NULL, $sql);
$sql = $this->db->sql->procedure_list(); $sql = $this->db->sql->procedure_list();
$this->assertEqual(NULL, $sql); $this->assertEqual(NULL, $sql);
$sql = $this->db->sql->sequence_list(); $sql = $this->db->sql->sequence_list();
$this->assertEqual(NULL, $sql); $this->assertEqual(NULL, $sql);
} }
// @TODO Fix this // @TODO Fix this
public function testGetTypes() {} public function testGetTypes() {}
} }