This repository has been archived on 2018-10-12. You can view files and clone it, but cannot push or open issues or pull requests.
OpenSQLManager/tests/databases/sqlite.php

236 lines
4.6 KiB
PHP

<?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();
}
function setUp()
{
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db';
$this->db = new SQLite($path);
}
function tearDown()
{
unset($this->db);
}
function TestConnection()
{
$this->assertIsA($this->db, 'SQLite');
}
function TestGetTables()
{
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
function TestGetSystemTables()
{
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
function TestCreateTransaction()
{
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
function TestCreateTable()
{
//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);
//Check
$dbs = $this->db->get_tables();
$this->assertEqual($dbs['create_test'], 'CREATE TABLE "create_test" (id INTEGER PRIMARY KEY, key TEXT , val TEXT )');
}
function TestPreparedStatements()
{
$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()
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
function TestCommitTransaction()
{
$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()
{
$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);
}
// This is really time intensive ! Run only when needed
/*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
$sql = $this->db->sql->delete_table('create_test');
$this->db->query($sql);
//Check
$dbs = $this->db->get_tables();
$this->assertFalse(in_array('create_test', $dbs));
}*/
}
// --------------------------------------------------------------------------
/**
* Class for testing Query Builder with SQLite
*/
class SQLiteQBTest extends UnitTestCase {
function setUp()
{
$path = TEST_DIR.DS.'test_dbs'.DS.'test_sqlite.db';
$params = new Stdclass();
$params->type = 'sqlite';
$params->file = $path;
$params->host = 'localhost';
$this->qb = new Query_Builder($params);
}
function tearDown()
{
unset($this->qb);
}
function TestGet()
{
$query = $this->qb->get('create_test');
$this->assertIsA($query, 'PDOStatement');
}
function TestGetLimit()
{
$query = $this->qb->get('create_test', 2);
$this->assertIsA($query, 'PDOStatement');
}
function TestGetLimitSkip()
{
$query = $this->qb->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectWhereGet()
{
$query = $this->qb->select('id, key as k, val')
->where('id >', 1)
->where('id <', 900)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectWhereGet2()
{
$query = $this->qb->select('id, key as k, val')
->where('id !=', 1)
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectGet()
{
$query = $this->qb->select('id, key as k, val')
->get('create_test', 2, 1);
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectFromGet()
{
$query = $this->qb->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->get();
$this->assertIsA($query, 'PDOStatement');
}
function TestSelectFromLimitGet()
{
$query = $this->qb->select('id, key as k, val')
->from('create_test ct')
->where('id >', 1)
->limit(3)
->get();
$this->assertIsA($query, 'PDOStatement');
}
}