2012-02-02 19:04:10 -05:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* OpenSQLManager
|
|
|
|
*
|
|
|
|
* Free Database manager for Open Source Databases
|
|
|
|
*
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/OpenSQLManager
|
|
|
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SQLiteTest class.
|
|
|
|
*
|
|
|
|
* @extends UnitTestCase
|
|
|
|
*/
|
|
|
|
class SQLiteTest extends UnitTestCase {
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
2012-03-06 16:57:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function setUp()
|
|
|
|
{
|
2012-02-29 15:25:35 -05:00
|
|
|
$path = dirname(__FILE__)."/../test_dbs/test_sqlite.db";
|
|
|
|
$this->db = new SQLite($path);
|
2012-03-08 12:37:33 -05:00
|
|
|
|
|
|
|
$params = new Stdclass();
|
|
|
|
$params->type = 'sqlite';
|
|
|
|
$params->file = $path;
|
|
|
|
$params->host = 'localhost';
|
|
|
|
$this->qb = new Query_Builder($params);
|
2012-02-02 19:04:10 -05:00
|
|
|
}
|
2012-03-06 16:57:43 -05:00
|
|
|
|
|
|
|
function tearDown()
|
|
|
|
{
|
|
|
|
unset($this->db);
|
|
|
|
}
|
2012-02-07 08:18:38 -05:00
|
|
|
|
|
|
|
function TestConnection()
|
|
|
|
{
|
2012-02-07 08:45:56 -05:00
|
|
|
$this->assertIsA($this->db, 'SQLite');
|
2012-02-07 08:18:38 -05:00
|
|
|
}
|
2012-02-28 16:02:37 -05:00
|
|
|
|
2012-02-07 20:31:36 -05:00
|
|
|
function TestGetTables()
|
|
|
|
{
|
|
|
|
$tables = $this->db->get_tables();
|
2012-02-28 16:02:37 -05:00
|
|
|
$this->assertTrue(is_array($tables));
|
2012-02-08 09:01:51 -05:00
|
|
|
}
|
2012-02-13 13:46:53 -05:00
|
|
|
|
|
|
|
function TestGetSystemTables()
|
|
|
|
{
|
|
|
|
$tables = $this->db->get_system_tables();
|
|
|
|
|
|
|
|
$this->assertTrue(is_array($tables));
|
|
|
|
}
|
2012-03-06 16:57:43 -05:00
|
|
|
|
|
|
|
function TestCreateTransaction()
|
|
|
|
{
|
|
|
|
$res = $this->db->beginTransaction();
|
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
2012-02-08 09:01:51 -05:00
|
|
|
|
|
|
|
function TestCreateTable()
|
|
|
|
{
|
|
|
|
//Attempt to create the table
|
2012-02-29 14:36:42 -05:00
|
|
|
$sql = $this->db->sql->create_table('create_test',
|
2012-02-28 12:28:56 -05:00
|
|
|
array(
|
|
|
|
'id' => 'INTEGER',
|
|
|
|
'key' => 'TEXT',
|
|
|
|
'val' => 'TEXT',
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'id' => 'PRIMARY KEY'
|
|
|
|
)
|
|
|
|
);
|
2012-02-08 09:01:51 -05:00
|
|
|
$this->db->query($sql);
|
|
|
|
|
|
|
|
//Check
|
|
|
|
$dbs = $this->db->get_tables();
|
2012-02-28 12:28:56 -05:00
|
|
|
$this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )');
|
2012-02-08 09:01:51 -05:00
|
|
|
}
|
2012-02-28 12:28:56 -05:00
|
|
|
|
|
|
|
function TestPreparedStatements()
|
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
2012-03-05 13:02:39 -05:00
|
|
|
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
|
2012-02-28 12:28:56 -05:00
|
|
|
|
|
|
|
$statement->execute();
|
2012-02-08 09:01:51 -05:00
|
|
|
|
2012-02-28 12:28:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function TestPrepareExecute()
|
|
|
|
{
|
|
|
|
$sql = <<<SQL
|
|
|
|
INSERT INTO "create_test" ("id", "key", "val")
|
|
|
|
VALUES (?,?,?)
|
|
|
|
SQL;
|
|
|
|
$this->db->prepare_execute($sql, array(
|
|
|
|
2, "works", 'also?'
|
|
|
|
));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-03-06 16:57:43 -05:00
|
|
|
function TestCommitTransaction()
|
|
|
|
{
|
|
|
|
$this->TestCreateTransaction();
|
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$this->TestCreateTransaction();
|
|
|
|
|
|
|
|
$sql = 'INSERT INTO "create_test" ("id", "key", "val") VALUES (182, 96, 43)';
|
|
|
|
$this->db->query($sql);
|
|
|
|
|
|
|
|
$res = $this->db->rollback();
|
|
|
|
$this->assertTrue($res);
|
|
|
|
}
|
|
|
|
|
2012-03-08 12:37:33 -05:00
|
|
|
function TestQBGet()
|
|
|
|
{
|
|
|
|
$query = $this->qb->get('create_test');
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestQBGetLimit()
|
|
|
|
{
|
|
|
|
$query = $this->qb->get('create_test', 2);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
|
|
|
|
|
|
|
function TestQBGetLimitSkip()
|
|
|
|
{
|
|
|
|
$query = $this->qb->get('create_test', 2, 1);
|
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
}
|
2012-03-09 08:10:37 -05:00
|
|
|
|
|
|
|
function TestQBSelectGet()
|
|
|
|
{
|
2012-03-09 08:26:16 -05:00
|
|
|
$query = $this->qb->select('id, key as k, val')->get('create_test', 2, 1);
|
2012-03-09 08:10:37 -05:00
|
|
|
|
|
|
|
$this->assertIsA($query, 'PDOStatement');
|
|
|
|
|
|
|
|
}
|
2012-03-08 12:37:33 -05:00
|
|
|
|
2012-02-08 09:01:51 -05:00
|
|
|
function TestDeleteTable()
|
|
|
|
{
|
|
|
|
//Make sure the table exists to delete
|
|
|
|
$dbs = $this->db->get_tables();
|
|
|
|
$this->assertTrue(isset($dbs['create_test']));
|
|
|
|
|
|
|
|
//Attempt to delete the table
|
2012-02-29 14:36:42 -05:00
|
|
|
$sql = $this->db->sql->delete_table('create_test');
|
2012-02-08 09:01:51 -05:00
|
|
|
$this->db->query($sql);
|
|
|
|
|
|
|
|
//Check
|
|
|
|
$dbs = $this->db->get_tables();
|
|
|
|
$this->assertTrue(empty($dbs['create_test']));
|
2012-02-07 20:31:36 -05:00
|
|
|
}
|
2012-02-28 12:28:56 -05:00
|
|
|
|
2012-02-02 19:04:10 -05:00
|
|
|
}
|