From 70ccdfea0fdaf4a92448e3e40fd3ec4a31de420e Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 15 Mar 2012 16:39:01 -0400 Subject: [PATCH] MySQL tests --- .gitignore | 3 +- sys/db/drivers/mysql.php | 23 ++-- sys/db/drivers/mysql_sql.php | 65 +++++++++- tests/databases/mysql-qb.php | 224 ++++++++++++++++++++++++++++++++++- tests/databases/mysql.php | 154 +++++++++++++++++++++++- 5 files changed, 450 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index d910a93..344ed21 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ settings.json errors.txt */simpletest/* -tests/test_dbs/* \ No newline at end of file +tests/test_dbs/* +test_config.json \ No newline at end of file diff --git a/sys/db/drivers/mysql.php b/sys/db/drivers/mysql.php index 3957dd3..0196e75 100644 --- a/sys/db/drivers/mysql.php +++ b/sys/db/drivers/mysql.php @@ -12,11 +12,11 @@ // -------------------------------------------------------------------------- - /** - * MySQL specific class - * - * @extends DB_PDO - */ +/** + * MySQL specific class + * + * @extends DB_PDO + */ class MySQL extends DB_PDO { /** @@ -57,7 +57,7 @@ class MySQL extends DB_PDO { public function get_dbs() { $res = $this->query("SHOW DATABASES"); - return $this->fetchAll(PDO::FETCH_ASSOC); + return array_values($this->fetchAll(PDO::FETCH_ASSOC)); } // -------------------------------------------------------------------------- @@ -70,7 +70,16 @@ class MySQL extends DB_PDO { public function get_tables() { $res = $this->query("SHOW TABLES"); - return $res->fetchAll(PDO::FETCH_ASSOC); + + $tables = array(); + $rows = $res->fetchAll(PDO::FETCH_NUM); + + foreach($rows as $r) + { + $tables[] = $r[0]; + } + + return $tables; } // -------------------------------------------------------------------------- diff --git a/sys/db/drivers/mysql_sql.php b/sys/db/drivers/mysql_sql.php index 04e2fc6..c2fa60d 100644 --- a/sys/db/drivers/mysql_sql.php +++ b/sys/db/drivers/mysql_sql.php @@ -10,12 +10,12 @@ * @license http://philsturgeon.co.uk/code/dbad-license */ - // -------------------------------------------------------------------------- +// -------------------------------------------------------------------------- - /** - * MySQL specifc SQL - */ - class MySQL_SQL extends DB_SQL{ +/** + * MySQL specifc SQL + */ +class MySQL_SQL extends DB_SQL{ /** * Convienience public function for creating a new MySQL table @@ -29,7 +29,60 @@ */ public function create_table($name, $columns, array $constraints=array(), array $indexes=array()) { - //TODO: implement + $column_array = array(); + + // Reorganize into an array indexed with column information + // Eg $column_array[$colname] = array( + // 'type' => ..., + // 'constraint' => ..., + // 'index' => ..., + // ) + foreach($columns as $colname => $type) + { + if(is_numeric($colname)) + { + $colname = $type; + } + + $column_array[$colname] = array(); + $column_array[$colname]['type'] = ($type !== $colname) ? $type : ''; + } + + if( ! empty($constraints)) + { + foreach($constraints as $col => $const) + { + $column_array[$col]['constraint'] = "{$const} ({$col})"; + } + } + + // Join column definitons together + $columns = array(); + foreach($column_array as $n => $props) + { + $n = trim($n, '`'); + + $str = "`{$n}` "; + $str .= (isset($props['type'])) ? "{$props['type']} " : ""; + + $columns[] = $str; + } + + // Add constraints + foreach($column_array as $n => $props) + { + if (isset($props['constraint'])) + { + $columns[] = $props['constraint']; + } + } + + // Generate the sql for the creation of the table + $sql = "CREATE TABLE IF NOT EXISTS `{$name}` ("; + $sql .= implode(", ", $columns); + $sql .= ")"; + + return $sql; } // -------------------------------------------------------------------------- diff --git a/tests/databases/mysql-qb.php b/tests/databases/mysql-qb.php index 4ef0a92..ff31744 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 ca3eb99..752c956 100644 --- a/tests/databases/mysql.php +++ b/tests/databases/mysql.php @@ -21,12 +21,162 @@ class MySQLTest extends UnitTestCase { function __construct() { - + parent::__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' => 'int(10)', + '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' => 'int(10)', + 'key' => 'TEXT', + 'val' => 'TEXT', + ), + array( + 'id' => 'PRIMARY KEY' + ) + ); + $this->db->query($sql); + + //Check + $dbs = $this->db->get_tables(); + + $this->assertTrue(in_array('create_test', $dbs)); + + } + + 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); + } + } -