Query/tests/databases/pgsql/pgsql.php

187 lines
3.7 KiB
PHP
Raw Normal View History

2012-03-15 09:25:18 -04:00
<?php
/**
2012-04-10 14:06:34 -04:00
* OpenSQLManager
2012-03-15 09:25:18 -04:00
*
2012-04-10 14:06:34 -04:00
* Free Database manager for Open Source Databases
2012-03-15 09:25:18 -04:00
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2012
2012-04-10 14:06:34 -04:00
* @link https://github.com/aviat4ion/OpenSQLManager
* @license http://philsturgeon.co.uk/code/dbad-license
2012-03-15 09:25:18 -04:00
*/
// --------------------------------------------------------------------------
/**
* PgTest class.
2012-04-10 14:06:34 -04:00
*
2012-03-15 09:25:18 -04:00
* @extends UnitTestCase
*/
2012-03-19 13:38:49 -04:00
class PgTest extends DBTest {
2012-03-15 09:25:18 -04:00
function __construct()
{
parent::__construct();
}
2012-04-10 14:06:34 -04:00
2012-03-19 10:40:16 -04:00
function setUp()
{
// Attempt to connect, if there is a test config file
2012-04-13 13:16:48 -04:00
if (is_file(BASE_DIR . "test_config.json"))
2012-03-19 10:40:16 -04:00
{
2012-04-13 13:16:48 -04:00
$params = json_decode(file_get_contents(BASE_DIR . "test_config.json"));
2012-03-19 10:40:16 -04:00
$params = $params->pgsql;
2012-04-10 14:06:34 -04:00
2012-03-19 10:40:16 -04:00
$this->db = new PgSQL("host={$params->host};port={$params->port};dbname={$params->database}", $params->user, $params->pass);
}
2012-03-22 16:33:28 -04:00
elseif (($var = getenv('CI')))
2012-03-22 16:26:17 -04:00
{
$this->db = new PgSQL('host=127.0.0.1;port=5432;dbname=test', 'postgres');
}
2012-03-19 10:40:16 -04:00
}
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
function TestExists()
{
$this->assertTrue(in_array('pgsql', pdo_drivers()));
}
2012-04-10 14:06:34 -04:00
2012-03-19 10:40:16 -04:00
function TestConnection()
{
2012-04-10 14:06:34 -04:00
if (empty($this->db)) return;
2012-03-19 10:40:16 -04:00
$this->assertIsA($this->db, 'PgSQL');
}
2012-03-22 16:46:47 -04:00
function TestCreateTable()
2012-03-19 10:40:16 -04:00
{
2012-04-10 14:06:34 -04:00
if (empty($this->db)) return;
2012-03-22 16:46:47 -04:00
// Drop the table(s) if they exist
$sql = 'DROP TABLE IF EXISTS "create_test"';
$this->db->query($sql);
$sql = 'DROP TABLE IF EXISTS "create_join"';
$this->db->query($sql);
2012-04-10 14:06:34 -04:00
2012-03-19 10:40:16 -04:00
//Attempt to create the table
2012-04-10 14:06:34 -04:00
$sql = $this->db->sql->create_table('create_test',
2012-03-19 10:40:16 -04:00
array(
'id' => 'integer',
'key' => 'TEXT',
'val' => 'TEXT',
2012-04-10 14:06:34 -04:00
),
2012-03-19 10:40:16 -04:00
array(
'id' => 'PRIMARY KEY'
)
);
2012-04-10 14:06:34 -04:00
2012-03-19 10:40:16 -04:00
$this->db->query($sql);
2012-04-10 14:06:34 -04:00
2012-03-19 10:40:16 -04:00
//Attempt to create the table
2012-04-10 14:06:34 -04:00
$sql = $this->db->sql->create_table('create_join',
2012-03-19 10:40:16 -04:00
array(
'id' => 'integer',
'key' => 'TEXT',
'val' => 'TEXT',
2012-04-10 14:06:34 -04:00
),
2012-03-19 10:40:16 -04:00
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
2012-04-10 14:06:34 -04:00
2012-03-22 16:46:47 -04:00
//echo $sql.'<br />';
2012-04-10 14:06:34 -04:00
2012-03-22 16:46:47 -04:00
//Reset
unset($this->db);
$this->setUp();
2012-03-19 10:40:16 -04:00
//Check
$dbs = $this->db->get_tables();
$this->assertTrue(in_array('create_test', $dbs));
2012-04-10 14:06:34 -04:00
}
function TestTruncate()
{
$this->db->truncate('create_test');
$this->db->truncate('create_join');
$ct_query = $this->db->query('SELECT * FROM create_test');
$cj_query = $this->db->query('SELECT * FROM create_join');
}
2012-03-19 10:40:16 -04:00
2012-04-10 14:06:34 -04:00
function TestPreparedStatements()
{
if (empty($this->db)) return;
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
function TestPrepareExecute()
{
if (empty($this->db)) return;
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->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);
}
function TestGetSchemas()
{
$this->assertTrue(is_array($this->db->get_schemas()));
}
function TestGetSequences()
{
$this->assertTrue(is_array($this->db->get_sequences()));
}
function TestGetsProcedures()
{
$this->assertTrue(is_array($this->db->get_procedures()));
}
function TestGetTriggers()
{
$this->assertTrue(is_array($this->db->get_triggers()));
2012-03-22 16:46:47 -04:00
}
2012-03-15 09:25:18 -04:00
}