From 0654b94793a459f82edb5c9a56761742002188dc Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Fri, 9 Mar 2012 13:42:02 -0500 Subject: [PATCH] Move QB tests into their own test cases, remove create/delete table tests --- sys/db/query_builder.php | 28 ++++++- tests/databases/firebird.php | 134 +++++++++++++++++++++------------- tests/databases/sqlite.php | 108 ++++++++++++++++----------- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 4 files changed, 173 insertions(+), 97 deletions(-) diff --git a/sys/db/query_builder.php b/sys/db/query_builder.php index f8435f6..ad45499 100644 --- a/sys/db/query_builder.php +++ b/sys/db/query_builder.php @@ -99,16 +99,23 @@ class Query_Builder { $sql = $this->sql->limit($sql, $limit, $offset); } - echo $sql."
"; + // echo $sql."
"; // Do prepared statements for anything involving a "where" clause if ( ! empty($this->where_string)) { - return $this->db->prepare_execute($sql, array_values($this->where_array)); + $result = $this->db->prepare_execute($sql, array_values($this->where_array)); + } + else + { + // Otherwise, a simple query will do. + $result = $this->db->query($sql); } - // Otherwise, a simple query will do. - return $this->db->query($sql); + // Reset for next query + $this->_reset(); + + return $result; } // -------------------------------------------------------------------------- @@ -248,6 +255,19 @@ class Query_Builder { // -------------------------------------------------------------------------- + /** + * Clear out the class variables, so the next query can be run + */ + private function _reset() + { + unset($this->table); + unset($this->where_array); + unset($this->where_string); + unset($this->select_string); + } + + // -------------------------------------------------------------------------- + /** * String together the sql statements for sending to the db * diff --git a/tests/databases/firebird.php b/tests/databases/firebird.php index 1fcfe4f..090a0c8 100644 --- a/tests/databases/firebird.php +++ b/tests/databases/firebird.php @@ -70,7 +70,9 @@ class FirebirdTest extends UnitTestCase { { $only_system = TRUE; - foreach($this->tables as $t) + $tables = $this->db->get_system_tables(); + + foreach($tables as $t) { if(stripos($t, 'rdb$') !== 0 && stripos($t, 'mon$') !== 0) { @@ -88,7 +90,7 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue($res); } - function TestCreateTable() + /*function TestCreateTable() { //Attempt to create the table $sql = $this->db->sql->create_table('create_test', array( @@ -104,16 +106,16 @@ class FirebirdTest extends UnitTestCase { $this->setUp(); //Check - /*$table_exists = (bool)in_array('create_test', $this->tables); + $table_exists = (bool)in_array('create_test', $this->tables); echo "create_test exists :".(int)$table_exists.'
'; - $this->assertTrue($table_exists);*/ - } + $this->assertTrue($table_exists); + }*/ function TestCommitTransaction() { - $this->TestCreateTransaction(); + $res = $this->db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)'; $this->db->query($sql); @@ -124,7 +126,7 @@ class FirebirdTest extends UnitTestCase { function TestRollbackTransaction() { - $this->TestCreateTransaction(); + $res = $this->db->beginTransaction(); $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; $this->db->query($sql); @@ -133,6 +135,81 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue($res); } + + + function TestPreparedStatements() + { + $sql = <<db->prepare($sql); + $this->db->execute(array(1,"booger's", "Gross")); + + } + + function TestPrepareExecute() + { + $sql = <<db->prepare_execute($sql, array( + 2, "works", 'also?' + )); + + } + + function TestPrepareQuery() + { + $this->assertFalse($this->db->prepare_query('', array())); + } + + /*function TestDeleteTable() + { + //Attempt to delete the table + $sql = $this->db->sql->delete_table('create_test'); + $this->db->query($sql); + + //Reset + $this->tearDown(); + $this->setUp(); + + //Check + $table_exists = in_array('create_test', $this->tables); + $this->assertFalse($table_exists); + }*/ +} + +/** + * Firebird Query Builder Tests + */ +class FirebirdQBTest extends UnitTestCase { + + function __construct() + { + parent::__construct(); + } + + function setUp() + { + $dbpath = TEST_DIR.DS.'test_dbs'.DS.'FB_TEST_DB.FDB'; + + // Test the query builder + $params = new Stdclass(); + $params->type = 'firebird'; + $params->file = $dbpath; + $params->host = 'localhost'; + $params->user = 'sysdba'; + $params->pass = 'masterkey'; + $this->qb = new Query_Builder($params); + } + + function tearDown() + { + unset($this->qb); + } + function TestQBGet() { $query = $this->qb->get('create_test'); @@ -176,47 +253,4 @@ class FirebirdTest extends UnitTestCase { $this->assertTrue(is_resource($query)); } - - function TestPreparedStatements() - { - $sql = <<db->prepare($sql); - $this->db->execute(array(1,"booger's", "Gross")); - - } - - function TestPrepareExecute() - { - $sql = <<db->prepare_execute($sql, array( - 2, "works", 'also?' - )); - - } - - function TestPrepareQuery() - { - $this->assertFalse($this->db->prepare_query('', array())); - } - - function TestDeleteTable() - { - //Attempt to delete the table - $sql = $this->db->sql->delete_table('create_test'); - $this->db->query($sql); - - //Reset - $this->tearDown(); - $this->setUp(); - - //Check - $table_exists = in_array('create_test', $this->tables); - $this->assertFalse($table_exists); - } } \ No newline at end of file diff --git a/tests/databases/sqlite.php b/tests/databases/sqlite.php index 73c743a..b49edb3 100644 --- a/tests/databases/sqlite.php +++ b/tests/databases/sqlite.php @@ -130,50 +130,7 @@ SQL; $res = $this->db->rollback(); $this->assertTrue($res); } - - function TestQBGet() - { - $query = $this->qb->get('create_test'); - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBGetLimit() - { - $query = $this->qb->get('create_test', 2); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBGetLimitSkip() - { - $query = $this->qb->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBSelectWhereGet() - { - $query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBSelectWhereGet2() - { - $query = $this->qb->select('id, key as k, val')->where('id', 1)->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - } - - function TestQBSelectGet() - { - $query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1); - - $this->assertIsA($query, 'PDOStatement'); - - } - // This is really time intensive ! Run only when needed /*function TestDeleteTable() { @@ -190,4 +147,69 @@ SQL; $this->assertFalse(in_array('create_test', $dbs)); }*/ +} + +/** + * Class for testing Query Builder with SQLite + */ + class SQLiteQBTest extends UnitTestCase { + + function setUp() + { + $path = dirname(__FILE__)."/../test_dbs/test_sqlite.db"; + $this->db = new SQLite($path); + + $params = new Stdclass(); + $params->type = 'sqlite'; + $params->file = $path; + $params->host = 'localhost'; + $this->qb = new Query_Builder($params); + } + + function tearDown() + { + unset($this->db); + } + + function TestGet() + { + $query = $this->qb->get('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetLimit() + { + $query = $this->qb->get('create_test', 2); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetLimitSkip() + { + $query = $this->qb->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectWhereGet() + { + $query = $this->qb->select('id, key as k, val')->where('id >', 1)->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectWhereGet2() + { + $query = $this->qb->select('id, key as k, val')->where('id', 1)->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectGet() + { + $query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } } \ No newline at end of file diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 9499e067cc0ed163b170b89eea29bcc9384d91b3..d960203809ea27583d50f53eb927da2ca324b09f 100755 GIT binary patch delta 5188 zcmc&%ZEPIH8GdKIyLY#HUye=ebK>|o`{K)o;XZ8N8A9T42`OMX-oxkO1{#}Iq8~sU zu&Y)j&7qVuRn^qNfl1nmnifJuB9e=!q{?!A?Q7P-7vMzwQBe8$SSn{NinZ!E~v8 zjoYjc1Ofj>3_$Sz>z@)Pv``d|cpH?UNFeL9( zb0I}e7PT|}XM@`anF&sQe3U#-YYS?fhkzOWOZv6FRWU@Sj)z(<=$NSe$RwbOo* zp9)^MuzQT`9uszt)tnp?#YytV$0cdkzFcEuR_um{#hYLaey<6RkmOV6K1ICML*J>9 zVmD?I{|pzP0Dpk5U>6Qx#bv-6PRh8s3NN*=MsE_Om5VcR=HLtv;UlOaARN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