From 2f4dfd9d9e4d254492cd98592f1093e4ec2787c1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Thu, 15 Mar 2012 15:46:04 -0400 Subject: [PATCH] Added mysql tests --- .gitignore | 1 + query_builder.php | 2 +- tests/databases/mysql-qb.php | 224 +++++++++++++++++++++++++++++++++- tests/databases/mysql.php | 149 +++++++++++++++++++++- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes tests/test_dbs/test_sqlite.db | Bin 3072 -> 3072 bytes 6 files changed, 371 insertions(+), 5 deletions(-) create mode 100644 .gitignore 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 6a52641bbd9d597278e52025e2de5a8723f0b7a9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