diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44d7ee7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +test_config.json diff --git a/query_builder.php b/query_builder.php index 1953d22..e3b6fe1 100644 --- a/query_builder.php +++ b/query_builder.php @@ -82,7 +82,7 @@ class Query_Builder { switch($dbtype) { default: - $this->db = new $dbtype("host={$params->host};port={$params->port};dbname={$this->database}", $params->user, $params->pass); + $this->db = new $dbtype("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass); break; case "sqlite": diff --git a/tests/databases/mysql-qb.php b/tests/databases/mysql-qb.php index 2b6d235..c58413a 100644 --- a/tests/databases/mysql-qb.php +++ b/tests/databases/mysql-qb.php @@ -15,12 +15,230 @@ class MySQLQBTest extends UnitTestCase { function __construct() - { - - } + { + parent::__construct(); + + // Attempt to connect, if there is a test config file + if (is_file("../test_config.json")) + { + $params = json_decode(file_get_contents("../test_config.json")); + $params = $params->mysql; + $params->type = "mysql"; + + $this->db = new Query_Builder($params); + + echo '
MySQL Queries
'; + + } + } + function TestExists() { $this->assertTrue(in_array('mysql', pdo_drivers())); } + + function TestGet() + { + if (empty($this->db)) return; + + $query = $this->db->get('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetLimit() + { + if (empty($this->db)) return; + + $query = $this->db->get('create_test', 2); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGetLimitSkip() + { + if (empty($this->db)) return; + + $query = $this->db->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectWhereGet() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->where('id >', 1) + ->where('id <', 900) + ->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectWhereGet2() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->where('id !=', 1) + ->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectGet() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->get('create_test', 2, 1); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectFromGet() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->from('create_test ct') + ->where('id >', 1) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestSelectFromLimitGet() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->from('create_test ct') + ->where('id >', 1) + ->limit(3) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestOrderBy() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->from('create_test') + ->where('id >', 0) + ->where('id <', 9000) + ->order_by('id', 'DESC') + ->order_by('k', 'ASC') + ->limit(5,2) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestOrderByRandom() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->from('create_test') + ->where('id >', 0) + ->where('id <', 9000) + ->order_by('id', 'rand') + ->limit(5,2) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestGroupBy() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->from('create_test') + ->where('id >', 0) + ->where('id <', 9000) + ->group_by('k') + ->group_by('val') + ->order_by('id', 'DESC') + ->order_by('k', 'ASC') + ->limit(5,2) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestOrWhere() + { + if (empty($this->db)) return; + + $query = $this->db->select('id, key as k, val') + ->from('create_test') + ->where(' id ', 1) + ->or_where('key >', 0) + ->limit(2, 1) + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestLike() + { + if (empty($this->db)) return; + + $query = $this->db->from('create_test') + ->like('key', 'og') + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestJoin() + { + if (empty($this->db)) return; + + $query = $this->db->from('create_test') + ->join('create_join cj', 'cj.id = create_test.id') + ->get(); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestInsert() + { + if (empty($this->db)) return; + + $query = $this->db->set('id', 4) + ->set('key', 4) + ->set('val', 5) + ->insert('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestUpdate() + { + if (empty($this->db)) return; + + $query = $this->db->set('id', 4) + ->set('key', 'gogle') + ->set('val', 'non-word') + ->where('id', 4) + ->update('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } + + function TestDelete() + { + if (empty($this->db)) return; + + $query = $this->db->where('id', 4)->delete('create_test'); + + $this->assertIsA($query, 'PDOStatement'); + } } \ No newline at end of file diff --git a/tests/databases/mysql.php b/tests/databases/mysql.php index 1c4a01a..e1f1611 100644 --- a/tests/databases/mysql.php +++ b/tests/databases/mysql.php @@ -21,12 +21,159 @@ class MySQLTest extends UnitTestCase { function __construct() { - + + } + + function setUp() + { + // Attempt to connect, if there is a test config file + if (is_file("../test_config.json")) + { + $params = json_decode(file_get_contents("../test_config.json")); + $params = $params->mysql; + + $this->db = new MySQL("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass); + } + } + + function tearDown() + { + unset($this->db); } function TestExists() { $this->assertTrue(in_array('mysql', pdo_drivers())); } + + function TestConnection() + { + if (empty($this->db)) return; + + $this->assertIsA($this->db, 'MySQL'); + } + + function TestGetTables() + { + if (empty($this->db)) return; + + $tables = $this->db->get_tables(); + $this->assertTrue(is_array($tables)); + } + + function TestGetSystemTables() + { + if (empty($this->db)) return; + + $tables = $this->db->get_system_tables(); + + $this->assertTrue(is_array($tables)); + } + + function TestCreateTransaction() + { + if (empty($this->db)) return; + + $res = $this->db->beginTransaction(); + $this->assertTrue($res); + } + + function TestCreateTable() + { + if (empty($this->db)) return; + + //Attempt to create the table + $sql = $this->db->sql->create_table('create_test', + array( + 'id' => 'INTEGER', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); + $this->db->query($sql); + + //Attempt to create the table + $sql = $this->db->sql->create_table('create_join', + array( + 'id' => 'INTEGER', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); + $this->db->query($sql); + + //Check + $dbs = $this->db->get_tables(); + //$this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )'); + } + + /*function TestTruncate() + { + if (empty($this->db)) return; + + $this->db->truncate('create_test'); + $this->assertIsA($this->db->affected_rows(), 'int'); + }*/ + + function TestPreparedStatements() + { + if (empty($this->db)) return; + + $sql = <<db->prepare_query($sql, array(1,"boogers", "Gross")); + + $statement->execute(); + + } + + function TestPrepareExecute() + { + if (empty($this->db)) return; + + $sql = <<db->prepare_execute($sql, array( + 2, "works", 'also?' + )); + + } + + function TestCommitTransaction() + { + if (empty($this->db)) return; + + $res = $this->db->beginTransaction(); + + $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (10, 12, 14)'; + $this->db->query($sql); + + $res = $this->db->commit(); + $this->assertTrue($res); + } + + function TestRollbackTransaction() + { + if (empty($this->db)) return; + + $res = $this->db->beginTransaction(); + + $sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)'; + $this->db->query($sql); + + $res = $this->db->rollback(); + $this->assertTrue($res); + } + } diff --git a/tests/test_dbs/FB_TEST_DB.FDB b/tests/test_dbs/FB_TEST_DB.FDB index 6a52641..01e68d6 100755 Binary files a/tests/test_dbs/FB_TEST_DB.FDB and b/tests/test_dbs/FB_TEST_DB.FDB differ diff --git a/tests/test_dbs/test_sqlite.db b/tests/test_dbs/test_sqlite.db index af75544..3490d13 100644 Binary files a/tests/test_dbs/test_sqlite.db and b/tests/test_dbs/test_sqlite.db differ