Query/tests/databases/sqlite/sqlite.php

203 lines
4.5 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
*/
// --------------------------------------------------------------------------
/**
* SQLiteTest class.
2012-04-10 14:06:34 -04:00
*
2012-03-15 09:25:18 -04:00
* @extends UnitTestCase
*/
2012-04-10 14:06:34 -04:00
class SQLiteTest extends UnitTestCase {
2012-04-24 14:00:44 -04:00
public function __construct()
2012-04-10 14:06:34 -04:00
{
parent::__construct();
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function setUp()
2012-03-15 09:25:18 -04:00
{
2012-04-30 16:06:06 -04:00
$path = QTEST_DIR.QDS.'db_files'.QDS.'test_sqlite.db';
2012-03-15 09:25:18 -04:00
$this->db = new SQLite($path);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function tearDown()
2012-03-15 09:25:18 -04:00
{
unset($this->db);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-03-15 09:25:18 -04:00
2012-04-24 14:00:44 -04:00
public function TestConnection()
2012-03-15 09:25:18 -04:00
{
$this->assertIsA($this->db, 'SQLite');
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestGetTables()
2012-04-10 14:06:34 -04:00
{
$tables = $this->db->get_tables();
$this->assertTrue(is_array($tables));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestGetSystemTables()
2012-04-10 14:06:34 -04:00
{
$tables = $this->db->get_system_tables();
$this->assertTrue(is_array($tables));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestCreateTransaction()
2012-04-10 14:06:34 -04:00
{
$res = $this->db->beginTransaction();
$this->assertTrue($res);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestCreateTable()
2012-03-15 09:25:18 -04:00
{
//Attempt to create the table
$sql = $this->db->util->create_table('create_test',
2012-03-15 09:25:18 -04:00
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
2012-04-10 14:06:34 -04:00
),
2012-03-15 09:25:18 -04:00
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
//Attempt to create the table
$sql = $this->db->util->create_table('create_join',
2012-03-15 09:25:18 -04:00
array(
'id' => 'INTEGER',
'key' => 'TEXT',
'val' => 'TEXT',
2012-04-10 14:06:34 -04:00
),
2012-03-15 09:25:18 -04:00
array(
'id' => 'PRIMARY KEY'
)
);
$this->db->query($sql);
//Check
$dbs = $this->db->get_tables();
2012-04-10 14:06:34 -04:00
$this->assertTrue(in_array('create_test', $dbs));
2012-03-15 09:25:18 -04:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestTruncate()
2012-03-15 09:25:18 -04:00
{
$this->db->truncate('create_test');
$this->assertIsA($this->db->affected_rows(), 'int');
2012-04-10 14:06:34 -04:00
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestPreparedStatements()
2012-04-10 14:06:34 -04:00
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$statement = $this->db->prepare_query($sql, array(1,"boogers", "Gross"));
$statement->execute();
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestPrepareExecute()
2012-04-10 14:06:34 -04:00
{
$sql = <<<SQL
INSERT INTO "create_test" ("id", "key", "val")
VALUES (?,?,?)
SQL;
$this->db->prepare_execute($sql, array(
2, "works", 'also?'
));
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestCommitTransaction()
2012-04-10 14:06:34 -04:00
{
$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);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestRollbackTransaction()
2012-04-10 14:06:34 -04:00
{
$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);
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-03-15 09:25:18 -04:00
// This is really time intensive ! Run only when needed
2012-04-24 14:00:44 -04:00
/*public function TestDeleteTable()
2012-03-15 09:25:18 -04:00
{
//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();
2012-04-10 14:06:34 -04:00
$this->assertFalse(in_array('create_test', $dbs));
2012-03-15 09:25:18 -04:00
}*/
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-03-15 09:25:18 -04:00
2012-04-24 14:00:44 -04:00
public function TestGetDBs()
2012-04-10 14:06:34 -04:00
{
$this->assertFalse($this->db->get_dbs());
}
2012-04-24 14:00:44 -04:00
// --------------------------------------------------------------------------
2012-04-10 14:06:34 -04:00
2012-04-24 14:00:44 -04:00
public function TestGetSchemas()
2012-04-10 14:06:34 -04:00
{
$this->assertFalse($this->db->get_schemas());
}
2012-03-15 09:25:18 -04:00
}