From 498fe54931945981ff63b13f92511e68a11a22bb Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 19 Mar 2012 10:19:34 -0400 Subject: [PATCH] Added Postgres Tests --- sys/db/drivers/pgsql_sql.php | 45 ++++++- tests/databases/pgsql-qb.php | 225 +++++++++++++++++++++++++++++++++- tests/databases/pgsql.php | 159 +++++++++++++++++++++++- tests/test_dbs/FB_TEST_DB.FDB | Bin 802816 -> 802816 bytes 4 files changed, 419 insertions(+), 10 deletions(-) diff --git a/sys/db/drivers/pgsql_sql.php b/sys/db/drivers/pgsql_sql.php index cc5baf6..8f903fe 100644 --- a/sys/db/drivers/pgsql_sql.php +++ b/sys/db/drivers/pgsql_sql.php @@ -19,7 +19,50 @@ class pgSQL_SQL extends DB_SQL { 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; + } + } + + // Join column definitons together + $columns = array(); + foreach($column_array as $n => $props) + { + $str = "{$n} "; + $str .= (isset($props['type'])) ? "{$props['type']} " : ""; + $str .= (isset($props['constraint'])) ? $props['constraint'] : ""; + + $columns[] = $str; + } + + // Generate the sql for the creation of the table + $sql = "CREATE TABLE \"{$name}\" ("; + $sql .= implode(", ", $columns); + $sql .= ")"; + + return $sql; } // -------------------------------------------------------------------------- diff --git a/tests/databases/pgsql-qb.php b/tests/databases/pgsql-qb.php index 8292756..f9fb634 100644 --- a/tests/databases/pgsql-qb.php +++ b/tests/databases/pgsql-qb.php @@ -15,12 +15,231 @@ class PgSQLQBTest 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->pgsql; + $params->type = "pgsql"; + + $this->db = new Query_Builder($params); + + echo '
Postgres Queries
'; + + } + } + function TestExists() { $this->assertTrue(in_array('pgsql', 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/pgsql.php b/tests/databases/pgsql.php index a528135..701e83b 100644 --- a/tests/databases/pgsql.php +++ b/tests/databases/pgsql.php @@ -19,19 +19,166 @@ */ class PgTest extends UnitTestCase { - /** - * __construct function. - * - * @access public - * @return void - */ 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->pgsql; + + $this->db = new PgSQL("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass); + } + } + + function tearDown() + { + unset($this->db); + } + function TestExists() { $this->assertTrue(in_array('pgsql', pdo_drivers())); } + + function TestConnection() + { + if (empty($this->db)) return; + + $this->assertIsA($this->db, 'PgSQL'); + } + + 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); + + echo $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); + } + } \ 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 3af7b2207ac5b175c82fb2d002b52f6dd71c6412..3829b02045bc9c5df5b69b8e6c2f13ffd19530c1 100755 GIT binary patch delta 1719 zcmai!O-vI(6vy9gTPU3_Ob}&_sO4Kp#I7j;y@(i$(WF2m#)BbXJQ+fb9xx`5#+Z$X zUWmqt_;EBI{Ydb}$-D93#TWynH(F3nMy>j0c3xRGG+i<~{Vg-U`R~WPY}UzUowNZ* zlPkMn>)c7<0gU&*?>^S#4d7tn-=qQq&%%DwI{CWc7I(Sfa?|A@ zmxo;*ae0l)Yh507dEF9^g}M3Eep{1>)z;h}vhd6HkDj}HK5sO@ zg?Ojp({T&3@veojoh>h{1l+dTVA5)b0qX!XVPDY0#OHO^!RA$JHBXG>ShMIYvL18J z7U=?O7js3{ZQiuSVu9@yo2#sG7>CQ7FtK4@GQmmRz-!CunxXzjEJ=k-q{!;bj4l3burubgExzQ~;qCla0)Arh6O;dvE$WM?oU+BUHS8x)y~)}{JkPA{ z{1*d$V)7G{f83w{oX$U5Ie$9fCni5J`A_Nm!#e*^mHdMNKQZ}<$v@!F->>thD(62E z@Dr1tnEX9D|3RI=V^#7;1Ab!i z6O$i&`HjC~A;-=dzr}}Dwom-Zu}bg8d^>4Z3qu9KDCNpzr zHgoOrj}p#WUT6DzF%2Dtn&PD7(5==7i@2aZ$~CFUp3TTH5p39(A}}?5J#!o$`771K zDwXPi|4m^KV((EnDPr$V`{15>Q>h9|-_@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